-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
275 lines (245 loc) · 9.16 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
package lungo
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"sync"
)
// App is the top-level application instance
type App struct {
mutex sync.RWMutex
pool sync.Pool
config *Config
router *Router
server *http.Server
}
// New creates an instance of App.
func New(configure ...func(*Config)) (app *App) {
app = &App{
router: NewRouter(),
pool: sync.Pool{
New: func() any {
return &Context{App: app}
},
},
config: &Config{
MaxBodySize: DefaultMaxBodySize,
},
}
for _, c := range configure {
c(app.config)
}
return
}
// NewContext returns a new Context instance.
//
// It serves as an adapter for http.Handlerfunc and converts the
// request to the context based API provided by Lungo.
func (app *App) NewContext(w http.ResponseWriter, r *http.Request) *Context {
params, _ := url.ParseQuery(r.URL.RawQuery)
return &Context{
App: app,
Request: r,
Response: w,
Params: params,
}
}
// Server returns the http.Server instance of the application
func (app *App) Server() *http.Server {
return app.server
}
// Config returns the Config instance of the application
func (app *App) Config() *Config {
return app.config
}
// AcquireContext acquires a empty context instance from the pool.
// This context instance must be released by calling `ReleaseContext()`.
func (app *App) AcquireContext() *Context {
return app.pool.Get().(*Context)
}
// ReleaseContext releases the context instance back to the pool.
// The context instace must first be acquired by calling `AcquireContext()`.
func (app *App) ReleaseContext(c *Context) {
app.pool.Put(c)
}
// Get adds a new Route with http method "GET" to the Router of the application.
func (app *App) Get(path string, handler HandlerFunc) {
app.Handle(http.MethodGet, path, handler)
}
// Head adds a new Route with http method "HEAD" to the Router of the application.
func (app *App) Head(path string, handler HandlerFunc) {
app.Handle(http.MethodHead, path, handler)
}
// Post adds a new Route with http method "POST" to the Router of the application.
func (app *App) Post(path string, handler HandlerFunc) {
app.Handle(http.MethodPost, path, handler)
}
// Put adds a new Route with http method "PUT" to the Router of the application.
func (app *App) Put(path string, handler HandlerFunc) {
app.Handle(http.MethodPut, path, handler)
}
// Patch adds a new Route with http method "PATCH" to the Router of the application.
func (app *App) Patch(path string, handler HandlerFunc) {
app.Handle(http.MethodPatch, path, handler)
}
// Delete adds a new Route with http method "DELETE" to the Router of the application.
func (app *App) Delete(path string, handler HandlerFunc) {
app.Handle(http.MethodDelete, path, handler)
}
// Connect adds a new Route with http method "CONNECT" to the Router of the application.
func (app *App) Connect(path string, handler HandlerFunc) {
app.Handle(http.MethodConnect, path, handler)
}
// Options adds a new Route with http method "OPTIONS" to the Router of the application.
func (app *App) Options(path string, handler HandlerFunc) {
app.Handle(http.MethodOptions, path, handler)
}
// Trace adds a new Route with http method "TRACE" to the Router of the application.
func (app *App) Trace(path string, handler HandlerFunc) {
app.Handle(http.MethodTrace, path, handler)
}
// Handle adds a new Route with the specified http method to the Router of the application.
func (app *App) Handle(method, path string, handler HandlerFunc) {
app.router.Handle(Route{Method: method, Path: path, Handler: handler})
}
// All adds a new Route on all HTTP methods to the Router of the application.
func (app *App) All(path string, handler HandlerFunc) {
for _, method := range methods {
app.Handle(method, path, handler)
}
}
// Static adds a new Route to the Router of the application, which serves static files.
func (app *App) Static(path, root string) {
app.router.Handle(Route{Method: http.MethodGet, Path: path, Handler: FileHandler(root)})
}
// Use adds a Middleware to the router.
// Middleware can be used to intercept or otherwise modify requests.
// The are executed in the order that they are applied to the Router (FIFO).
func (app *App) Use(middlewares ...Middleware) {
app.router.Use(middlewares...)
}
// Mount adds a new app which handles requests on the specified pattern.
func (app *App) Mount(pattern string, group *App) {
app.All(pattern, WithContext(http.StripPrefix(pattern, group)))
}
// HandleError is a centralized error handler function which resolves the
// provided error and replies to the request with the specified error message
// and HTTP code. The error message is written as plain text.
// JSON response with status code and message.
func (app *App) HandleError(c *Context, e error) {
re, ok := e.(*RequestError)
if !ok {
re = &RequestError{
Code: http.StatusInternalServerError,
Message: http.StatusText(http.StatusInternalServerError),
}
}
http.Error(c.Response, re.Message, re.Code)
}
// ServeHTTP implements the http.Handler interface which
// is used by the http.Server to dispatch requests.
//
// It serves as an adapter for http.Handler and converts the
// request to the context based API provided by Lungo.
func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c, ok := r.Context().Value("context").(*Context)
if !ok {
c = app.AcquireContext()
}
c.Reset(w, r)
if err := app.router.ServeHTTP(c); err != nil {
app.HandleError(c, err)
}
if !ok {
app.ReleaseContext(c)
}
}
// Serve accepts incoming HTTP connections on the listener l,
// creating a new service goroutine for each. The service goroutines
// read requests and then call handler to reply to them.
//
// The handler is typically nil, in which case the DefaultServeMux is used.
//
// HTTP/2 support is only enabled if the Listener returns *tls.Conn
// connections and they were configured with "h2" in the TLS
// Config.NextProtos.
//
// Serve always returns a non-nil error.
func (app *App) Serve(l net.Listener) error {
app.mutex.Lock()
app.server = &http.Server{Handler: app}
app.mutex.Unlock()
return app.server.Serve(l)
}
// ServeTLS accepts incoming HTTPS connections on the listener l,
// creating a new service goroutine for each. The service goroutines
// read requests and then call handler to reply to them.
//
// The handler is typically nil, in which case the DefaultServeMux is used.
//
// Additionally, files containing a certificate and matching private key
// for the server must be provided. If the certificate is signed by a
// certificate authority, the certFile should be the concatenation
// of the server's certificate, any intermediates, and the CA's certificate.
//
// ServeTLS always returns a non-nil error.
func (app *App) ServeTLS(l net.Listener, certFile, keyFile string) error {
app.mutex.Lock()
app.server = &http.Server{Handler: app}
app.mutex.Unlock()
return app.server.ServeTLS(l, certFile, keyFile)
}
// Listen listens on the TCP network address addr and then calls
// Serve with handler to handle requests on incoming connections.
// Accepted connections are configured to enable TCP keep-alives.
//
// The handler is typically nil, in which case the DefaultServeMux is used.
//
// ListenAndServe always returns a non-nil error.
func (app *App) Listen(addr string) error {
app.mutex.Lock()
app.server = &http.Server{Addr: addr, Handler: app}
app.mutex.Unlock()
return app.server.ListenAndServe()
}
// ListenTLS acts identically to Listen, except that it
// expects HTTPS connections. Additionally, files containing a certificate and
// matching private key for the server must be provided. If the certificate
// is signed by a certificate authority, the certFile should be the concatenation
// of the server's certificate, any intermediates, and the CA's certificate.
func (app *App) ListenTLS(addr, certFile, keyFile string) error {
app.mutex.Lock()
app.server = &http.Server{Addr: addr, Handler: app}
app.mutex.Unlock()
return app.server.ListenAndServeTLS(certFile, keyFile)
}
// Shutdown gracefully shuts down the server without interrupting any
// active connections. Shutdown works by first closing all open
// listeners, then closing all idle connections, and then waiting
// indefinitely for connections to return to idle and then shut down.
// If the provided context expires before the shutdown is complete,
// Shutdown returns the context's error, otherwise it returns any
// error returned from closing the Server's underlying Listener(s).
//
// When Shutdown is called, Serve, ListenAndServe, and
// ListenAndServeTLS immediately return ErrServerClosed. Make sure the
// program doesn't exit and waits instead for Shutdown to return.
//
// Shutdown does not attempt to close nor wait for hijacked
// connections such as WebSockets. The caller of Shutdown should
// separately notify such long-lived connections of shutdown and wait
// for them to close, if desired. See RegisterOnShutdown for a way to
// register shutdown notification functions.
//
// Once Shutdown has been called on a server, it may not be reused;
// future calls to methods such as Serve will return ErrServerClosed.
func (app *App) Shutdown(ctx context.Context) error {
app.mutex.RLock()
defer app.mutex.RUnlock()
if app.server == nil {
return fmt.Errorf("Shutdown: Server is not running")
}
return app.server.Shutdown(ctx)
}