Use embedded filesystems properly.
This commit is contained in:
@ -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",
|
||||
|
Reference in New Issue
Block a user