-
Notifications
You must be signed in to change notification settings - Fork 7
/
context.go
76 lines (58 loc) · 1.48 KB
/
context.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
package gin
import (
"context"
"net/http/httptest"
"time"
"github.com/gin-gonic/gin"
"github.com/goravel/framework/contracts/http"
)
func Background() http.Context {
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
return NewContext(ctx)
}
type Context struct {
instance *gin.Context
request http.ContextRequest
}
func NewContext(ctx *gin.Context) http.Context {
return &Context{instance: ctx}
}
func (c *Context) Request() http.ContextRequest {
if c.request == nil {
c.request = NewContextRequest(c, LogFacade, ValidationFacade)
}
return c.request
}
func (c *Context) Response() http.ContextResponse {
responseOrigin := c.Value("responseOrigin")
if responseOrigin != nil {
return NewContextResponse(c.instance, responseOrigin.(http.ResponseOrigin))
}
return NewContextResponse(c.instance, &BodyWriter{ResponseWriter: c.instance.Writer})
}
func (c *Context) WithValue(key string, value any) {
c.instance.Set(key, value)
}
func (c *Context) Context() context.Context {
ctx := context.Background()
for key, value := range c.instance.Keys {
// nolint
ctx = context.WithValue(ctx, key, value)
}
return ctx
}
func (c *Context) Deadline() (deadline time.Time, ok bool) {
return c.instance.Deadline()
}
func (c *Context) Done() <-chan struct{} {
return c.instance.Done()
}
func (c *Context) Err() error {
return c.instance.Err()
}
func (c *Context) Value(key any) any {
return c.instance.Value(key)
}
func (c *Context) Instance() *gin.Context {
return c.instance
}