package server import ( "context" "io/fs" "net/http" "github.com/mjensen/weimar/internal/config" ) func New(cfg config.Config, webFS fs.FS) (*Server, error) { mux := http.NewServeMux() // API routes mux.HandleFunc("GET /api/health", handleHealth) // Auth mux.HandleFunc("POST /api/auth/login", handleNotImplemented) mux.HandleFunc("POST /api/auth/register", handleNotImplemented) // Images mux.HandleFunc("POST /api/upload", handleNotImplemented) mux.HandleFunc("GET /api/images", handleNotImplemented) mux.HandleFunc("GET /api/images/{id}", handleNotImplemented) mux.HandleFunc("GET /api/images/{id}/download", handleNotImplemented) mux.HandleFunc("GET /api/images/{id}/thumbnail", handleNotImplemented) // Tags mux.HandleFunc("GET /api/tags/suggest", handleNotImplemented) // SPA — serve index.html for all non-API, non-file routes spaFS := http.FileServer(http.FS(webFS)) mux.Handle("/", spaFS) return &Server{ cfg: cfg, mux: mux, }, nil } type Server struct { cfg config.Config mux *http.ServeMux } func (s *Server) Start(ctx context.Context) error { addr := s.cfg.Server.Host + ":" + itoa(s.cfg.Server.Port) srv := &http.Server{Addr: addr, Handler: s.mux} go func() { <-ctx.Done() srv.Shutdown(context.Background()) }() return srv.ListenAndServe() } func itoa(n int) string { if n == 0 { return "0" } var buf [20]byte i := len(buf) for n > 0 { i-- buf[i] = byte('0' + n%10) n /= 10 } return string(buf[i:]) } func handleHealth(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"status":"ok"}`)) } func handleNotImplemented(w http.ResponseWriter, r *http.Request) { http.Error(w, "not implemented", http.StatusNotImplemented) }