shelves/backend/routes/routes.go
2024-11-15 13:34:42 -05:00

43 lines
989 B
Go

package routes
import (
"html/template"
"net/http"
"shelves/backend/httpx"
"strings"
"shelves"
)
func html(w http.ResponseWriter, s template.HTML) {
w.Header().Add("Content-Type", "text/html")
w.Write([]byte(s))
}
func redirectSetup(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := httpx.GetCtx(r)
if ctx.NeedsOwnerSetup && !strings.HasPrefix(r.URL.Path, "/static") {
if r.URL.Path != "/setup" {
httpx.SeeOther(w, "/setup")
return
}
}
next.ServeHTTP(w, r)
})
}
func Routes() http.Handler {
mux := http.NewServeMux()
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(shelves.Frontend)))
mux.HandleFunc("GET /{$}", HomeGet)
mux.HandleFunc("GET /setup", SetupGet)
mux.HandleFunc("POST /setup", SetupPost)
mux.HandleFunc("GET /login", LoginGet)
mux.HandleFunc("POST /login", LoginPost)
mux.HandleFunc("DELETE /login", LoginDelete)
return redirectSetup(mux)
}