-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
37 lines (34 loc) · 831 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"net/http"
"encoding/json"
"fmt"
)
func main() {
http.HandleFunc("/iot", httpHandler)
http.ListenAndServe(":8080", nil)
}
func httpHandler(w http.ResponseWriter, req *http.Request) {
var err error
resp := map[string]interface{}{}
if req.Method == "POST" {
params := map[string]interface{}{}
err = json.NewDecoder(req.Body).Decode(¶ms)
if err != nil {
fmt.Fprintf(w, err.Error())
}
resp, err = createPoint(params)
}
if req.Method == "GET" {
resp, err = getPoints()
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err != nil {
fmt.Println(err.Error())
} else {
if err := enc.Encode(resp); err != nil {
fmt.Println(err.Error())
}
}
}