summaryrefslogtreecommitdiffstats
path: root/server/server.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-11-04 11:25:06 -0500
committerSam Anthony <sam@samanthony.xyz>2024-11-04 11:25:06 -0500
commit64912a13d01896ed73a88097e4fa1d93a5e45a6a (patch)
tree99c63db475040014606a7ba3b0ea6b88954807f3 /server/server.go
parent6c91921e24e9e6e3518769ca0ba012438e630311 (diff)
downloadsoen422-64912a13d01896ed73a88097e4fa1d93a5e45a6a.zip
server: organize function definitions
Diffstat (limited to 'server/server.go')
-rw-r--r--server/server.go52
1 files changed, 26 insertions, 26 deletions
diff --git a/server/server.go b/server/server.go
index 03383ba..e1fecb8 100644
--- a/server/server.go
+++ b/server/server.go
@@ -22,6 +22,15 @@ type HumidityHandler struct {
rooms map[RoomID]Record[Humidity]
}
+func main() {
+ humidityHandler := newHumidityHandler(rooms)
+ defer humidityHandler.Close()
+
+ http.Handle("/humidity", humidityHandler)
+ fmt.Printf("Listening on %s...\n", addr)
+ log.Fatal(http.ListenAndServe(addr, nil))
+}
+
func newHumidityHandler(rooms []RoomID) HumidityHandler {
h := HumidityHandler{make(map[RoomID]Record[Humidity])}
for _, id := range rooms {
@@ -88,26 +97,6 @@ func (h HumidityHandler) post(w http.ResponseWriter, r *http.Request) {
record.put <- Humidity(humidity)
}
-// Parse the value associated with each key in the query string. Returns a map of
-// keys and values, or error if one of the keys is missing, or if there is no value
-// associated with one of the keys.
-func parseQuery(query string, keys []string) (map[string]string, error) {
- queryVals, err := url.ParseQuery(query)
- if err != nil {
- return nil, err
- }
-
- vals := make(map[string]string)
- for _, key := range keys {
- val := queryVals.Get(key)
- if val == "" {
- return nil, fmt.Errorf("missing key '%s'", key)
- }
- vals[key] = val
- }
- return vals, nil
-}
-
// Calculate the average humidity in the building. Returns false if there is not enough data available.
func (h HumidityHandler) average() (Humidity, bool) {
var sum Humidity = 0
@@ -129,11 +118,22 @@ func (h HumidityHandler) average() (Humidity, bool) {
return sum / Humidity(nRooms), true
}
-func main() {
- humidityHandler := newHumidityHandler(rooms)
- defer humidityHandler.Close()
+// Parse the value associated with each key in the query string. Returns a map of
+// keys and values, or error if one of the keys is missing, or if there is no value
+// associated with one of the keys.
+func parseQuery(query string, keys []string) (map[string]string, error) {
+ queryVals, err := url.ParseQuery(query)
+ if err != nil {
+ return nil, err
+ }
- http.Handle("/humidity", humidityHandler)
- fmt.Printf("Listening on %s...\n", addr)
- log.Fatal(http.ListenAndServe(addr, nil))
+ vals := make(map[string]string)
+ for _, key := range keys {
+ val := queryVals.Get(key)
+ if val == "" {
+ return nil, fmt.Errorf("missing key '%s'", key)
+ }
+ vals[key] = val
+ }
+ return vals, nil
}