-
Notifications
You must be signed in to change notification settings - Fork 17
/
router.go
46 lines (38 loc) · 862 Bytes
/
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
package api
import (
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
)
type Router struct {
r *mux.Router
notFound http.Handler
}
type RouterArguments struct {
Handler http.HandlerFunc
Path string
PathPrefix string
Method string
}
func NewRouter() *Router {
return &Router{
r: mux.NewRouter(),
}
}
func (router *Router) Handler() http.Handler {
return router.r
}
func (router *Router) NotFoundHandler(h http.Handler) {
router.notFound = h
router.r.NotFoundHandler = h
}
func (router *Router) AddHandler(args RouterArguments) {
r := router.r
var prefix, path string
if args.PathPrefix != "" {
prefix = fmt.Sprintf("/%s", strings.Trim(args.PathPrefix, "/"))
}
path = fmt.Sprintf("/%s", strings.Trim(args.Path, "/"))
r.Methods(args.Method).Path(fmt.Sprintf("%s%s", prefix, path)).HandlerFunc(args.Handler)
}