-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
105 lines (87 loc) · 1.92 KB
/
runtime.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
package espresso
import (
"context"
"net/http"
"time"
)
type runtimeEndpoint struct {
request *http.Request
endpoint *Endpoint
err BindErrors
}
func (e *runtimeEndpoint) BindPath(key string, v any) EndpointBuilder {
binder, ok := e.endpoint.PathParams[key]
if !ok {
return e
}
strV := e.request.PathValue(key)
if err := binder.Func(v, strV); err != nil {
e.err = append(e.err, errorBind(binder, err))
}
return e
}
func (e *runtimeEndpoint) End() BindErrors {
return e.err
}
type runtimeContext struct {
ctx context.Context
endpoint *Endpoint
request *http.Request
response http.ResponseWriter
err error
chainIndex int
}
func (c *runtimeContext) Endpoint(method, path string, mid ...HandleFunc) EndpointBuilder {
return &runtimeEndpoint{
request: c.request,
endpoint: c.endpoint,
}
}
func (c *runtimeContext) Value(key any) any {
return c.ctx.Value(key)
}
func (c *runtimeContext) Deadline() (time.Time, bool) {
return c.ctx.Deadline()
}
func (c *runtimeContext) Done() <-chan struct{} {
return c.ctx.Done()
}
func (c *runtimeContext) Err() error {
if c.err != nil {
return c.err
}
return c.ctx.Err()
}
func (c *runtimeContext) WithParent(ctx context.Context) Context {
return &runtimeContext{
ctx: ctx,
endpoint: c.endpoint,
request: c.request,
response: c.response,
err: c.err,
chainIndex: c.chainIndex,
}
}
func (c *runtimeContext) WithResponseWriter(w http.ResponseWriter) Context {
return &runtimeContext{
ctx: c.ctx,
endpoint: c.endpoint,
request: c.request,
response: w,
err: c.err,
chainIndex: c.chainIndex,
}
}
func (c *runtimeContext) Request() *http.Request {
return c.request
}
func (c *runtimeContext) ResponseWriter() http.ResponseWriter {
return c.response
}
func (c *runtimeContext) Next() {
index := c.chainIndex
c.chainIndex++
if err := c.endpoint.ChainFuncs[index](c); err != nil {
c.err = err
}
}