final tweaks
This commit is contained in:
parent
683a3774c3
commit
df42c35456
2
build
2
build
|
@ -1,2 +1,2 @@
|
|||
#!/bin/sh
|
||||
CGO_ENABLED=0 go build
|
||||
CGO_ENABLED=0 go build -ldflags="-s -w"
|
||||
|
|
35
wemo.go
35
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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user