summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-11-07 18:59:30 -0500
committerSam Anthony <sam@samanthony.xyz>2024-11-07 18:59:30 -0500
commit70e3bd6a7e9d76dcb16cc587ddbcf184c0360fd7 (patch)
tree446d5231e165ebd630a7682f05a9d046cf4b324d
parentb77d978d3774ce444e3514a16a11ea04916584f0 (diff)
downloadsoen422-70e3bd6a7e9d76dcb16cc587ddbcf184c0360fd7.zip
server: move parseQuery() to humidity.go
-rw-r--r--server/humidity.go21
-rw-r--r--server/server.go21
2 files changed, 21 insertions, 21 deletions
diff --git a/server/humidity.go b/server/humidity.go
index 079c9bf..3982545 100644
--- a/server/humidity.go
+++ b/server/humidity.go
@@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/http"
+ "net/url"
"strconv"
)
@@ -93,3 +94,23 @@ func (h HumidityHandler) average() (Humidity, bool) {
}
return sum / Humidity(nRooms), true
}
+
+// 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
+}
diff --git a/server/server.go b/server/server.go
index 07b961b..31d53f4 100644
--- a/server/server.go
+++ b/server/server.go
@@ -4,7 +4,6 @@ import (
"fmt"
"log"
"net/http"
- "net/url"
)
const addr = ":9090"
@@ -28,26 +27,6 @@ func main() {
log.Fatal(http.ListenAndServe(addr, nil))
}
-// 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
-}
-
func badRequest(w http.ResponseWriter, format string, a ...any) {
log.Println("Warning: bad request:", fmt.Sprintf(format, a))
w.WriteHeader(http.StatusBadRequest)