-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
112 lines (98 loc) · 2.79 KB
/
server.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"embed"
"encoding/json"
"log"
"net/http"
"time"
)
//go:embed main.html style.css casper.woff favicon.ico
var appFs embed.FS
type Handler struct {
read func() Reading
}
func NewHandler(read func() Reading) *Handler {
return &Handler{read: read}
}
func (s *Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Println(err)
}
}()
// Allow access from anywhere
writer.Header().Set("Access-Control-Allow-Origin", "*")
writer.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
writer.Header().Set("Access-Control-Request-Method", "*")
writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Accept")
if request.Method == http.MethodOptions {
writer.Header().Set("Allow", "OPTIONS, GET")
writer.WriteHeader(http.StatusNoContent)
return
} else if request.Method != http.MethodGet {
writer.WriteHeader(http.StatusMethodNotAllowed)
return
}
switch request.URL.Path {
case "", "/":
request.URL.Path = "main.html"
case "config.json", "/config.json":
writer.Header().Set("Content-Type", "application/json")
b, err := json.Marshal(struct {
Version string `json:"version"`
Language Language `json:"language"`
Port string `json:"port"`
}{
Version: version,
Language: language,
Port: serialPort,
})
if err != nil {
log.Printf("Could not marshal JSON: %v", err)
return
}
writer.WriteHeader(http.StatusOK)
if _, err = writer.Write(b); err != nil {
log.Printf("Could not write response: %v", err)
return
}
return
case "data.json", "/data.json":
writer.Header().Set("Content-Type", "application/json")
reading := s.read()
mode := Translations[language][reading.Mode.Translation()]
stale := time.Now().Sub(reading.Received).Seconds() >= 2
b, err := json.Marshal(struct {
Recorded bool `json:"recorded"`
Overload bool `json:"overload"`
Time int64 `json:"time"`
Value float64 `json:"value"`
Needle int `json:"needle"`
Digits int `json:"digits"`
Unit string `json:"unit"`
Polarity string `json:"polarity"`
Mode string `json:"mode"`
}{
Recorded: reading.Valid && reading.Recorded && !stale,
Overload: reading.Overload,
Time: reading.Received.UnixMilli(),
Value: reading.Absolute,
Needle: int(reading.Relative * 100),
Digits: reading.Precision,
Unit: string(reading.Unit),
Polarity: string(reading.Polarity),
Mode: mode,
})
if err != nil {
log.Printf("Could not marshal JSON: %v", err)
return
}
writer.WriteHeader(http.StatusOK)
if _, err = writer.Write(b); err != nil {
log.Printf("Could not write response: %v", err)
return
}
return
}
http.FileServer(http.FS(appFs)).ServeHTTP(writer, request)
}