Skip to content

Commit

Permalink
main cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Feb 15, 2018
1 parent 579baee commit 9d57900
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
package main

import (
"net/http"
"os"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/bhoriuchi/go-bunyan/bunyan"
"github.com/gorilla/mux"
"github.com/cjimti/iotwifi/iotwifi"
"github.com/gorilla/mux"
)

type ApiReturn struct {
Status string `json:"status"`
Message string `json:"message"`
Status string `json:"status"`
Message string `json:"message"`
Payload interface{} `json:"payload"`
}

Expand All @@ -34,8 +34,7 @@ func main() {
}

blog.Info("Starting IoT Wifi...")



messages := make(chan iotwifi.CmdMessage, 1)

go iotwifi.RunWifi(blog, messages)
Expand All @@ -44,16 +43,16 @@ func main() {

apiPayloadReturn := func(w http.ResponseWriter, message string, payload interface{}) {
apiReturn := &ApiReturn{
Status: "OK",
Status: "OK",
Message: message,
Payload: payload,
}
ret, _ := json.Marshal(apiReturn)

w.Header().Set("Content-Type", "application/json")
w.Write(ret)
w.Write(ret)
}

// marshallPost populates a struct with json in post body
marshallPost := func(w http.ResponseWriter, r *http.Request, v interface{}) {
bytes, err := ioutil.ReadAll(r.Body)
Expand All @@ -78,13 +77,13 @@ func main() {
// common error return from api
retError := func(w http.ResponseWriter, err error) {
apiReturn := &ApiReturn{
Status: "FAIL",
Status: "FAIL",
Message: err.Error(),
}
ret, _ := json.Marshal(apiReturn)

w.Header().Set("Content-Type", "application/json")
w.Write(ret)
w.Write(ret)
}

// handle /status POSTs json in the form of iotwifi.WpaConnect
Expand Down Expand Up @@ -113,19 +112,19 @@ func main() {
blog.Error(err.Error())
return
}

apiReturn := &ApiReturn{
Status: "OK",
Status: "OK",
Message: "Connection",
Payload: connection,
}

ret, err := json.Marshal(apiReturn)
if err != nil {
retError(w, err)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(ret)
}
Expand All @@ -140,17 +139,17 @@ func main() {
}

apiReturn := &ApiReturn{
Status: "OK",
Status: "OK",
Message: "Networks",
Payload: wpaNetworks,
}

ret, err := json.Marshal(apiReturn)
if err != nil {
retError(w, err)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(ret)
}
Expand All @@ -160,15 +159,15 @@ func main() {
messages <- iotwifi.CmdMessage{Id: "kill"}

apiReturn := &ApiReturn{
Status: "OK",
Status: "OK",
Message: "Killing service.",
}
ret, err := json.Marshal(apiReturn)
if err != nil {
retError(w, err)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(ret)
}
Expand Down

0 comments on commit 9d57900

Please sign in to comment.