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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user