84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
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, `<!doctype html>
|
|
<title>cacher API</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<h1><a href="https://gitea.nulo.in/Nulo/cacher">cacher</a></h1>
|
|
|
|
<h2><code>POST /cache/$NAME</code></h2>
|
|
<ul>
|
|
<li><code>Authorization: Bearer $ADMIN_SECRET</code></li>
|
|
</ul>
|
|
<p>Crea el cache y devuelve un JSON con el secreto.</p>
|
|
<pre>curl --request POST -H "Authorization: Bearer $ADMIN_SECRET" $URL/cache/$CACHE_NAME</pre>
|
|
|
|
<h2><code>PUT /cache/$NAME</code></h2>
|
|
<ul>
|
|
<li><code>Authorization: Bearer $CACHE_SECRET</code></li>
|
|
<li>Pasar archivo como body (no es multipart)</li>
|
|
</ul>
|
|
<p>Guarda en el cache un archivo arbitrario.</p>
|
|
<pre>curl --request PUT --upload-file $ARCHIVO -H "Authorization: Bearer $CACHE_SECRET" $URL/cache/$CACHE_NAME</pre>
|
|
|
|
<h2><code>GET /cache/$NAME</code></h2>
|
|
<ul>
|
|
<li><code>Authorization: Bearer $CACHE_SECRET</code></li>
|
|
</ul>
|
|
<p>Envía el archivo en el cache.</p>
|
|
<pre>curl --output $FILE -H "Authorization: Bearer $CACHE_SECRET" $URL/cache/$CACHE_NAME</pre>
|
|
`)
|
|
}
|