Browse Source

original static serve for css didn't work with embed, so it has been updated

cli-archive
nic 1 year ago
parent
commit
cb666b4946
  1. 9
      apps/web/main.go
  2. 13
      web_templates.go

9
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")

13
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")
}

Loading…
Cancel
Save