-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
60 lines (48 loc) · 1.08 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
60
package geh
import (
"net/http"
"path/filepath"
"runtime"
"strings"
"github.com/gorilla/mux"
)
var endpointDocs DocApp
type GEHRouter struct {
muxRouter *mux.Router
}
func (router *GEHRouter) GetMuxRouter() *mux.Router {
return router.muxRouter
}
func (router *GEHRouter) Handle(
method string,
endpoint string,
handler func(http.ResponseWriter, *http.Request),
docs DocEndpoint,
) *mux.Route {
addEndpointToDocs(strings.ToLower(method), endpoint, docs)
return router.muxRouter.
HandleFunc(endpoint, handler).
Methods(method)
}
func NewRouter(
title string, version string,
) *GEHRouter {
endpointDocs = NewDocApp(title, version)
muxRouter := mux.NewRouter()
muxRouter.HandleFunc(
"/docs",
Handler(docsHandler),
).Methods(http.MethodGet)
muxRouter.HandleFunc(
"/docs/swagger.json",
Handler(docsSwaggerHandler),
).Methods(http.MethodGet)
_, filename, _, _ := runtime.Caller(0)
currentDirectory := filepath.Dir(filename)
muxRouter.PathPrefix("/assets/").Handler(
http.FileServer(http.Dir(currentDirectory)),
)
return &GEHRouter{
muxRouter: muxRouter,
}
}