diff --git a/internal/server/server.go b/internal/server/server.go index ecad02e..e12566c 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -28,6 +28,7 @@ func New(secret string, chartMax int, st *store.Store, templates fs.FS) *Server func (s *Server) Routes() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/", s.handleIndex) + mux.HandleFunc("/logo.png", s.handleLogo) mux.HandleFunc("/favicon.svg", s.handleFavicon) mux.HandleFunc("/write", s.handleWrite) return mux @@ -70,6 +71,22 @@ func (s *Server) handleFavicon(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(faviconSVG)) } +func (s *Server) handleLogo(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/logo.png" { + http.NotFound(w, r) + return + } + + data, err := fs.ReadFile(s.templates, "static/logo.png") + if err != nil { + http.Error(w, "asset missing", http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "image/png") + _, _ = w.Write(data) +} + func (s *Server) handleWrite(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) diff --git a/main_test.go b/main_test.go index d3511bd..eba0685 100644 --- a/main_test.go +++ b/main_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" "os" + "image/png" "strings" "testing" "time" @@ -127,3 +128,21 @@ func TestBuildHistoryChart(t *testing.T) { } } } + +func TestLogoServedAsPng(t *testing.T) { + srv := server.New("secret", 0, &store.Store{}, embeddedStatic) + req := httptest.NewRequest(http.MethodGet, "/logo.png", nil) + rec := httptest.NewRecorder() + + srv.Routes().ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK) + } + if got := rec.Header().Get("Content-Type"); got != "image/png" { + t.Fatalf("content-type = %q, want image/png", got) + } + if _, err := png.Decode(bytes.NewReader(rec.Body.Bytes())); err != nil { + t.Fatalf("logo is not a valid png: %v", err) + } +} diff --git a/static/index.html b/static/index.html index fc9267d..54a352a 100644 --- a/static/index.html +++ b/static/index.html @@ -14,10 +14,12 @@ .updated-at { margin-top: 0.5rem; font-size: 0.875rem; color: #64748b; } .chart { margin-top: 1.25rem; width: min(92vw, 42rem); } .chart-svg { display: block; width: 100%; height: auto; } + .logo { display: block; width: min(80vw, 36rem); height: auto; margin: 0 auto 0; }