diff --git a/appview/pages/pages.go b/appview/pages/pages.go index aa579bc5..bd316831 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -1436,6 +1436,35 @@ func (p *Pages) Static() http.Handler { return Cache(http.StripPrefix("/static/", http.FileServer(http.FS(sub)))) } +func (p *Pages) StaticFile(path string) http.Handler { + if p.dev { + filePath := filepath.Join("appview/pages/static", path) + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, filePath) + }) + } + sub, err := fs.Sub(p.embedFS, "static") + if err != nil { + p.logger.Error("no static dir found? that's crazy", "err", err) + panic(err) + } + fsys := http.FS(sub) + return Cache(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + f, err := fsys.Open(path) + if err != nil { + http.NotFound(w, r) + return + } + defer f.Close() + stat, err := f.Stat() + if err != nil { + http.NotFound(w, r) + return + } + http.ServeContent(w, r, stat.Name(), stat.ModTime(), f.(io.ReadSeeker)) + })) +} + func Cache(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := strings.Split(r.URL.Path, "?")[0] diff --git a/appview/state/router.go b/appview/state/router.go index cdbda927..5d1d4196 100644 --- a/appview/state/router.go +++ b/appview/state/router.go @@ -32,6 +32,7 @@ func (s *State) Router() http.Handler { s.pages, ) + router.Get("/favicon.ico", s.pages.StaticFile("logos/dolly.ico").ServeHTTP) router.Get("/pwa-manifest.json", s.WebAppManifest) router.Get("/robots.txt", s.RobotsTxt)