-
Notifications
You must be signed in to change notification settings - Fork 2
/
muxinator.go
125 lines (103 loc) · 4.14 KB
/
muxinator.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package muxinator
import (
"context"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
)
// Middleware replicates the negroni.HandlerFunc type but decouples the code from the library
type Middleware func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
// Router is a wrapper around the gorilla mux router and the negroni middleware library.
// It has some convenience functions to make it easier to do per-route middleware
type Router struct {
n *negroni.Negroni
m *mux.Router
s *http.Server
}
// NewRouter returns a new Router instance with some defaults
func NewRouter() Router {
n := negroni.New()
m := mux.NewRouter().StrictSlash(true)
h := &http.Server{}
return Router{n, m, h}
}
// ListenAndServe builds the final handler and passes it to http.ListenAndServe
func (router *Router) ListenAndServe(addr string) error {
router.s.Addr = addr
router.s.Handler = router.BuildHandler()
return router.s.ListenAndServe()
}
// Shutdown gracefully shuts down the server
func (router *Router) Shutdown(ctx context.Context) error {
return router.s.Shutdown(ctx)
}
// BuildHandler returns an http.Handler that can be used as the argument to http.ListenAndServe.
func (router *Router) BuildHandler() http.Handler {
// The mux router needs to be the last item of middleware added to the negroni instance.
router.n.UseHandler(router.m)
return router.n
}
// Match returns whether a route exists for the given request
func (router *Router) Match(req *http.Request) bool {
match := mux.RouteMatch{}
return router.m.Match(req, &match)
}
// AddMiddleware adds middleware that will be applied to every request.
// Middleware handlers are executed in the order defined.
func (router *Router) AddMiddleware(middlewares ...Middleware) {
for _, middleware := range middlewares {
router.n.UseFunc(middleware)
}
}
// Handle registers a route with the router. Internally, gorilla mux is used.
// See https://github.com/gorilla/mux for options available for the path, including variables.
// End a path with an asterisk "*" to create a wildcard route.
func (router *Router) Handle(method string, path string, handler http.Handler, middlewares ...Middleware) {
// A slice to hold all of the middleware once it's converted (including the handler itself)
var stack []negroni.Handler
// The middleware functions have type Middleware but they need to conform to the negroni.Handler interface.
// By using the negroni.HandlerFunc adapter, they will be given the method required by the interface.
for _, middleware := range middlewares {
stack = append(stack, negroni.HandlerFunc(middleware))
}
// The handler needs to be treated like middleware
stack = append(stack, negroni.Wrap(handler))
// Create a new mux route
route := router.m.NewRoute()
// Set the route's handler
route.Handler(negroni.New(stack...))
// If the last character is an asterisk, create
// a wildcard route using route.PathPrefix().
if strings.HasSuffix(path, "*") {
// Be sure to strip the asterisk off again
route.PathPrefix(path[:len(path)-1])
} else {
// Otherwise just add the path normally
route.Path(path)
}
// Restrict to a particular method if set
if method != "" {
route.Methods(method)
}
}
// Get is a helper function to add a GET route
func (router *Router) Get(path string, handler http.Handler, middlewares ...Middleware) {
router.Handle("GET", path, handler, middlewares...)
}
// Post is a helper function to add a POST route
func (router *Router) Post(path string, handler http.Handler, middlewares ...Middleware) {
router.Handle("POST", path, handler, middlewares...)
}
// Put is a helper function to add a PUT route
func (router *Router) Put(path string, handler http.Handler, middlewares ...Middleware) {
router.Handle("PUT", path, handler, middlewares...)
}
// Patch is a helper function to add a PATCH route
func (router *Router) Patch(path string, handler http.Handler, middlewares ...Middleware) {
router.Handle("PATCH", path, handler, middlewares...)
}
// Delete is a helper function to add a DELETE route
func (router *Router) Delete(path string, handler http.Handler, middlewares ...Middleware) {
router.Handle("DELETE", path, handler, middlewares...)
}