-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
91 lines (74 loc) · 2.51 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
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
package docgen
import (
"net/http"
"github.com/gorilla/mux"
)
type HTTPHandler func(w http.ResponseWriter, r *http.Request) // TODO : add di.Container
func (HTTPHandler) ServeHTTP(http.ResponseWriter, *http.Request) {
}
type Info struct {
Name string
Version string
}
type Router struct {
Routes []Route
BaseUrl string
DefaultResps map[int]interface{}
}
type Route struct {
Path string
Method string
Input interface{} // No need to map by method because of the line above ^^
Resps map[int]interface{} // outputs per return code
Handler http.HandlerFunc
Internal bool
Deprecated bool
}
func InitApi(name, version string) *Router {
r := new(Router)
r.Routes = make([]Route, 30)
return r
}
func NewRouter() *Router {
r := new(Router)
return r
}
func (r *Router) NewRoute(path, meth string) *Router {
r.Routes = append(r.Routes, Route{
Path: path,
Method: meth,
})
return r
}
func CreateHTTPHandler(h HTTPHandler, input interface{}, out200 interface{}) {
// Among other stuff, return the function like we already do, but this time, calling the handler function passing the parameters needed
}
// Router as param or as caller ? -> Param to keep the function calling pattern
// Pass "app" as parameter -> Get the real router and add it to the handlers
func RegisterRoutes(r *Router) (*mux.Router, error) {
router := new(mux.Router)
for _, route := range r.Routes {
if &route != nil && &route.Path != nil && &route.Method != nil {
if &route.Handler != nil {
router.NewRoute().Path(route.Path).Methods(route.Method).Handler(route.Handler) //Add handler
}
}
// Shall we add some error handling here? If an error, then log (the x route couldn't be set and continue ?)
// Or even the route doesn't contain the path or the method or the handler ...
}
return router, nil
}
// Router as param or as caller ? -> Param to keep the function calling pattern
// Pass "app" as parameter -> Get the real router and add it to the handlers
func AttachRoutes(r *Router, muxRouter *mux.Router) (*mux.Router, error) {
for _, route := range r.Routes {
if &route != nil && &route.Path != nil && &route.Method != nil {
if &route.Handler != nil {
muxRouter.NewRoute().Path(route.Path).Methods(route.Method).Handler(route.Handler) //Add handler
}
}
// Shall we add some error handling here? If an error, then log (the x route couldn't be set and continue ?)
// Or even the route doesn't contain the path or the method or the handler ...
}
return muxRouter, nil
}