-
Notifications
You must be signed in to change notification settings - Fork 13
/
registry.go
132 lines (111 loc) · 2.87 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
package hitrix
import (
"context"
"flag"
"log"
"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
servicesDefinitions []*service.Definition
}
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) Build() (*Hitrix, func()) {
r.initializeIoCHandlers()
flags := service.DI().App().Flags
if flags.Bool("list-scripts") {
listScrips()
}
scriptToRun := flags.String("run-script")
ctx, cancel := context.WithCancel(context.Background())
h := &Hitrix{ctx: ctx, cancel: cancel, done: make(chan bool), exit: make(chan int)}
if scriptToRun != "" {
h.runDynamicScrips(ctx, scriptToRun)
}
return h, func() {
if r := recover(); r != nil {
errorLogger, has := service.DI().ErrorLogger()
if has {
errorLogger.LogError(r)
} else {
log.Print(r.(string))
}
}
}
}
func (r *Registry) RegisterDevPanel(devPanelUserEntity app.DevPanelUserEntity, router func(ginEngine *gin.Engine), poolStream *string) *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, PoolStream: poolStream}
return r
}
func (r *Registry) RegisterDIService(service ...*service.Definition) *Registry {
r.servicesDefinitions = append(r.servicesDefinitions, service...)
return r
}
func (r *Registry) initializeIoCHandlers() {
ioCBuilder, _ := di.NewBuilder()
defaultDefinitions := []*service.Definition{
registry.ServiceApp(r.app),
registry.ServiceConfig(),
}
flagsRegistry := &app.FlagsRegistry{Flags: make(map[string]interface{})}
for _, def := range append(defaultDefinitions, r.servicesDefinitions...) {
if def == nil {
continue
}
var scope string
if def.Global {
scope = di.App
} else {
scope = di.Request
}
if def.Script {
r.app.Scripts = append(r.app.Scripts, def.Name)
}
err := ioCBuilder.Add(di.Def{
Name: def.Name,
Scope: scope,
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("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())
if !flag.Parsed() {
flag.Parse()
}
r.app.Flags = &app.Flags{Registry: flagsRegistry}
}