You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
914 B
30 lines
914 B
// Package web serves the laundry status API and embedded phone-first page.
|
|
package web
|
|
|
|
import (
|
|
_ "embed"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"mqtt_washingmachine/internal/status"
|
|
)
|
|
|
|
//go:embed static/index.html
|
|
var indexHTML []byte
|
|
|
|
// NewHandler fetches a fresh snapshot for each API request. Disabling caching
|
|
// keeps reloads and polling from showing a previous laundry state.
|
|
func NewHandler(snapshot func() status.Result) http.Handler {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
w.Write(indexHTML)
|
|
})
|
|
mux.HandleFunc("GET /api/status", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
json.NewEncoder(w).Encode(snapshot())
|
|
})
|
|
return mux
|
|
}
|
|
|