-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.go
68 lines (61 loc) · 2.12 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"net/http"
"strings"
"github.com/aaparella/vidwell/controllers"
"github.com/gorilla/mux"
)
// Controller defines a web controller that can be registered with a mux
// to serve webpages and endpoints.
type Controller interface {
// Prefix is the root path that all endpoints will be accessible through
// for this controller.
Prefix() string
// Endpoints defines the web pages and APIs that this controller offers.
// The first key is the path for the handler, and the second maps each
// method to the appropriate handler.
//
// map[string]map[string]http.HandlerFunc{
// "/users": {
// "GET": c.ViewUsers,
// },
// "/users/new": {
// "GET": c.NewUserPage,
// "POST": c.MakeNewUser,
// }
// }
Endpoints() map[string]map[string]http.HandlerFunc
}
// RegisterRoutes prepares the router with all routes needed for the entire
// application.
//
// This router will be used in main as the router for http.ListenAndServe.
// All services in the application must register routes here, so that all
// routes can be seen from a single location. This allows route configuration
// to be kept separate from the rest of the application's logic.
//
// Routes can use variable syntax supported by gorilla mux. Variables in the
// path can then be fetched by using mux.Vars(r) in the handler. For example
// the video viewing page:
// mux.Register("/videos/{id}", videos.ViewVideo)
//
// Then in the handler, the value of id can be retrieved:
// keys := mux.Vars(r)
// id, ok := keys["id"]
func RegisterRoutes(router *mux.Router) {
// register adds the routes defined by the controller to the router.
register := func(c Controller) {
registerController(router, c)
}
// Register a controller here in order to register it's endpoints
register(controllers.VideoController{})
register(controllers.UserController{})
}
func registerController(router *mux.Router, c Controller) {
subrouter := router.PathPrefix(c.Prefix()).Subrouter()
for path, handlers := range c.Endpoints() {
for methods, fn := range handlers {
subrouter.HandleFunc(path, fn).Methods(strings.Split(methods, ", ")...)
}
}
}