-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
35 lines (30 loc) · 980 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
package main
import "github.com/julienschmidt/httprouter"
// Route represents single API route and stores its handler function.
type Route struct {
Name string
Method string
Path string
HandlerFunc httprouter.Handle
}
// Routes groups API routes for passing them between functions.
type Routes []Route
// AllRoutes defines all supported API routes.
func AllRoutes() Routes {
routes := Routes{
Route{"Index", "GET", "/", Index},
Route{"RelayIndex", "GET", "/relays", RelayIndex},
Route{"RelayShow", "GET", "/relays/:id", RelayShow},
Route{"RelayOn", "GET", "/relays/:id/on", RelayOn},
Route{"RelayOff", "GET", "/relays/:id/off", RelayOff},
}
return routes
}
// NewRouter reads from the routes slice to set the values for httprouter.Handle.
func NewRouter(routes Routes) *httprouter.Router {
router := httprouter.New()
for _, route := range routes {
router.Handle(route.Method, route.Path, Logger(route.HandlerFunc))
}
return router
}