From 589c9ebe19f5b58ee43a005eb751dc9386daa59a Mon Sep 17 00:00:00 2001 From: Dustin Pianalto Date: Thu, 6 Mar 2025 15:33:14 -0900 Subject: [PATCH] set mime type --- web/server.go | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/web/server.go b/web/server.go index 8348778..4a3f364 100644 --- a/web/server.go +++ b/web/server.go @@ -6,7 +6,9 @@ import ( "encoding/json" "fmt" "io/fs" + "log" "net/http" + "strings" "sync" "time" @@ -54,25 +56,43 @@ func NewServer(clock *derby.DerbyClock, events <-chan derby.Event, port int) (*S return s, nil } -// routes sets up the HTTP routes +// routes sets up the routes for the server func (s *Server) routes() { // Middleware s.router.Use(middleware.Logger) s.router.Use(middleware.Recoverer) - s.router.Use(middleware.Timeout(10 * time.Second)) - // Static files - staticFS, _ := fs.Sub(content, "static") - s.router.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS)))) + // Create a file server for static files + staticFS, err := fs.Sub(content, "static") + if err != nil { + log.Fatal(err) + } - // Pages - s.router.Get("/", s.handleIndex()) + // Set up static file server with proper MIME types + fileServer := http.FileServer(http.FS(staticFS)) + s.router.Get("/static/*", func(w http.ResponseWriter, r *http.Request) { + // Set correct MIME types based on file extension + path := r.URL.Path + if strings.HasSuffix(path, ".js") { + w.Header().Set("Content-Type", "application/javascript") + } else if strings.HasSuffix(path, ".css") { + w.Header().Set("Content-Type", "text/css") + } - // API endpoints - s.router.Post("/api/reset", s.handleReset()) - s.router.Post("/api/force-end", s.handleForceEnd()) - s.router.Get("/api/status", s.handleStatus()) - s.router.Get("/api/events", s.handleEvents()) + // Strip /static/ prefix before serving + r.URL.Path = strings.TrimPrefix(r.URL.Path, "/static") + fileServer.ServeHTTP(w, r) + }) + + // API routes + s.router.Route("/api", func(r chi.Router) { + r.Post("/reset", s.handleReset()) + r.Post("/force-end", s.handleForceEnd()) + r.Get("/events", s.handleEvents()) + }) + + // Main page + s.router.Get("/", s.handleIndex()) } // Start starts the web server