-
Notifications
You must be signed in to change notification settings - Fork 144
/
httpctx.go
49 lines (41 loc) · 1.21 KB
/
httpctx.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
// Package httpctx provides a context-aware HTTP handler adaptor
package httpctx
import (
"net/http"
"strings"
"golang.org/x/net/context"
)
// Handler is a request handler with an added context
type Handler interface {
ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request)
}
// A HandlerFunc is an adaptor to turn a function in to a Handler
type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request)
// ServeHTTPContext calls the underlying handler function
func (h HandlerFunc) ServeHTTPContext(
ctx context.Context, rw http.ResponseWriter, req *http.Request,
) {
h(ctx, rw, req)
}
// Adapter turns a context.Handler to an http.Handler
type Adapter struct {
Ctx context.Context
Handler Handler
}
func (ca *Adapter) ServeHTTP(
rw http.ResponseWriter, req *http.Request,
) {
ca.Handler.ServeHTTPContext(ca.Ctx, rw, req)
}
// StripPrefix strips a prefix from the request URL
func StripPrefix(prefix string, h Handler) Handler {
if prefix == "" {
return h
}
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {
r.URL.Path = p
h.ServeHTTPContext(ctx, w, r)
}
})
}