-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
360 lines (308 loc) · 8.94 KB
/
server.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package webthing
import (
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"strconv"
"strings"
)
// ThingServer Web Thing Server.
type ThingServer struct {
*http.Server
Things []*Thing
Name string
BasePath string
}
// NewWebThingServer Initialize the WebThingServer.
//
// @param thingType List of Things managed by this server
// @param basePath Base URL path to use, rather than '/'
//
func NewWebThingServer(thingType ThingsType, httpServer *http.Server, basePath string) *ThingServer {
server := &ThingServer{httpServer, thingType.Things(), thingType.Name(), basePath}
thingsNum := len(server.Things)
thingsHandle := &ThingsHandle{server.Things, basePath}
http.HandleFunc("/", thingsHandle.Handle)
if thingsNum == 1 {
thing := server.Things[0]
prePath := strings.TrimRight(server.BasePath+"/"+thing.Title(), "/")
preIdx := strings.TrimRight(server.BasePath, "/")
thing.SetHrefPrefix(preIdx)
thingHandle := &ThingHandle{thing}
propertiesHandle := &PropertiesHandle{thingHandle}
actionsHandle := &ActionsHandle{thingHandle}
eventsHandle := &EventsHandle{thingHandle}
handlerfuncs(prePath, preIdx, thingHandle, propertiesHandle, actionsHandle, eventsHandle)
return server
}
for id, thing := range server.Things {
prePath := strings.TrimRight(server.BasePath+"/"+thing.Title(), "/")
preIdx := "/" + strconv.Itoa(id)
thing.SetHrefPrefix(preIdx)
thingHandle := &ThingHandle{thing}
propertiesHandle := &PropertiesHandle{thingHandle}
actionsHandle := &ActionsHandle{thingHandle}
eventsHandle := &EventsHandle{thingHandle}
handlerfuncs(prePath, preIdx, thingHandle, propertiesHandle, actionsHandle, eventsHandle)
}
return server
}
func handlerfuncs(prePath, preIdx string,
thingHandle *ThingHandle,
propertiesHandle *PropertiesHandle,
actionsHandle *ActionsHandle,
eventsHandle *EventsHandle,
) {
http.HandleFunc(prePath, thingHandle.Handle)
http.HandleFunc(prePath+"/properties", propertiesHandle.Handle)
http.HandleFunc(prePath+"/properties/", propertiesHandle.Handle)
http.HandleFunc(prePath+"/actions", actionsHandle.Handle)
http.HandleFunc(prePath+"/actions/", actionsHandle.Handle)
http.HandleFunc(prePath+"/events", eventsHandle.Handle)
http.HandleFunc(prePath+"/events/", eventsHandle.Handle)
http.HandleFunc(preIdx+"/properties", propertiesHandle.Handle)
http.HandleFunc(preIdx+"/properties/", propertiesHandle.Handle)
http.HandleFunc(preIdx+"/actions", actionsHandle.Handle)
http.HandleFunc(preIdx+"/actions/", actionsHandle.Handle)
http.HandleFunc(preIdx+"/events", eventsHandle.Handle)
http.HandleFunc(preIdx+"/events/", eventsHandle.Handle)
if preIdx != "" {
http.HandleFunc(preIdx, thingHandle.Handle)
}
}
// Start Start listening for incoming connections.
//
// @return Error on failure to listen on port
func (server *ThingServer) Start() error {
return server.ListenAndServe()
}
// Stop Stop listening.
func (server *ThingServer) Stop() error {
return server.Close()
}
// ThingsType Container of Things Type
type ThingsType interface {
// Thing Get the thing at the given index.
//
// @param idx Index of thing.
// @return The thing, or null.
Thing(idx int) *Thing
// Things Get the list of things.
//
// @return The list of things.
Things() []*Thing
// Name Get the mDNS server name.
//
// @return The server name.
Name() string
}
// SingleThing A container for a single thing.
type SingleThing struct {
thing *Thing
}
// NewSingleThing Initialize the container.
//
// @param {Object} thing The thing to store
func NewSingleThing(thing *Thing) *SingleThing {
return &SingleThing{thing}
}
// Thing Get the thing at the given index.
func (st *SingleThing) Thing(idx int) *Thing {
return st.thing
}
// Things Get the list of things.
func (st *SingleThing) Things() []*Thing {
return []*Thing{st.thing}
}
// Name Get the mDNS server name.
func (st *SingleThing) Name() string {
return st.thing.title
}
// MultipleThings A container for multiple things.
type MultipleThings struct {
things []*Thing
name string
}
// NewMultipleThings Initialize the container.
//
// @param {Object} things The things to store
// @param {String} name The mDNS server name
func NewMultipleThings(things []*Thing, name string) *MultipleThings {
mt := &MultipleThings{
things: things,
name: name,
}
return mt
}
// Thing Get the thing at the given index.
//
// @param {Number|String} idx The index
func (mt *MultipleThings) Thing(idx int) *Thing {
return mt.things[idx]
}
// Things Get the list of things.
func (mt *MultipleThings) Things() []*Thing {
return mt.things
}
// Name Get the mDNS server name.
func (mt *MultipleThings) Name() string {
return mt.name
}
// // BaseHandler Base handler that is initialized with a list of things.
// type BaseHandler interface {
// Get(w http.ResponseWriter, r *http.Request)
// Post(w http.ResponseWriter, r *http.Request)
// Put(w http.ResponseWriter, r *http.Request)
// Delete(w http.ResponseWriter, r *http.Request)
// }
// GetInterface Implementation of http Get menthod.
type GetInterface interface {
Get(w http.ResponseWriter, r *http.Request)
}
// PostInterface Implementation of http Post menthod.
type PostInterface interface {
Post(w http.ResponseWriter, r *http.Request)
}
// PutInterface Implementation of http Put menthod.
type PutInterface interface {
Put(w http.ResponseWriter, r *http.Request)
}
// DeleteInterface Implementation of http Delete menthod.
type DeleteInterface interface {
Delete(w http.ResponseWriter, r *http.Request)
}
// BaseHandle Base handler that is initialized with a list of things.
// func BaseHandle(h BaseHandler, w http.ResponseWriter, r *http.Request) {
func BaseHandle(h interface{}, w http.ResponseWriter, r *http.Request) {
corsResponse(w)
jsonResponse(w)
switch r.Method {
case http.MethodGet:
if base, ok := h.(GetInterface); ok {
base.Get(w, r)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
return
case http.MethodPost:
if base, ok := h.(PostInterface); ok {
base.Post(w, r)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
return
case http.MethodPut:
if base, ok := h.(PutInterface); ok {
base.Put(w, r)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
return
case http.MethodDelete:
if base, ok := h.(DeleteInterface); ok {
base.Delete(w, r)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
}
// ThingsHandle things struct.
type ThingsHandle struct {
Things []*Thing
basePath string
}
// Handle handle request.
func (h *ThingsHandle) Handle(w http.ResponseWriter, r *http.Request) {
if len(h.Things) == 1 {
thingHandle := &ThingHandle{h.Things[0]}
thingHandle.Handle(w, r)
return
}
BaseHandle(h, w, r)
}
// Get Handle a Get request.
//
// @param {Object} r The request object
// @param {Object} w The response object
func (h *ThingsHandle) Get(w http.ResponseWriter, r *http.Request) {
var things []json.RawMessage
for _, thing := range h.Things {
things = append(things, thing.AsThingDescription())
}
content, _ := json.Marshal(things)
if _, err := w.Write(content); err != nil {
fmt.Println(err)
}
}
// ThingHandle Handle a request to thing.
type ThingHandle struct {
*Thing
}
// Handle a request to /thing.
func (h *ThingHandle) Handle(w http.ResponseWriter, r *http.Request) {
BaseHandle(h, w, r)
}
// Get Handle a Get request.
//
// @param {Object} r The request object
// @param {Object} w The response object
func (h *ThingHandle) Get(w http.ResponseWriter, r *http.Request) {
base := h.Thing.AsThingDescription()
var ls map[string][]Link
json.Unmarshal(base, &ls)
scheme := "ws"
// if r.URL.Scheme != "" {
// scheme = r.URL.Scheme
// }
wsHref := fmt.Sprintf("%s://%s%s", scheme, r.Host, h.Href())
ls["links"] = append(ls["links"], Link{
Rel: "alternate",
Href: filepath.Clean(strings.TrimRight(wsHref+"/"+h.Href(), "/")),
})
var desc map[string]interface{}
if err := json.Unmarshal(base, &desc); err != nil {
fmt.Print(err)
}
desc["links"] = ls["links"]
type securityDefinitions struct {
NosecSc struct {
Scheme string `json:"scheme"`
} `json:"nosec_sc"`
}
sec := &securityDefinitions{}
sec.NosecSc.Scheme = "nosec"
desc["securityDefinitions"] = sec
desc["security"] = "nosec_sc"
re, _ := json.Marshal(desc)
if _, err := w.Write(re); err != nil {
fmt.Println(err)
}
}
// PropertiesHandle Handle a request to /properties.
type PropertiesHandle struct {
*ThingHandle
}
// Handle Handle a request to /properties.
func (h *PropertiesHandle) Handle(w http.ResponseWriter, r *http.Request) {
if name, err := resource(trimSlash(r.RequestURI)); err == nil {
propertyHandle := &PropertyHandle{h, h.properties[name]}
propertyHandle.Handle(w, r)
return
}
BaseHandle(h, w, r)
}
// Get Handle a Get request.
//
// @param {Object} r The request object
// @param {Object} w The response object
func (h *PropertiesHandle) Get(w http.ResponseWriter, r *http.Request) {
content, err := json.Marshal(h.Thing.Properties())
if err != nil {
fmt.Println(err)
}
if _, err := w.Write(content); err != nil {
fmt.Println(err)
}
}