-
Notifications
You must be signed in to change notification settings - Fork 14
/
servlet.go
67 lines (57 loc) · 1.83 KB
/
servlet.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
package srvutil
import (
"net/http"
"github.com/gorilla/mux"
)
// Servlet represents a component that can handle HTTP requests
// It it responsible for registering on the Router.
//
// Simple Servlets can be created with FuncServlet or HandlerServlet,
// but a common pattern is to have a complex struct that will initialize dependencies.
//
// Servlets can also be modified and modified using PrefixServlet, UseServlet, and CombineServlets.
//
// See examples.
type Servlet interface {
RegisterRouting(r *mux.Router)
}
type InlineServlet func(r *mux.Router)
func (m InlineServlet) RegisterRouting(r *mux.Router) {
m(r)
}
// FuncServlet creates a Servlet from a function.
func FuncServlet(path string, handler http.HandlerFunc) Servlet {
return InlineServlet(func(r *mux.Router) {
r.HandleFunc(path, handler)
})
}
// HandlerServlet creates a Servlet from a Handler, which implements ServeHTTP.
func HandlerServlet(path string, handler http.Handler) Servlet {
return InlineServlet(func(r *mux.Router) {
r.Handle(path, handler)
})
}
// PrefixServlet registers a whole Servlet under a path prefix.
func PrefixServlet(s Servlet, path string) Servlet {
return InlineServlet(func(r *mux.Router) {
s.RegisterRouting(r.PathPrefix(path).Subrouter())
})
}
// UseServlet applies a middleware to a whole Servlet.
// Great for applying authentication layers.
func UseServlet(s Servlet, mwf ...mux.MiddlewareFunc) Servlet {
return InlineServlet(func(r *mux.Router) {
r = r.NewRoute().Subrouter()
r.Use(mwf...)
s.RegisterRouting(r)
})
}
// CombineServlets combines all Servlets into one, without other modifications.
// Great for splitting the dependency managements into smaller servlets.
func CombineServlets(servlets ...Servlet) Servlet {
return InlineServlet(func(r *mux.Router) {
for _, s := range servlets {
s.RegisterRouting(r)
}
})
}