forked from go-chi/chi
/
chi.go
66 lines (53 loc) · 1.89 KB
/
chi.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
package chi
import (
"net/http"
"golang.org/x/net/context"
)
func NewRouter() *Mux {
return NewMux()
}
type Router interface {
http.Handler
Handler
Use(middlewares ...interface{})
Group(fn func(r Router)) Router
Route(pattern string, fn func(r Router)) Router
Mount(pattern string, handlers ...interface{})
Handle(pattern string, handlers ...interface{})
NotFound(h HandlerFunc)
Connect(pattern string, handlers ...interface{})
Head(pattern string, handlers ...interface{})
Get(pattern string, handlers ...interface{})
Post(pattern string, handlers ...interface{})
Put(pattern string, handlers ...interface{})
Patch(pattern string, handlers ...interface{})
Delete(pattern string, handlers ...interface{})
Trace(pattern string, handlers ...interface{})
Options(pattern string, handlers ...interface{})
}
// NOTE, the plan: hopefully once net/context makes it into the stdlib and
// net/http supports a request context, we will remove the chi.Handler
// interface, and the Router argument types will be http.Handler instead
// of interface{}.
// Handler is like net/http's http.Handler, but also includes a
// mechanism for serving requests with a context.
type Handler interface {
ServeHTTPC(context.Context, http.ResponseWriter, *http.Request)
}
// HandlerFunc is like net/http's http.HandlerFunc, but supports a context
// object.
type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request)
// ServeHTTPC wraps ServeHTTP with a context parameter.
func (h HandlerFunc) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) {
h(ctx, w, r)
}
// ServeHTTP provides compatibility with http.Handler.
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h(context.Background(), w, r)
}
func URLParams(ctx context.Context) map[string]string {
if urlParams, ok := ctx.Value(URLParamsCtxKey).(map[string]string); ok {
return urlParams
}
return nil
}