summaryrefslogtreecommitdiffstats
path: root/server/server.go
blob: 217801a8966a5035afe4f1d1bc26629c02988cfb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main

import (
	"fmt"
	"github.com/sam-rba/share"
	"log"
	"net/http"
)

const (
	addr                  = ":9090"
	historySize           = 100_000 // Number of old samples to keep in memory.
	targetHumidityDefault = 35.0
)

var roomIDs = []RoomID{
	"SNbeEcs7XVWMEvjeEYgwZnp9XYjToVhh",
	"rEKKa5TW5xjArmR25wT4Uiw7tksk4noE",
}

type RoomID string

func main() {
	target := share.NewVal[Humidity]()
	target.Set <- targetHumidityDefault
	building := newBuilding(roomIDs)
	dutyCycle := newRecord[DutyCycle](historySize)
	defer target.Close()
	defer building.Close()
	defer dutyCycle.Close()

	http.Handle("/", DashboardHandler{target, building, dutyCycle})
	http.Handle("/humidity", HumidityHandler{building})
	http.Handle("/target_humidity", TargetHumidityHandler{target})
	http.Handle("/duty_cycle", DutyCycleHandler{dutyCycle})
	http.Handle("/humidity_chart.png", HumidityChartHandler{building})
	http.Handle("/duty_cycle_chart.png", DutyCycleChartHandler{dutyCycle})

	fmt.Printf("Listening on %s...\n", addr)
	log.Fatal(http.ListenAndServe(addr, nil))
}

func badRequest(w http.ResponseWriter, format string, a ...any) {
	log.Println("Warning: bad request:", fmt.Sprintf(format, a...))
	w.WriteHeader(http.StatusBadRequest)
	fmt.Fprintf(w, format, a...)
}