github.com/troglobit / digit

Serve /favicon.ico

Browsers request /favicon.ico unprompted, and it fell through to the
repo handler as a 404.  Serve the configured icon there.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
Joachim Wiberg · 59 days ago commit ff62e89 · parent 213b268 · child a73851e · patch
diff --git a/internal/web/server.go b/internal/web/server.go
index 5c683d5..4218b1d 100644
--- a/internal/web/server.go
+++ b/internal/web/server.go
@@ -138,6 +138,11 @@ func New(cfg *config.Config, assets fs.FS) (*Server, error) {
 	mux.Handle("GET /robots.txt", cached(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		http.ServeFileFS(w, r, sfs, "robots.txt")
 	})))
+	// Browsers request /favicon.ico unprompted; serve the configured
+	// icon there so it isn't a 404 through the repo handler.
+	mux.Handle("GET /favicon.ico", cached(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		http.ServeFileFS(w, r, sfs, cfg.Meta.Favicon)
+	})))
 	mux.HandleFunc("GET /{$}", s.handle(s.index))
 	mux.HandleFunc("GET /atom", s.handle(func(w http.ResponseWriter, r *http.Request) error {
 		return s.siteFeed(w, r, "atom")