This repository was archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.go
More file actions
133 lines (105 loc) · 2.42 KB
/
request.go
File metadata and controls
133 lines (105 loc) · 2.42 KB
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
package treemux
import (
"context"
"net/http"
"strconv"
)
type Request struct {
*http.Request
ctx context.Context
route string
params Params
}
func NewRequest(req *http.Request) Request {
return Request{
ctx: req.Context(),
Request: req,
}
}
func (req Request) Context() context.Context {
return req.ctx
}
func (req Request) WithContext(ctx context.Context) Request {
req.ctx = ctx
return req
}
func (req Request) WithParams(params Params) Request {
req.params = params
return req
}
func (req Request) Route() string {
return req.route
}
func (req Request) Params() Params {
return req.params
}
func (req Request) Param(key string) string {
return req.params.Text(key)
}
//------------------------------------------------------------------------------
type Param struct {
Name string
Value string
}
type Params []Param
func (ps Params) Get(name string) (string, bool) {
for _, param := range ps {
if param.Name == name {
return param.Value, true
}
}
return "", false
}
func (ps Params) Text(name string) string {
s, _ := ps.Get(name)
return s
}
func (ps Params) Uint32(name string) (uint32, error) {
n, err := strconv.ParseUint(ps.Text(name), 10, 32)
return uint32(n), err
}
func (ps Params) Uint64(name string) (uint64, error) {
return strconv.ParseUint(ps.Text(name), 10, 64)
}
func (ps Params) Int32(name string) (int32, error) {
n, err := strconv.ParseInt(ps.Text(name), 10, 32)
return int32(n), err
}
func (ps Params) Int64(name string) (int64, error) {
return strconv.ParseInt(ps.Text(name), 10, 64)
}
func (ps Params) Map() map[string]string {
if len(ps) == 0 {
return nil
}
m := make(map[string]string, len(ps))
for _, param := range ps {
m[param.Name] = param.Value
}
return m
}
//------------------------------------------------------------------------------
type routeCtxKey struct{}
func RouteFromContext(ctx context.Context) *RouteInfo {
route, _ := ctx.Value(routeCtxKey{}).(*RouteInfo)
return route
}
func contextWithRoute(ctx context.Context, route string, params Params) context.Context {
return context.WithValue(ctx, routeCtxKey{}, &RouteInfo{
name: route,
params: params,
})
}
type RouteInfo struct {
name string
params Params // exported so users can change params
}
func (r *RouteInfo) Name() string {
return r.name
}
func (r *RouteInfo) Params() Params {
return r.params
}
func (r *RouteInfo) Param(name string) string {
return r.params.Text(name)
}