logo
This commit is contained in:
1 parent
17baba1490
commit
4fcefd7fb7
4 files changed
+38
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<img class="logo" src="/logo.png" alt="Power Watts logo">
|
||||
<div class="value">{{.Watts}} W</div>
|
||||
<div class="updated-at">Last updated: {{.UpdatedAt}}</div>
|
||||
<div class="chart">{{.HistoryChart}}</div>
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 61 KiB |
Reference in new issue
Block a user