diff --git a/build b/build index babd3a5..6e6da2b 100755 --- a/build +++ b/build @@ -1,2 +1,2 @@ #!/bin/sh -CGO_ENABLED=0 go build +CGO_ENABLED=0 go build -ldflags="-s -w" diff --git a/wemo.go b/wemo.go index e356e0f..ffbab45 100644 --- a/wemo.go +++ b/wemo.go @@ -44,7 +44,9 @@ var client http.Client func main() { command = os.Getenv("CMD") - + port := os.Getenv("PORT") + if port == "" { port = ":8081" } else { port = ":" + port } + logger.Infof("listening on port %s", port) if len(os.Args) > 1 { if os.Args[1] == "on" { movieMode(true) @@ -57,17 +59,30 @@ func main() { } // POST /basement { movieMode: true } OR { movieMode: false } http.HandleFunc("/basement", func(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { + var postBody basementPost + switch r.Method { + case "GET": + query := r.URL.Query().Get("moviemode") + switch query { + case "on", "true", "1": + postBody.MovieMode = true + case "off", "false", "0": + postBody.MovieMode = false + default: + http.Error(w, "Missing query string", 400) + return + } + case "POST": + postBodyBytes, err := ioutil.ReadAll(r.Body) + if err != nil { + http.Error(w, "Could not read body", 400) + return + } + json.Unmarshal(postBodyBytes, &postBody) + default: http.Error(w, "Not found", 404) return } - postBodyBytes, err := ioutil.ReadAll(r.Body) - if err != nil { - http.Error(w, "Could not read body", 500) - return - } - var postBody basementPost - json.Unmarshal(postBodyBytes, &postBody) fmt.Fprintf(w, "MovieMode: %t", postBody.MovieMode) movieMode(postBody.MovieMode) }) @@ -76,7 +91,7 @@ func main() { http.Error(w, "Not found", 404) }) - log.Fatal(http.ListenAndServe(":8081", nil)) + log.Fatal(http.ListenAndServe(port, nil)) } func movieMode(desiredState bool) { @@ -122,7 +137,7 @@ func commandDevice(url string, device deviceAction) { logger.Tracef("%s: %d ms per step over %d seconds", device.Device, millisecondsPerTick, device.Seconds) deltaPerTick := (device.End - device.Start) / (device.Steps - 1) logger.Tracef("%s: %d change per command", device.Device, deltaPerTick) - currentValue := device.Start + currentValue := device.Start currentSteps := 0 ticker := time.NewTicker(time.Duration(millisecondsPerTick) * time.Millisecond) quit := make(chan struct{})