set mime type

main
DustyP 9 months ago
parent 500eff7402
commit 589c9ebe19

@ -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

Loading…
Cancel
Save