homelabby/internal/api/router.go
Mikkel Georgsen 77e5a78d5a feat(01-foundation-01): Go module init, chi server, go:embed SPA scaffold
- Initialize module git.georgsen.dk/hwlab with Go 1.23
- Install chi v5.2.5, go-redis v9.18.0, viper v1.21.0, godotenv v1.5.1, uuid v1.6.0, go-netbox v4.3.0
- Create health handler GET /api/health returning {status:ok, version:0.1.0}
- Create chi router with Logger/Recoverer/RealIP middleware and SPA fallback
- Embed web/dist via assets.go at module root (go:embed cannot use .. paths)
- Create stub web/dist/index.html with ClickHouse dark theme
- TestHealth passes
2026-04-10 01:17:03 +00:00

30 lines
690 B
Go

package api
import (
"io/fs"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"git.georgsen.dk/hwlab/internal/api/handlers"
)
// NewRouter creates the chi router. staticFiles is the fs.FS rooted at web/dist,
// passed from main.go where the go:embed directive lives.
func NewRouter(staticFiles fs.FS) http.Handler {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.RealIP)
r.Route("/api", func(r chi.Router) {
r.Get("/health", handlers.Health)
})
// SPA fallback — serve index.html for all non-API routes
fileServer := http.FileServer(http.FS(staticFiles))
r.Handle("/*", fileServer)
return r
}