updated time
This commit is contained in:
1 parent
fc1673dcff
commit
8ade787421
3 files changed
+33
-7
No files matched your search
@@ -11,6 +11,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
//go:embed static/*
|
||||
@@ -19,6 +20,7 @@ var indexHTML embed.FS
|
||||
type store struct {
|
||||
mu sync.RWMutex
|
||||
value int
|
||||
updatedAt time.Time
|
||||
}
|
||||
|
||||
func (s *store) get() int {
|
||||
@@ -30,9 +32,16 @@ func (s *store) get() int {
|
||||
func (s *store) set(v int) {
|
||||
s.mu.Lock()
|
||||
s.value = v
|
||||
s.updatedAt = time.Now()
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *store) snapshot() (int, time.Time) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.value, s.updatedAt
|
||||
}
|
||||
|
||||
func main() {
|
||||
secret := os.Getenv("WRITE_SECRET")
|
||||
addr := os.Getenv("ADDR")
|
||||
@@ -108,7 +117,12 @@ func (s *server) handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := page.Execute(w, map[string]any{"Watts": s.store.get()}); err != nil {
|
||||
watts, updatedAt := s.store.snapshot()
|
||||
updatedLabel := "not updated yet"
|
||||
if !updatedAt.IsZero() {
|
||||
updatedLabel = updatedAt.Format("02.01.2006 15:04:05")
|
||||
}
|
||||
if err := page.Execute(w, map[string]any{"Watts": watts, "UpdatedAt": updatedLabel}); err != nil {
|
||||
http.Error(w, "render error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
+12
-2
@@ -2,15 +2,16 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHandleIndexRendersWatts(t *testing.T) {
|
||||
srv := &server{secret: "secret", store: &store{value: 123}}
|
||||
srv := &server{secret: "secret", store: &store{value: 123, updatedAt: time.Date(2026, 4, 20, 12, 34, 56, 0, time.UTC)}}
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
@@ -22,6 +23,12 @@ func TestHandleIndexRendersWatts(t *testing.T) {
|
||||
if !strings.Contains(rec.Body.String(), "123") {
|
||||
t.Fatalf("response did not contain watt value: %s", rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "Last updated:") {
|
||||
t.Fatalf("response did not contain last updated label: %s", rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "20.04.2026 12:34:56") {
|
||||
t.Fatalf("response did not contain german datetime: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWriteRequiresSecret(t *testing.T) {
|
||||
@@ -52,6 +59,9 @@ func TestHandleWriteStoresValue(t *testing.T) {
|
||||
if got := srv.store.get(); got != 456 {
|
||||
t.Fatalf("stored watts = %d, want %d", got, 456)
|
||||
}
|
||||
if srv.store.updatedAt.IsZero() {
|
||||
t.Fatal("updatedAt was not set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig(t *testing.T) {
|
||||
|
||||
+6
-4
@@ -11,12 +11,14 @@
|
||||
main { text-align: center; padding: 2rem; }
|
||||
.value { font-size: clamp(3rem, 12vw, 8rem); font-weight: 800; line-height: 1; }
|
||||
.unit { margin-top: 0.75rem; font-size: 1.25rem; color: #94a3b8; }
|
||||
.updated-at { margin-top: 0.5rem; font-size: 0.875rem; color: #64748b; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<div class="value">{{.Watts}}</div>
|
||||
<div class="unit">watts</div>
|
||||
</main>
|
||||
<main>
|
||||
<div class="value">{{.Watts}}</div>
|
||||
<div class="unit">watts</div>
|
||||
<div class="updated-at">Last updated: {{.UpdatedAt}}</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user