-
Notifications
You must be signed in to change notification settings - Fork 3
/
ctx.go
90 lines (72 loc) · 1.76 KB
/
ctx.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
package lucid
import (
"context"
e "errors"
"net/http"
"time"
"github.com/lucidfy/lucid/pkg/engines"
"github.com/lucidfy/lucid/pkg/facade/session"
"github.com/lucidfy/lucid/pkg/helpers"
)
type EngineCtx struct{}
type RouterCtx struct{}
type Middleware struct {
Continue bool
}
type Context struct {
ctx context.Context
}
func New(ctx context.Context) Context {
return Context{ctx: ctx}
}
func (c Context) Engine() engines.EngineContract {
return c.ctx.Value(EngineCtx{}).(engines.EngineContract)
}
func (c Context) Router() RouterContract {
return c.ctx.Value(RouterCtx{}).(RouterContract)
}
func (c Context) Session() session.SessionContract {
coo := c.Engine().GetCookie()
var ses session.SessionContract
if helpers.IsTrue(helpers.Getenv("SESSION_ENABLED", "true")) {
sessionKey, app_err := coo.Get(helpers.Getenv("SESSION_NAME", "lucid_session"))
if app_err != nil && e.Is(app_err.Error, http.ErrNoCookie) {
sessionKey = coo.CreateSessionCookie()
}
ses = session.Driver(
helpers.Getenv("SESSION_DRIVER", "file"),
sessionKey.(string),
)
}
return ses
}
func (c Context) Stop() Middleware {
return Middleware{
Continue: false,
}
}
func (c Context) Next() Middleware {
return Middleware{
Continue: true,
}
}
func (c *Context) Bind(key interface{}, value interface{}) *Context {
c.ctx = context.WithValue(c.ctx, key, value)
return c
}
// this should be similar to Value()
func (c Context) Resolve(key interface{}) interface{} {
return c.Value(key)
}
func (c Context) Deadline() (deadline time.Time, ok bool) {
return c.ctx.Deadline()
}
func (c Context) Done() <-chan struct{} {
return c.ctx.Done()
}
func (c Context) Err() error {
return c.ctx.Err()
}
func (c Context) Value(key interface{}) interface{} {
return c.ctx.Value(key)
}