This commit is contained in:
flo_isea committed 2026-04-20 20:11:43 +02:00
1 parent 17baba1490
commit 4fcefd7fb7
4 files changed
+38

No files matched your search

+17
View File
@@ -28,6 +28,7 @@ func New(secret string, chartMax int, st *store.Store, templates fs.FS) *Server
func (s *Server) Routes() http.Handler { func (s *Server) Routes() http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", s.handleIndex) mux.HandleFunc("/", s.handleIndex)
mux.HandleFunc("/logo.png", s.handleLogo)
mux.HandleFunc("/favicon.svg", s.handleFavicon) mux.HandleFunc("/favicon.svg", s.handleFavicon)
mux.HandleFunc("/write", s.handleWrite) mux.HandleFunc("/write", s.handleWrite)
return mux return mux
@@ -70,6 +71,22 @@ func (s *Server) handleFavicon(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(faviconSVG)) _, _ = 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) { func (s *Server) handleWrite(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed) http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
+19
View File
@@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"image/png"
"strings" "strings"
"testing" "testing"
"time" "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)
}
}
+2
View File
@@ -14,10 +14,12 @@
.updated-at { margin-top: 0.5rem; font-size: 0.875rem; color: #64748b; } .updated-at { margin-top: 0.5rem; font-size: 0.875rem; color: #64748b; }
.chart { margin-top: 1.25rem; width: min(92vw, 42rem); } .chart { margin-top: 1.25rem; width: min(92vw, 42rem); }
.chart-svg { display: block; width: 100%; height: auto; } .chart-svg { display: block; width: 100%; height: auto; }
.logo { display: block; width: min(80vw, 36rem); height: auto; margin: 0 auto 0; }
</style> </style>
</head> </head>
<body> <body>
<main> <main>
<img class="logo" src="/logo.png" alt="Power Watts logo">
<div class="value">{{.Watts}} W</div> <div class="value">{{.Watts}} W</div>
<div class="updated-at">Last updated: {{.UpdatedAt}}</div> <div class="updated-at">Last updated: {{.UpdatedAt}}</div>
<div class="chart">{{.HistoryChart}}</div> <div class="chart">{{.HistoryChart}}</div>
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB