-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp_resolve.go
171 lines (133 loc) · 3.39 KB
/
app_resolve.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package gimlet
import (
"fmt"
"net/http"
"strings"
"github.com/evergreen-ci/negroni"
"github.com/gorilla/mux"
"github.com/mongodb/grip"
)
// Handler returns a handler interface for integration with other
// server frameworks.
func (a *APIApp) Handler() (http.Handler, error) {
return a.getNegroni()
}
// Resolve processes the data in an application instance, including
// all routes and creats a mux.Router object for the application
// instance.
func (a *APIApp) Resolve() error {
if a.isResolved {
return nil
}
if a.router == nil {
a.router = mux.NewRouter().StrictSlash(a.StrictSlash).UseEncodedPath()
}
if err := a.attachRoutes(a.router, true); err != nil {
return err
}
a.isResolved = true
return nil
}
// getHander internal helper resolves the negorni middleware for the
// application and returns it in the form of a http.Handler for use in
// stitching together applications.
func (a *APIApp) getNegroni() (*negroni.Negroni, error) {
if err := a.Resolve(); err != nil {
return nil, err
}
n := negroni.New()
for _, m := range a.middleware {
n.Use(m)
}
n.UseHandler(a.router)
return n, nil
}
func (a *APIApp) attachRoutes(router *mux.Router, addAppPrefix bool) error {
router.StrictSlash(a.StrictSlash)
catcher := grip.NewCatcher()
for _, route := range a.routes {
if !route.IsValid() {
catcher.Add(fmt.Errorf("%s is not a valid route, skipping", route))
continue
}
var methods []string
for _, m := range route.methods {
methods = append(methods, strings.ToLower(m.String()))
}
routeString := ""
if route.version >= 0 {
routeString = route.resolveVersionedRoute(a, addAppPrefix)
} else if a.NoVersions {
routeString = route.resolveLegacyRoute(a, addAppPrefix)
} else {
catcher.Add(fmt.Errorf("skipping '%s', because of versioning error", route))
continue
}
handler := route.getHandlerWithMiddlware(a.wrappers)
if route.isPrefix {
router.PathPrefix(routeString).Handler(handler).Methods(methods...)
} else {
router.Handle(routeString, handler).Methods(methods...)
}
}
return catcher.Resolve()
}
func (r *APIRoute) getRoutePrefix(app *APIApp, addAppPrefix bool) string {
if !addAppPrefix {
return ""
}
if r.overrideAppPrefix && r.prefix != "" {
return r.prefix
}
return app.prefix
}
func (r *APIRoute) resolveLegacyRoute(app *APIApp, addAppPrefix bool) string {
var output string
prefix := r.getRoutePrefix(app, addAppPrefix)
if prefix != "" {
output += prefix
}
if r.prefix != prefix && r.prefix != "" {
output += r.prefix
}
output += r.route
return output
}
func (r *APIRoute) getVersionPart(app *APIApp) string {
var versionPrefix string
if !app.SimpleVersions {
versionPrefix = "v"
}
return fmt.Sprintf("/%s%d", versionPrefix, r.version)
}
func (r *APIRoute) resolveVersionedRoute(app *APIApp, addAppPrefix bool) string {
var (
output string
route string
)
route = r.route
firstPrefix := r.getRoutePrefix(app, addAppPrefix)
if firstPrefix != "" {
output += firstPrefix
}
output += r.getVersionPart(app)
if r.prefix != firstPrefix && r.prefix != "" {
output += r.prefix
}
output += route
return output
}
func (r *APIRoute) getHandlerWithMiddlware(mws []Middleware) http.Handler {
if len(mws) == 0 && len(r.wrappers) == 0 {
return r.handler
}
n := negroni.New()
for _, m := range mws {
n.Use(m)
}
for _, m := range r.wrappers {
n.Use(m)
}
n.UseHandler(r.handler)
return n
}