da2d72e95d
The //go:embed static directive nests files under static/, so after StripPrefix removes /static/ from the URL, the FileServer couldn't find the files. Use fs.Sub to root the FS at the static/ subdirectory. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
|
|
"provisioning/internal/api"
|
|
"provisioning/internal/events"
|
|
"provisioning/internal/web"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
)
|
|
|
|
type Deps struct {
|
|
HostAPI *api.HostAPI
|
|
BootAPI *api.BootAPI
|
|
UI *api.UI
|
|
Hub *events.Hub
|
|
}
|
|
|
|
func NewRouter(d Deps) http.Handler {
|
|
r := chi.NewRouter()
|
|
r.Use(middleware.RealIP)
|
|
r.Use(middleware.Recoverer)
|
|
r.Use(middleware.Logger)
|
|
|
|
// Static files
|
|
staticFS, _ := fs.Sub(web.Static, "static")
|
|
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
|
|
|
|
// SSE
|
|
r.Get("/events", d.Hub.ServeSSE)
|
|
|
|
// Dashboard UI
|
|
r.Get("/", d.UI.Dashboard)
|
|
r.Get("/hosts/new", d.UI.NewHostForm)
|
|
r.Post("/hosts", d.UI.CreateHost)
|
|
r.Get("/hosts/{id}", d.UI.HostDetail)
|
|
r.Post("/hosts/{id}/rebuild", d.UI.TriggerRebuild)
|
|
r.Post("/hosts/{id}/delete", d.UI.DeleteHost)
|
|
r.Get("/images", d.UI.ImagesPage)
|
|
|
|
// Host JSON API
|
|
r.Route("/api/hosts", func(r chi.Router) {
|
|
r.Get("/", d.HostAPI.List)
|
|
r.Post("/", d.HostAPI.Create)
|
|
r.Get("/{id}", d.HostAPI.Get)
|
|
r.Delete("/{id}", d.HostAPI.Delete)
|
|
r.Post("/{id}/rebuild", d.HostAPI.Rebuild)
|
|
})
|
|
|
|
// Boot / PXE endpoints
|
|
r.Get("/ipxe/{mac}", d.BootAPI.IPXEScript)
|
|
r.Post("/api/boot/answer", d.BootAPI.AnswerFile)
|
|
r.Post("/api/hosts/{id}/installed", d.BootAPI.InstallComplete)
|
|
r.Get("/api/hosts/{id}/first-boot-script", d.BootAPI.FirstBootScript)
|
|
r.Post("/api/hosts/{id}/phone-home", d.BootAPI.PhoneHome)
|
|
|
|
return r
|
|
}
|