-
Notifications
You must be signed in to change notification settings - Fork 225
/
router.go
59 lines (50 loc) · 1.88 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
package server
import (
"net/http"
"github.com/gorilla/mux"
)
// Router is an interface to wrap different router types to be embedded within
// Gizmo server.Server implementations.
type Router interface {
Handle(method string, path string, handler http.Handler)
HandleFunc(method string, path string, handlerFunc func(http.ResponseWriter, *http.Request))
ServeHTTP(w http.ResponseWriter, r *http.Request)
SetNotFoundHandler(handler http.Handler)
}
// NewRouter will return the router specified by the server
// config. If no Router value is supplied, the server
// will default to using Gorilla mux.
func NewRouter(cfg *Config) Router {
switch cfg.RouterType {
case "gorilla":
return &GorillaRouter{mux.NewRouter()}
default:
return &GorillaRouter{mux.NewRouter()}
}
}
// GorillaRouter is a Router implementation for the Gorilla web toolkit's `mux.Router`.
type GorillaRouter struct {
mux *mux.Router
}
// Handle will call the Gorilla web toolkit's Handle().Method() methods.
func (g *GorillaRouter) Handle(method, path string, h http.Handler) {
g.mux.Handle(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// copy the route params into a shared location
// duplicating memory, but allowing Gizmo to be more flexible with
// router implementations.
SetRouteVars(r, mux.Vars(r))
h.ServeHTTP(w, r)
})).Methods(method)
}
// HandleFunc will call the Gorilla web toolkit's HandleFunc().Method() methods.
func (g *GorillaRouter) HandleFunc(method, path string, h func(http.ResponseWriter, *http.Request)) {
g.Handle(method, path, http.HandlerFunc(h))
}
// SetNotFoundHandler will set the Gorilla mux.Router.NotFoundHandler.
func (g *GorillaRouter) SetNotFoundHandler(h http.Handler) {
g.mux.NotFoundHandler = h
}
// ServeHTTP will call Gorilla mux.Router.ServerHTTP directly.
func (g *GorillaRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
g.mux.ServeHTTP(w, r)
}