-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
49 lines (37 loc) · 1002 Bytes
/
routes.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
package main
import (
"net/http"
"github.com/gorilla/mux"
"github.com/ti-mo/comfo/comfoserver"
rpc "github.com/ti-mo/comfo/rpc/comfo"
)
// Route is a gorilla/mux route entry.
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
// NewRouter is the initialization method for a gorilla/mux router.
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
// Add Twirp handler
server := &comfoserver.Server{}
twirpHandler := rpc.NewComfoServer(server, nil)
router.PathPrefix(rpc.ComfoPathPrefix).Handler(Logger(twirpHandler, "twirp"))
// Add Route entries to router
for _, route := range routes {
// Add middleware handlers
handler := Logger(route.HandlerFunc, route.Name)
handler = Headers(handler)
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
return router
}
// Routes is a list of Route items.
type Routes []Route
var routes = Routes{}