-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (53 loc) · 1.21 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
package main
import (
"fmt"
"log"
coap "github.com/coapcloud/go-coap"
flag "github.com/spf13/pflag"
)
var (
openfaasAddress = "http://127.0.0.1:31112"
port = flag.IntP("port", "p", 5683, "coap port to listen on")
)
// add flags to register routes too (plumb through to openfaas)
type routeTuple struct {
FuncName string
Verb coap.COAPCode
}
func main() {
flag.StringVarP(&openfaasAddress, "addr", "a", openfaasAddress, "openfaas gateway address")
flag.Parse()
r := NewRouter()
registerRoutes(&r, routes)
mux := coap.NewServeMux()
mux.Handle("*", r)
fmt.Println("starting store sync")
go run(&r)
fmt.Printf("serving CoAP requests on %d\n", *port)
log.Fatal(coap.ListenAndServe("udp", fmt.Sprintf(":%d", *port), mux))
}
var routes = map[string][]routeTuple{
"add": []routeTuple{
routeTuple{
Verb: coap.GET,
FuncName: "sum",
},
routeTuple{
Verb: coap.POST,
FuncName: "add",
},
},
"go-fn": []routeTuple{
routeTuple{
Verb: coap.GET,
FuncName: "go-fn",
},
},
}
func registerRoutes(r *Router, routes map[string][]routeTuple) {
for k, routeTuples := range routes {
for _, v := range routeTuples {
r.registerRoute(v.Verb, k, v.FuncName)
}
}
}