diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-01 22:29:11 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-01 22:29:11 -0400 |
| commit | ac8fdf42da0c607686e76c68840f38cfd21f83a5 (patch) | |
| tree | 11b1ebf4527bb1a563db15380827d518ae3a2b7c | |
| parent | b533c08f92ae4dcfe3fb7e7bffc4eda796918382 (diff) | |
| download | soen422-ac8fdf42da0c607686e76c68840f38cfd21f83a5.zip | |
close channels
| -rw-r--r-- | server.go | 21 |
1 files changed, 14 insertions, 7 deletions
@@ -9,15 +9,19 @@ import ( const addr = ":9090" -type humidityHandler struct { +type HumidityHandler struct { humidity Record[float32] } -func newHumidityHandler() humidityHandler { - return humidityHandler{newRecord[float32]()} +func newHumidityHandler() HumidityHandler { + return HumidityHandler{newRecord[float32]()} } -func (h humidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (h HumidityHandler) Close() { + h.humidity.Close() +} + +func (h HumidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Println(r.Method, r.URL) switch r.Method { case http.MethodGet: @@ -30,7 +34,7 @@ func (h humidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } -func (h humidityHandler) get(w http.ResponseWriter, r *http.Request) { +func (h HumidityHandler) get(w http.ResponseWriter, r *http.Request) { c := make(chan float32) h.humidity.getRecent <- c humidity, ok := <-c @@ -42,7 +46,7 @@ func (h humidityHandler) get(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%.2f", humidity) } -func (h humidityHandler) post(w http.ResponseWriter, r *http.Request) { +func (h HumidityHandler) post(w http.ResponseWriter, r *http.Request) { query := r.URL.RawQuery humidity, err := strconv.ParseFloat(query, 32) if err != nil { @@ -54,7 +58,10 @@ func (h humidityHandler) post(w http.ResponseWriter, r *http.Request) { } func main() { - http.Handle("/humidity", newHumidityHandler()) + humidityHandler := newHumidityHandler() + defer humidityHandler.Close() + + http.Handle("/humidity", humidityHandler) fmt.Printf("Listening on %s...\n", addr) log.Fatal(http.ListenAndServe(addr, nil)) } |