/
endpoint.go
76 lines (62 loc) · 1.85 KB
/
endpoint.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
package api
import (
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
"golang.org/x/net/context"
)
// An Endpoint is the structure representing an API endpoint
// encapsulating information needed to
// dispatch a request and generate documentation.
type Endpoint struct {
Method string
Path string
// The middlewares to execute on the request.
Middleware MiddlewareStack
// Called after middleware stack was executed on the request
Implementation func(ctx context.Context, r *Req)
}
// Append a middleware to the middleware stack.
func (e Endpoint) Use(mw Middleware) {
e.Middleware = append(e.Middleware, mw)
}
// ServeHTTP implements the http.Handler interface
func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req := WrapReq(w, r)
e.Serve(context.Background(), req)
}
// HandlerFunc converts an Endpoint to a http.HandlerFunc
func (e Endpoint) HandlerFunc() http.HandlerFunc {
return http.HandlerFunc(e.ServeHTTP)
}
// Handle is a httprouter.Handle function
func (e Endpoint) Handle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
req := WrapHttpRouterReq(w, r, ps)
e.Serve(context.Background(), req)
}
// Serve dispatches an api.Req
func (e Endpoint) Serve(ctx context.Context, req *Req) {
defer req.handlePanic()
// Parse the parameters and cleanup
defer cleanUpParams(req)
err := req.ParseParams()
// We must return a 400 and stop here if there was a problem parsing the request.
if err != nil {
er := WrapErr(err, 400)
http.Error(req.Response, er.HTTPBody(), er.HTTPStatus())
return
}
// call each middleware
for _, m := range e.Middleware {
c, err := m.Run(ctx, req)
if err != nil {
er := WrapErr(err, 0)
req.Response.WriteHeader(er.HTTPStatus())
fmt.Fprintln(req.Response, er.HTTPBody())
return
}
ctx = c
}
// Dispatch the request via the endpoint
e.Implementation(ctx, req)
}