set mime type

main
DustyP 9 months ago
parent 500eff7402
commit 589c9ebe19

@ -6,7 +6,9 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/fs" "io/fs"
"log"
"net/http" "net/http"
"strings"
"sync" "sync"
"time" "time"
@ -54,25 +56,43 @@ func NewServer(clock *derby.DerbyClock, events <-chan derby.Event, port int) (*S
return s, nil return s, nil
} }
// routes sets up the HTTP routes // routes sets up the routes for the server
func (s *Server) routes() { func (s *Server) routes() {
// Middleware // Middleware
s.router.Use(middleware.Logger) s.router.Use(middleware.Logger)
s.router.Use(middleware.Recoverer) s.router.Use(middleware.Recoverer)
s.router.Use(middleware.Timeout(10 * time.Second))
// Static files // Create a file server for static files
staticFS, _ := fs.Sub(content, "static") staticFS, err := fs.Sub(content, "static")
s.router.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS)))) if err != nil {
log.Fatal(err)
}
// Pages // Set up static file server with proper MIME types
s.router.Get("/", s.handleIndex()) 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 // Strip /static/ prefix before serving
s.router.Post("/api/reset", s.handleReset()) r.URL.Path = strings.TrimPrefix(r.URL.Path, "/static")
s.router.Post("/api/force-end", s.handleForceEnd()) fileServer.ServeHTTP(w, r)
s.router.Get("/api/status", s.handleStatus()) })
s.router.Get("/api/events", s.handleEvents())
// 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 // Start starts the web server

Loading…
Cancel
Save