diff options
author | xengineering <me@xengineering.eu> | 2024-03-25 20:06:08 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-03-25 20:06:08 +0100 |
commit | 82b6721e5fe7e626c6c731e408be9c989caf370c (patch) | |
tree | 476e5213412475164efd94510bfcbfbd87941e42 /main.go | |
parent | 7ef343e871c0053534eab74763c4a48346669976 (diff) | |
download | webiot-82b6721e5fe7e626c6c731e408be9c989caf370c.tar webiot-82b6721e5fe7e626c6c731e408be9c989caf370c.tar.zst webiot-82b6721e5fe7e626c6c731e408be9c989caf370c.zip |
Define /api handler directly
Using a function returning a closure does not really make sense here.
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 34 |
1 files changed, 15 insertions, 19 deletions
@@ -16,7 +16,7 @@ func main() { configPath := parseFlags() c := parseConfig(configPath) http.HandleFunc("/", index(c.Devices)) - http.HandleFunc("/api", api()) + http.HandleFunc("/api", api) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(static)))) fmt.Printf("Serving at http://%s\n", c.Web.Listen) @@ -51,26 +51,22 @@ func index(devices DevicesConfig) func(http.ResponseWriter, *http.Request) { } } -// api() returns the HTTP handler for the API endpoint. -func api() func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - - // TODO assert correct HTTP method +func api(w http.ResponseWriter, r *http.Request) { + // TODO assert correct HTTP method - errHost, host := assertSingleParam(r, "host") - errState, state := assertSingleParam(r, "state") - if (errHost != nil) || (errState != nil) { - http.Error(w, - "Provide exactly one host and one state parameter!", 400) - return - } + errHost, host := assertSingleParam(r, "host") + errState, state := assertSingleParam(r, "state") + if (errHost != nil) || (errState != nil) { + http.Error(w, + "Provide exactly one host and one state parameter!", 400) + return + } - err := set(host, state) - if err != nil { - http.Error(w, "Could not set WiFi plug.", 500) - } else { - fmt.Fprint(w, "ok") - } + err := set(host, state) + if err != nil { + http.Error(w, "Could not set WiFi plug.", 500) + } else { + fmt.Fprint(w, "ok") } } |