-
Notifications
You must be signed in to change notification settings - Fork 5
/
context.go
71 lines (54 loc) · 1.44 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
package fiber
import (
"context"
"time"
"github.com/gofiber/fiber/v2"
"github.com/goravel/framework/contracts/http"
"github.com/valyala/fasthttp"
)
func Background() http.Context {
app := fiber.New()
httpCtx := app.AcquireCtx(&fasthttp.RequestCtx{})
return NewContext(httpCtx)
}
type Context struct {
instance *fiber.Ctx
request http.ContextRequest
}
func NewContext(ctx *fiber.Ctx) 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 {
return NewContextResponse(c.instance, &ResponseOrigin{Ctx: c.instance})
}
func (c *Context) WithValue(key any, value any) {
ctx := context.WithValue(c.instance.UserContext(), key, value)
c.instance.SetUserContext(ctx)
}
func (c *Context) WithContext(ctx context.Context) {
c.instance.SetUserContext(ctx)
}
func (c *Context) Context() context.Context {
return c.instance.UserContext()
}
func (c *Context) Deadline() (deadline time.Time, ok bool) {
return c.instance.UserContext().Deadline()
}
func (c *Context) Done() <-chan struct{} {
return c.instance.UserContext().Done()
}
func (c *Context) Err() error {
return c.instance.UserContext().Err()
}
func (c *Context) Value(key any) any {
return c.instance.UserContext().Value(key)
}
func (c *Context) Instance() *fiber.Ctx {
return c.instance
}