forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux.go
214 lines (193 loc) · 6.47 KB
/
mux.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
package goa
import (
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
"golang.org/x/net/context"
"github.com/goadesign/goa/design"
"github.com/julienschmidt/httprouter"
)
type (
// MuxHandler provides the low level implementation for an API endpoint.
// The values argument includes both the querystring and path parameter values.
MuxHandler func(http.ResponseWriter, *http.Request, url.Values)
// ServeMux is the interface implemented by the service request muxes. There is one instance
// of ServeMux per service version and one for requests targeting no version.
// It implements http.Handler and makes it possible to register request handlers for
// specific HTTP methods and request path via the Handle method.
ServeMux interface {
http.Handler
// Handle sets the MuxHandler for a given HTTP method and path.
Handle(method, path string, handle MuxHandler)
// Lookup returns the MuxHandler associated with the given HTTP method and path.
Lookup(method, path string) MuxHandler
}
// VersionMux is implemented by muxes that back versioned APIs.
VersionMux interface {
// Mux returns the mux for the version with given name.
Mux(version string) ServeMux
// VersionName returns the name of the version targeted by the given request.
VersionName(req *http.Request) string
// HandleMissingVersion handles requests that target a non-existent API version (that
// is requests for which RequestMux returns nil).
// The context request data object contains the name of the targeted version.
HandleMissingVersion(ctx context.Context, rw http.ResponseWriter, req *http.Request)
}
// Muxer implements an adapter that given a request handler can produce a mux handler.
Muxer interface {
MuxHandler(name string, hdlr Handler, unm Unmarshaler) MuxHandler
}
// RootMux is the default VersionMux and ServeMux implementation. It dispatches requests to the
// appropriate version mux using a SelectVersionFunc. There is one and exactly one root mux per
// service.
RootMux struct {
*mux
SelectVersionFunc SelectVersionFunc
muxes map[string]ServeMux
service *Service // Keep reference to service for encoding missing version responses
}
// SelectVersionFunc computes the API version targeted by a given request.
SelectVersionFunc func(*http.Request) string
// mux is the default ServeMux implementation.
mux struct {
router *httprouter.Router
handles map[string]MuxHandler
}
)
// NewMux returns a RootMux.
func NewMux(service *Service) *RootMux {
return &RootMux{
mux: &mux{
router: httprouter.New(),
handles: make(map[string]MuxHandler),
},
service: service,
}
}
// PathSelectVersionFunc returns a SelectVersionFunc that uses the given path pattern and param to
// extract the version from the request path. Use the same path pattern given in the DSL to define
// the API base path, e.g. "/api/:api_version".
func PathSelectVersionFunc(pattern, param string) (SelectVersionFunc, error) {
params := design.ExtractWildcards(pattern)
index := -1
for i, p := range params {
if p == param {
index = i
break
}
}
if index == -1 {
return nil, fmt.Errorf("Mux versioning setup: no param %s in pattern %s", param, pattern)
}
rgs := strings.Replace(pattern, ":"+param, `([^/]+)`, 1)
rg := regexp.MustCompile("^" + rgs)
return func(req *http.Request) (version string) {
match := rg.FindStringSubmatch(req.URL.Path)
if len(match) > 1 {
version = match[1]
}
return
}, nil
}
// HeaderSelectVersionFunc returns a SelectVersionFunc that looks for the version in the header with
// the given name.
func HeaderSelectVersionFunc(header string) SelectVersionFunc {
return func(req *http.Request) string {
return req.Header.Get(header)
}
}
// QuerySelectVersionFunc returns a SelectVersionFunc that looks for the version in the querystring
// with the given key.
func QuerySelectVersionFunc(query string) SelectVersionFunc {
return func(req *http.Request) string {
return req.URL.Query().Get(query)
}
}
// CombineSelectVersionFunc returns a SelectVersionFunc that tries each func passed as argument
// in order and returns the first non-empty string version.
func CombineSelectVersionFunc(funcs ...SelectVersionFunc) SelectVersionFunc {
switch len(funcs) {
case 0:
return nil
case 1:
return funcs[0]
default:
return func(req *http.Request) string {
for _, f := range funcs {
if version := f(req); version != "" {
return version
}
}
return ""
}
}
}
// ServeHTTP is the function called back by the underlying HTTP server to handle incoming requests.
func (m *RootMux) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Optimize the unversionned API case
if m.SelectVersionFunc == nil || len(m.muxes) == 0 {
m.router.ServeHTTP(rw, req)
return
}
var mux ServeMux
version := m.VersionName(req)
if version == "" {
mux = m.mux
} else {
var ok bool
mux, ok = m.muxes[version]
if !ok {
ctx := NewContext(RootContext, m.service, rw, req, nil)
go IncrCounter([]string{"goa", "handler", "missingversion", version}, 1.0)
resp := TypedError{
ID: ErrInvalidVersion,
Mesg: fmt.Sprintf(`API does not support version %s`, version),
}
Response(ctx).Send(ctx, 400, resp)
return
}
}
mux.ServeHTTP(rw, req)
}
// Mux returns the mux addressing the given version.
func (m *RootMux) Mux(version string) ServeMux {
if m.muxes == nil {
m.muxes = make(map[string]ServeMux)
}
if mux, ok := m.muxes[version]; ok {
return mux
}
mux := &mux{
router: httprouter.New(),
handles: make(map[string]MuxHandler),
}
m.muxes[version] = mux
return mux
}
// VersionName returns the name of the version targeted by the request if any, emoty string
// otherwise.
func (m *RootMux) VersionName(req *http.Request) string {
return m.SelectVersionFunc(req)
}
// Handle sets the handler for the given verb and path.
func (m *mux) Handle(method, path string, handle MuxHandler) {
hthandle := func(rw http.ResponseWriter, req *http.Request, htparams httprouter.Params) {
params := req.URL.Query()
for _, p := range htparams {
params.Set(p.Key, p.Value)
}
handle(rw, req, params)
}
m.handles[method+path] = handle
m.router.Handle(method, path, hthandle)
}
// Lookup returns the MuxHandler associated with the given method and path.
func (m *mux) Lookup(method, path string) MuxHandler {
return m.handles[method+path]
}
// ServeHTTP is the function called back by the underlying HTTP server to handle incoming requests.
func (m *mux) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
m.router.ServeHTTP(rw, req)
}