-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
50 lines (41 loc) · 940 Bytes
/
app.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
package app
import (
"github.com/shoplineapp/go-app/plugins"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
_ "go.uber.org/automaxprocs"
)
type Application struct {
name string
fx *fx.App
plugins []interface{}
}
type AppOption struct{}
func NewApplication() *Application {
return &Application{
plugins: plugins.Registry,
}
}
func (a *Application) SetPlugins(plugins ...interface{}) {
a.plugins = plugins
}
func (a *Application) AddModule(module AppModuleInterface) {
controllers := module.Controllers()
provides := module.Provide()
if len(controllers) > 0 {
a.plugins = append(a.plugins, module.Controllers()...)
}
if len(provides) > 0 {
a.plugins = append(a.plugins, module.Provide()...)
}
}
func (app *Application) Run(funcs ...interface{}) {
app.fx = fx.New(
fx.WithLogger(func() fxevent.Logger { return AppLogger{} }),
fx.Provide(
app.plugins...,
),
fx.Invoke(funcs...),
)
app.fx.Run()
}