-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_context.go
58 lines (49 loc) · 1.27 KB
/
pool_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
package app_with_pools
import (
"github.com/evgeniums/go-backend-helpers/pkg/app_context"
"github.com/evgeniums/go-backend-helpers/pkg/db"
"github.com/evgeniums/go-backend-helpers/pkg/logger"
"github.com/evgeniums/go-backend-helpers/pkg/op_context"
"github.com/evgeniums/go-backend-helpers/pkg/op_context/default_op_context"
"github.com/evgeniums/go-backend-helpers/pkg/pool"
)
type Context interface {
op_context.Context
Pool() pool.Pool
SetPool(pool pool.Pool)
}
type ContextBase struct {
op_context.Context
pool pool.Pool
}
func (c *ContextBase) Pool() pool.Pool {
return c.pool
}
func (c *ContextBase) SetPool(pool pool.Pool) {
c.pool = pool
}
func (c *ContextBase) Construct(baseCtx ...op_context.Context) {
if len(baseCtx) == 0 {
c.Context = default_op_context.NewContext()
} else {
c.Context = baseCtx[0]
}
}
func NewOpContext(fromCtx ...op_context.Context) *ContextBase {
c := &ContextBase{}
c.Construct(fromCtx...)
return c
}
func NewInitOpContext(app app_context.Context, log logger.Logger, db db.DB) *ContextBase {
c := default_op_context.NewContext()
c.Init(app, log, db)
t := NewOpContext(c)
return t
}
func ContextSelfPool(ctx Context) string {
app, ok := ctx.App().(AppWithPools)
if !ok {
return ""
}
return pool.SelfPoolName(app.Pools())
}