-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (166 loc) · 4.47 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net"
"reflect"
"strconv"
"strings"
"github.com/buaazp/fasthttprouter"
"github.com/tbarker25/thermostat-api/internal/temp"
"github.com/tbarker25/thermostat-api/internal/thermostat"
"github.com/valyala/fasthttp"
)
func main() {
addr := flag.String("address", "0.0.0.0:80", "Address to listen on")
flag.Parse()
handler := getHandler()
ln, err := net.Listen("tcp4", *addr)
if err != nil {
log.Fatal(err)
}
fmt.Printf("listening on %s\n", ln.Addr())
err = (&fasthttp.Server{
Handler: handler,
}).Serve(ln)
if err != nil {
log.Fatal(err)
}
}
func getHandler() fasthttp.RequestHandler {
router := fasthttprouter.New()
router.PanicHandler = handlePanic
router.GET("/thermostat", handleThermostatList)
router.GET("/thermostat/:id", handleThermostatGet)
router.PATCH("/thermostat/:id", handleThermostatSet)
return router.Handler
}
func handlePanic(ctx *fasthttp.RequestCtx, err interface{}) {
log.Print(err)
sendError(ctx, fasthttp.StatusInternalServerError, "unexpected error: %+v\n", err)
}
func handleThermostatList(ctx *fasthttp.RequestCtx) {
sendOK(ctx, thermostats)
}
func handleThermostatGet(ctx *fasthttp.RequestCtx) {
id, err := strconv.ParseUint(ctx.UserValue("id").(string), 10, 32)
if err != nil {
sendError(ctx, fasthttp.StatusBadRequest,
"ID field must be integer, got ID='%s'\n",
ctx.UserValue("id").(string))
return
}
thermostat := getThermostatByID(uint32(id))
if thermostat == nil {
sendError(ctx, fasthttp.StatusNotFound,
"No thermostat with ID=%d\n", id)
return
}
sendOK(ctx, thermostat)
}
func handleThermostatSet(ctx *fasthttp.RequestCtx) {
id, err := strconv.ParseUint(ctx.UserValue("id").(string), 10, 32)
if err != nil {
sendError(ctx, fasthttp.StatusBadRequest,
"ID field must be integer, got ID='%s'\n",
ctx.UserValue("id").(string))
return
}
device := getThermostatByID(uint32(id))
if device == nil {
sendError(ctx, fasthttp.StatusNotFound,
"No device with ID=%d\n", id)
return
}
var body struct {
Name *string `json:"name"`
OperatingMode *thermostat.OperatingMode `json:"operatingMode"`
HeatPoint *temp.Temp `json:"heatPoint"`
CoolPoint *temp.Temp `json:"coolPoint"`
FanMode *thermostat.FanMode `json:"fanMode"`
}
err = jsonUnmarshalStrict(ctx.PostBody(), &body)
if err != nil {
sendError(ctx, fasthttp.StatusBadRequest,
"Could not unmarshal body: %v", err)
return
}
if body.Name != nil {
device.SetName(*body.Name)
}
if body.OperatingMode != nil {
device.SetOperatingMode(*body.OperatingMode)
}
if body.HeatPoint != nil {
device.SetHeatPoint(*body.HeatPoint)
}
if body.CoolPoint != nil {
device.SetCoolPoint(*body.CoolPoint)
}
if body.FanMode != nil {
device.SetFanMode(*body.FanMode)
}
sendOK(ctx, device)
}
// jsonUnmarshalStrict is a helper function to unmarshal with added strictness.
// unlike json.Unmarshal, this function fails if the input data has extra
// fields Note that this functionality is going to be incorperated into the
// standard library in Go 1.10.
func jsonUnmarshalStrict(data []byte, v interface{}) error {
tmp := map[string]interface{}{}
err := json.Unmarshal(data, &tmp)
if err != nil {
return err
}
err = json.Unmarshal(data, v)
if err != nil {
return err
}
t := reflect.ValueOf(v).Elem().Type()
var unwantedFields []string
nextField:
for k := range tmp {
for i := 0; i < t.NumField(); i++ {
if strings.EqualFold(k, t.Field(i).Name) {
continue nextField
}
}
unwantedFields = append(unwantedFields, k)
}
if unwantedFields != nil {
return fmt.Errorf("unsupported fields '%s'",
strings.Join(unwantedFields, "', '"))
}
return nil
}
func sendOK(ctx *fasthttp.RequestCtx, v interface{}) {
resp := struct {
Status string `json:"status"`
Data interface{} `json:"data"`
}{
Status: "ok",
Data: v,
}
body, err := json.MarshalIndent(resp, "", " ")
if err != nil {
sendError(ctx, fasthttp.StatusBadRequest, "Could not encode JSON: %v\n", err)
return
}
ctx.Success("application/json", body)
}
func sendError(ctx *fasthttp.RequestCtx, statusCode int, format string, a ...interface{}) {
resp := struct {
Status string `json:"status"`
ErrorMessage string `json:"errorMessage"`
}{
Status: "error",
ErrorMessage: fmt.Sprintf(format, a...),
}
j, err := json.MarshalIndent(&resp, "", " ")
if err != nil {
panic(err)
}
ctx.Error(string(j), statusCode)
}