/
app.go
285 lines (240 loc) · 7.77 KB
/
app.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Package gimlet is a toolkit for building JSON/HTTP interfaces (e.g. REST).
//
// Gimlet builds on standard library and common tools for building web
// applciations (e.g. Negroni and gorilla,) and is only concerned with
// JSON/HTTP interfaces, and omits support for aspects of HTTP
// applications outside of the scope of JSON APIs (e.g. templating,
// sessions.) Gimilet attempts to provide minimal convinences on top
// of great infrastucture so that your application can omit
// boilerplate and you don't have to build potentially redundant
// infrastructure.
package gimlet
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/mongodb/grip"
"github.com/mongodb/grip/recovery"
"github.com/phyber/negroni-gzip/gzip"
"github.com/urfave/negroni"
)
// APIApp is a structure representing a single API service.
type APIApp struct {
StrictSlash bool
isResolved bool
prefix string
defaultVersion int
port int
router *mux.Router
address string
subApps []*APIApp
routes []*APIRoute
middleware []negroni.Handler
}
// NewApp returns a pointer to an application instance. These
// instances have reasonable defaults and include middleware to:
// recover from panics in handlers, log information about the request,
// and gzip compress all data. Users must specify a default version
// for new methods.
func NewApp() *APIApp {
a := &APIApp{
StrictSlash: true,
defaultVersion: -1, // this is the same as having no version prepended to the path.
port: 3000,
}
a.AddMiddleware(negroni.NewRecovery())
a.AddMiddleware(NewAppLogger())
a.AddMiddleware(gzip.Gzip(gzip.DefaultCompression))
return a
}
// SetDefaultVersion allows you to specify a default version for the
// application. Default versions must be 0 (no version,) or larger.
func (a *APIApp) SetDefaultVersion(version int) {
if version < 0 {
grip.Warningf("%d is not a valid version", version)
} else {
a.defaultVersion = version
grip.Noticef("Set default api version to /v%d/", version)
}
}
// Router is the getter for an APIApp's router object. If thetr
// application isn't resolved, then the error return value is non-nil.
func (a *APIApp) Router() (*mux.Router, error) {
if a.isResolved {
return a.router, nil
}
return nil, errors.New("application is not resolved")
}
// AddApp allows you to combine App instances, by taking one app and
// add its routes to the current app. Returns a non-nill error value
// if the current app is resolved. If the apps have different default
// versions set, the versions on the second app are explicitly set.
func (a *APIApp) AddApp(app *APIApp) error {
// if we've already resolved then it has to be an error
if a.isResolved {
return errors.New("cannot merge an app into a resolved app")
}
a.subApps = append(a.subApps, app)
return nil
}
// AddMiddleware adds a negroni handler as middleware to the end of
// the current list of middleware handlers.
func (a *APIApp) AddMiddleware(m negroni.Handler) {
a.middleware = append(a.middleware, m)
}
// 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 {
catcher := grip.NewCatcher()
a.router = mux.NewRouter().StrictSlash(a.StrictSlash)
for _, route := range a.routes {
if !route.IsValid() {
catcher.Add(fmt.Errorf("%d is an invalid api version. not adding route for %s",
route.version, route.route))
continue
}
var methods []string
for _, m := range route.methods {
methods = append(methods, strings.ToLower(m.String()))
}
if route.version > 0 {
versionedRoute := getVersionedRoute(a.prefix, route.version, route.route)
a.router.HandleFunc(versionedRoute, route.handler).Methods(methods...)
grip.Debugln("added route for:", versionedRoute)
}
if route.version == a.defaultVersion {
route.route = getDefaultRoute(a.prefix, route.route)
a.router.HandleFunc(route.route, route.handler).Methods(methods...)
grip.Debugln("added route for:", route.route)
}
}
a.isResolved = true
return catcher.Resolve()
}
func getVersionedRoute(prefix string, version int, route string) string {
if strings.HasPrefix(route, prefix) {
if prefix == "" {
return fmt.Sprintf("/v%d%s", version, route)
}
route = route[len(prefix):]
}
return fmt.Sprintf("%s/v%d%s", prefix, version, route)
}
func getDefaultRoute(prefix, route string) string {
if strings.HasPrefix(route, prefix) {
return route
}
return prefix + route
}
// ResetMiddleware removes *all* middleware handlers from the current
// application.
func (a *APIApp) ResetMiddleware() {
a.middleware = []negroni.Handler{}
}
// 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 applicationstr
func (a *APIApp) getNegroni() (*negroni.Negroni, error) {
if err := a.Resolve(); err != nil {
return nil, err
}
catcher := grip.NewCatcher()
n := negroni.New(a.middleware...)
n.UseHandler(a.router)
for _, app := range a.subApps {
if !strings.HasPrefix(app.prefix, a.prefix) {
app.prefix = a.prefix + app.prefix
}
catcher.Add(app.Resolve())
subNegroni, err := app.getNegroni()
if err != nil {
catcher.Add(err)
}
n.UseHandler(subNegroni)
}
if catcher.HasErrors() {
return nil, catcher.Resolve()
}
return n, nil
}
// Handler returns a handler interface for integration with other
// server frameworks.
func (a *APIApp) Handler() (http.Handler, error) {
return a.getNegroni()
}
// Run configured API service on the configured port. Before running
// the application, Run also resolves any sub-apps, and adds all
// routes.
func (a *APIApp) Run(ctx context.Context) error {
n, err := a.getNegroni()
if err != nil {
return err
}
srv := &http.Server{
Addr: fmt.Sprintf("%s:%d", a.address, a.port),
Handler: n,
ReadTimeout: time.Minute,
ReadHeaderTimeout: 30 * time.Second,
WriteTimeout: time.Minute,
}
catcher := grip.NewBasicCatcher()
serviceWait := make(chan struct{})
go func() {
defer recovery.LogStackTraceAndContinue("app service")
grip.Noticef("starting app on: %s:$d", a.address, a.port)
catcher.Add(srv.ListenAndServe())
}()
go func() {
defer recovery.LogStackTraceAndContinue("server shutdown")
catcher.Add(srv.Shutdown(ctx))
close(serviceWait)
}()
<-serviceWait
return catcher.Resolve()
}
// SetPort allows users to configure a default port for the API
// service. Defaults to 3000, and return errors will refuse to set the
// port to something unreasonable.
func (a *APIApp) SetPort(port int) error {
defaultPort := 3000
if port == a.port {
grip.Warningf("port is already set to %d", a.port)
} else if port <= 0 {
a.port = defaultPort
return fmt.Errorf("%d is not a valid port numbaer, using %d", port, defaultPort)
} else if port > 65535 {
a.port = defaultPort
return fmt.Errorf("port %d is too large, using default port (%d)", port, defaultPort)
} else if port < 1024 {
a.port = defaultPort
return fmt.Errorf("port %d is too small, using default port (%d)", port, defaultPort)
} else {
a.port = port
}
return nil
}
// SetHost sets the hostname or address for the application to listen
// on. Errors after resolving the application. You do not need to set
// this, and if unset the application will listen on the specified
// port on all interfaces.
func (a *APIApp) SetHost(name string) error {
if a.isResolved {
return fmt.Errorf("cannot set host to '%s', after resolving. Host is still '%s'",
name, a.address)
}
a.address = name
return nil
}
// SetPrefix sets the route prefix, adding a leading slash, "/", if
// neccessary.
func (a *APIApp) SetPrefix(p string) {
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
a.prefix = p
}