From cb666b494619c19bc1f82ceb37f803b935480e96 Mon Sep 17 00:00:00 2001 From: nic Date: Tue, 15 Oct 2024 18:16:01 -0400 Subject: [PATCH] original static serve for css didn't work with embed, so it has been updated --- apps/web/main.go | 9 ++++++++- web_templates.go | 13 +++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/apps/web/main.go b/apps/web/main.go index 4b0e855..aefd84e 100644 --- a/apps/web/main.go +++ b/apps/web/main.go @@ -18,8 +18,15 @@ func main() { } r := mux.NewRouter() + // Serve embedded static files + staticFS, err := root.GetStaticFS() + if err != nil { + log.Fatalf("Failed to get static filesystem: %v", err) + } + r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(staticFS)))) + // Serve static files - r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + // r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) // Auth routes r.HandleFunc("/login", web.LoginHandler).Methods("GET", "POST") diff --git a/web_templates.go b/web_templates.go index d549b98..9540002 100644 --- a/web_templates.go +++ b/web_templates.go @@ -8,8 +8,8 @@ import ( "path/filepath" ) -//go:embed templates -var webTemplateFS embed.FS +//go:embed templates static/* +var webAssetsFS embed.FS var WebTemplates *template.Template @@ -23,7 +23,7 @@ func InitializeWebTemplates() error { func parseWebTemplates() (*template.Template, error) { tmpl := template.New("") - err := fs.WalkDir(webTemplateFS, "templates", func(path string, d fs.DirEntry, err error) error { + err := fs.WalkDir(webAssetsFS, "templates", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } @@ -38,7 +38,7 @@ func parseWebTemplates() (*template.Template, error) { log.Printf("Parsing template: %s", path) - content, err := webTemplateFS.ReadFile(path) + content, err := webAssetsFS.ReadFile(path) if err != nil { return err } @@ -53,3 +53,8 @@ func parseWebTemplates() (*template.Template, error) { return tmpl, nil } + +// GetStaticFS provides access to the static assets filesystem for serving CSS and other static files +func GetStaticFS() (fs.FS, error) { + return fs.Sub(webAssetsFS, "static") +}