Use embedded filesystems properly.

This commit is contained in:
2025-05-13 22:49:36 -05:00
parent 27eb2e5f12
commit c0bc02fed5
2 changed files with 11 additions and 32 deletions

View File

@ -2,6 +2,7 @@ package server
import (
"context"
"io/fs"
"log/slog"
"net"
"net/http"
@ -87,11 +88,16 @@ func Start(ctx context.Context, cfg Config) error {
BaseContext: func(_ net.Listener) context.Context { return ctx }, // Pass base context to all requests
}
// Serve the templates and static files using embedded content
// Serve templates from the embedded "templates" directory
httpRequestRouter.Handle("/", http.FileServer(http.FS(embeds.Templates)))
// Serve static files from the embedded "static" directory
httpRequestRouter.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(embeds.Static))))
// For embedded templates
templateSubFS, err := fs.Sub(embeds.Templates, "templates")
if err != nil {
cfg.Logger.Error("Failed to create sub filesystem for templates", slog.Any("error", err))
return err
}
httpRequestRouter.Handle("/", http.FileServer(http.FS(templateSubFS)))
// For embedded static files
httpRequestRouter.Handle("/static/", http.FileServer(http.FS(embeds.Static)))
// Log server startup with structured logging
cfg.Logger.Info("starting server",