-
Notifications
You must be signed in to change notification settings - Fork 14
/
registry.go
142 lines (118 loc) · 3.42 KB
/
registry.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package hitrix
import (
"context"
"flag"
"os"
"github.com/coretrix/hitrix/service/registry"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/app"
"github.com/gin-gonic/gin"
"github.com/sarulabs/di"
)
type Registry struct {
app *app.App
servicesDefinitionsGlobal []*service.DefinitionGlobal
servicesDefinitionsRequest []*service.DefinitionRequest
}
func New(appName string, secret string) *Registry {
mode, hasMode := os.LookupEnv("APP_MODE")
if !hasMode {
mode = app.ModeLocal
}
r := &Registry{
app: &app.App{Mode: mode, Name: appName, Secret: secret},
}
return r
}
func (r *Registry) SetParallelTestID(parallelTestID string) *Registry {
r.app.ParallelTestID = parallelTestID
return r
}
func (r *Registry) Build() (*Hitrix, func()) {
globalContext, cancel := context.WithCancel(context.Background())
r.app.GlobalContext = globalContext
r.app.CancelContext = cancel
r.initializeIoCHandlers()
flags := service.DI().App().Flags
if flags.Bool("list-scripts") {
listScrips()
}
scriptToRun := flags.String("run-script")
h := &Hitrix{done: make(chan bool), exit: make(chan int)}
if scriptToRun != "" {
h.runDynamicScrips(globalContext, scriptToRun)
}
h.startupOnBuild()
return h, func() {
if r := recover(); r != nil {
service.DI().ErrorLogger().LogError(r)
}
}
}
func (r *Registry) RegisterDevPanel(devPanelUserEntity app.IDevPanelUserEntity, router func(ginEngine *gin.Engine)) *Registry {
if devPanelUserEntity == nil {
panic("devPanelUserEntity cannot be nil")
}
if router == nil {
panic("router cannot be nil")
}
r.app.DevPanel = &app.DevPanel{UserEntity: devPanelUserEntity, Router: router}
return r
}
func (r *Registry) RegisterRedisPools(pools *app.RedisPools) *Registry {
r.app.RedisPools = pools
return r
}
func (r *Registry) RegisterDIGlobalService(service ...*service.DefinitionGlobal) *Registry {
r.servicesDefinitionsGlobal = append(r.servicesDefinitionsGlobal, service...)
return r
}
func (r *Registry) RegisterDIRequestService(service ...*service.DefinitionRequest) *Registry {
r.servicesDefinitionsRequest = append(r.servicesDefinitionsRequest, service...)
return r
}
func (r *Registry) initializeIoCHandlers() {
ioCBuilder, _ := di.NewBuilder()
defaultDefinitions := []*service.DefinitionGlobal{
registry.ServiceProviderApp(r.app),
registry.ServiceProviderConfig(),
registry.ServiceProviderGoroutine(),
}
flagsRegistry := &app.FlagsRegistry{Flags: make(map[string]interface{})}
for _, def := range append(defaultDefinitions, r.servicesDefinitionsGlobal...) {
if def == nil {
continue
}
if def.Script {
r.app.Scripts = append(r.app.Scripts, def.Name)
}
err := ioCBuilder.Add(di.Def{
Name: def.Name,
Scope: di.App,
Build: def.Build,
Close: def.Close,
})
if err != nil {
panic(err)
}
if def.Flags != nil && !flag.Parsed() {
def.Flags(flagsRegistry)
}
}
if !flag.Parsed() {
flagsRegistry.Bool("force-alters", false, "Execute all alters")
flagsRegistry.Bool("pre-deploy", false, "Check for alters and exit")
flagsRegistry.Bool("list-scripts", false, "list all available scripts")
flagsRegistry.String("run-script", "", "run script")
}
err := ioCBuilder.Add()
if err != nil {
panic(err)
}
service.SetContainer(ioCBuilder.Build())
service.SetRequestServices(r.servicesDefinitionsRequest)
if !flag.Parsed() {
flag.Parse()
}
r.app.Flags = &app.Flags{Registry: flagsRegistry}
}