Files
minibin/main.go
T
whoswhip 505cb7f757 style the home page, get rid of nojs pages
instead of having dedicated nojs pages, i will instead just use javascript to enhance the already existing pages
2026-11-12 18:33:15 -05:00

64 lines
1.4 KiB
Go

package main
import (
"html/template"
"log"
"net/http"
"os"
)
func main() {
config, err := loadConfig()
if err != nil {
log.Fatal(err)
}
log.Println("config loaded")
db, err := initDatabase(config.DatabasePath)
if err != nil {
log.Fatal(err)
}
log.Println("database initialized")
if err := os.MkdirAll(config.UploadDir, 0700); err != nil {
log.Fatal(err)
}
pages, err := template.ParseFiles("templates/home.html")
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("static"))
mux.Handle("GET /css/", fs)
mux.Handle("GET /js/", fs)
mux.Handle("GET /static/", http.StripPrefix("/static/", fs))
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
data := HomePageData{
Expiries: config.Expiries,
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := pages.ExecuteTemplate(w, "home.html", data); err != nil {
log.Println("render home page:", err)
}
})
mux.HandleFunc("GET /{id}", contentHandler(db, config))
mux.HandleFunc("POST /upload", uploadHandler(db, config))
mux.HandleFunc("DELETE /{id}", deleteHandler(db, config))
mux.HandleFunc("POST /{id}/delete", deleteHandler(db, config))
mux.HandleFunc("GET /health", health)
log.Println("listening on http://localhost:8080")
err = http.ListenAndServe("localhost:8080", mux)
if err != nil {
log.Fatal(err)
}
}