-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
212 lines (163 loc) · 4.97 KB
/
router.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
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
"time"
coap "github.com/coapcloud/go-coap"
"github.com/derekparker/trie"
)
// Router Root-Level Router
type Router struct {
*trie.Trie
*sync.RWMutex
}
// NewRouter ...
func NewRouter() Router {
return Router{
trie.New(),
&sync.RWMutex{},
}
}
// ServeCOAP - implementation of coap.Handler
func (r Router) ServeCOAP(w coap.ResponseWriter, req *coap.Request) {
var (
respBdy string
err error
)
log.Printf("Got message: %#v path=%q: from %v\n", req.Msg.PathString(), req.Msg, req.Client.RemoteAddr())
funcID, ok := r.match(req.Msg.Code(), req.Msg.PathString())
if !ok {
log.Println("could not match route")
w.SetCode(coap.NotFound)
respBdy = fmt.Sprintf("not found")
}
// run openfaas function for route + verb
respBdy, err = openfaasCall(funcID, req.Msg.Payload())
if err != nil {
log.Printf("Error while trying to invoke openfaas function %v\n", err)
w.SetCode(coap.InternalServerError)
respBdy = fmt.Sprint("could not run callback for request")
}
ctx, cancel := context.WithTimeout(req.Ctx, 3*time.Second)
defer cancel()
log.Printf("Writing response to %v\n\n", req.Client.RemoteAddr())
w.SetContentFormat(coap.TextPlain)
if _, err := w.WriteWithContext(ctx, []byte(respBdy)); err != nil {
log.Printf("Cannot send response: %v", err)
}
}
func openfaasCall(funcID string, bdy []byte) (string, error) {
resp, err := http.Post(fmt.Sprintf("%s/function/%s", openfaasAddress, funcID), "application/octet-stream", bytes.NewBuffer(bdy))
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
output := string(b)
return fmt.Sprintf("Invoked func: %s with input: %v. Result: %s", funcID, string(bdy), output), nil
}
func (r *Router) registerRoute(verb coap.COAPCode, path, openfaasFuncID string) {
key := routeKey(verb, path)
log.Printf("registering route: %v %v -> openfaas func: %s\n", verb.String(), path, openfaasFuncID)
node := r.Add(key, openfaasFuncID)
if node != nil {
log.Printf("registered route: %s /%s to func %q\n", verb.String(), path, openfaasFuncID)
}
}
func (r *Router) HotRegisterRoute(verb coap.COAPCode, path, openfaasFuncID string) {
r.Lock()
defer r.Unlock()
key := routeKey(verb, path)
log.Printf("registering route: %v %v -> openfaas func: %s\n", verb.String(), path, openfaasFuncID)
node := r.Add(key, openfaasFuncID)
if node != nil {
log.Printf("registered route: %s /%s to func %q\n", verb.String(), path, openfaasFuncID)
}
}
func (r *Router) HotModifyRoute(verb coap.COAPCode, path, openfaasFuncID string) error {
r.Lock()
defer r.Unlock()
key := routeKey(verb, path)
_, ok := r.Find(key)
if !ok {
return errors.New("could not find route")
}
log.Printf("deregistering route: %v %v -> openfaas func: %s\n", verb.String(), path, openfaasFuncID)
r.Remove(key)
log.Printf("registering route: %v %v -> openfaas func: %s\n", verb.String(), path, openfaasFuncID)
node := r.Add(key, openfaasFuncID)
if node != nil {
log.Printf("registered route: %s /%s to func %q\n", verb.String(), path, openfaasFuncID)
}
return nil
}
func (r *Router) HotDeRegisterRoute(verb coap.COAPCode, path, openfaasFuncID string) error {
r.Lock()
defer r.Unlock()
key := routeKey(verb, path)
_, ok := r.Find(key)
if !ok {
return errors.New("could not find route")
}
log.Printf("deregistering route: %v %v -> openfaas func: %s\n", verb.String(), path, openfaasFuncID)
r.Remove(key)
return nil
}
func (r *Router) match(verb coap.COAPCode, path string) (string, bool) {
r.RLock()
defer r.RUnlock()
key := routeKey(verb, path)
fmt.Println(key)
node, ok := r.Find(key)
if !ok {
log.Printf("couldn't find openfaas function id for route: %s\n", key)
return "", false
}
meta := node.Meta()
v, ok := meta.(string)
if !ok {
log.Printf("couldn't find string-ey openfaas function id for route: %s\n", key)
return "", false
}
return v, true
}
func routeKey(verb coap.COAPCode, path string) string {
return fmt.Sprintf("%d-%s", verb, path)
}
// GET - register a CoAP GET /{path} to a func callback
func (r *Router) GET(path, openfaasFuncID string) {
if r != nil {
r.registerRoute(coap.GET, path, openfaasFuncID)
}
log.Println("can't register route to nil router")
}
// POST - register a CoAP POST /{path} to a func callback
func (r *Router) POST(path, openfaasFuncID string) {
if r != nil {
r.registerRoute(coap.POST, path, openfaasFuncID)
}
log.Println("can't register route to nil router")
}
// PUT - register a CoAP PUT /{path} to a func callback
func (r *Router) PUT(path, openfaasFuncID string) {
if r != nil {
r.registerRoute(coap.PUT, path, openfaasFuncID)
}
log.Println("can't register route to nil router")
}
// DELETE - register a CoAP DELETE /{path} to a func callback
func (r *Router) DELETE(path, openfaasFuncID string) {
if r != nil {
r.registerRoute(coap.DELETE, path, openfaasFuncID)
}
log.Println("can't register route to nil router")
}