package main import ( "fmt" "log" "net/http" "os" migrate "gitea.nulo.in/Nulo/go-migrate" "github.com/jmoiron/sqlx" _ "github.com/mattn/go-sqlite3" ) var admin_secret string func main() { if len(os.Args) < 2 { log.Fatal("Missing port argument") } admin_secret = os.Getenv("CACHER_ADMIN_SECRET") if len(admin_secret) == 0 { log.Fatal("Missing CACHER_ADMIN_SECRET") } err := os.MkdirAll("./files/", 0700) if err != nil { log.Fatalf("MkdirAll: %w", err) } db := sqlx.MustConnect("sqlite3", "file:./db.db?_foreign_keys=true") migrator := migrate.Sqlx{ Migrations: []migrate.SqlxMigration{ migrate.SqlxQueryMigration( "0001_create_caches", `create table caches( name text not null primary key, secret text not null );`, "", ), }, } err = migrator.Migrate(db.DB, "sqlite3") if err != nil { log.Fatalf("Migrate() err = %v; want nil", err) } http.HandleFunc("/", index) http.Handle("/cache/", &cache{db: db}) log.Fatal(http.ListenAndServe(os.Args[1], nil)) } func index(w http.ResponseWriter, req *http.Request) { w.Header().Add("Content-Type", "text/html; charset=utf-8") fmt.Fprintf(w, ` cacher API

cacher

POST /cache/$NAME

Crea el cache y devuelve un JSON con el secreto.

curl --request POST -H "Authorization: Bearer $ADMIN_SECRET" $URL/cache/$CACHE_NAME

PUT /cache/$NAME

Guarda en el cache un archivo arbitrario.

curl --request PUT --upload-file $ARCHIVO -H "Authorization: Bearer $CACHE_SECRET" $URL/cache/$CACHE_NAME

GET /cache/$NAME

EnvĂ­a el archivo en el cache.

curl --output $FILE -H "Authorization: Bearer $CACHE_SECRET" $URL/cache/$CACHE_NAME
`) }