/
bootstrap.go
61 lines (49 loc) · 1.44 KB
/
bootstrap.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
package fxbootstrapper
import (
"testing"
"github.com/ekkinox/fx-template/modules/fxconfig"
"github.com/ekkinox/fx-template/modules/fxhealthchecker"
"github.com/ekkinox/fx-template/modules/fxlogger"
"github.com/ekkinox/fx-template/modules/fxtracer"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
)
type Bootstrapper struct {
defaultOptions []fx.Option
}
func NewBootstrapper() *Bootstrapper {
return &Bootstrapper{
defaultOptions: []fx.Option{
fxconfig.FxConfigModule,
fxlogger.FxLoggerModule,
fxtracer.FxTracerModule,
fxhealthchecker.FxHealthCheckerModule,
},
}
}
func (b *Bootstrapper) WithOptions(option ...fx.Option) *Bootstrapper {
b.defaultOptions = append(b.defaultOptions, option...)
return b
}
func (b *Bootstrapper) BoostrapApp(bootstrapOptions ...fx.Option) *fx.App {
return fx.New(
fx.WithLogger(fxlogger.FxEventLogger),
fx.Options(b.defaultOptions...),
fx.Options(bootstrapOptions...),
)
}
func (b *Bootstrapper) BoostrapAndRunApp(bootstrapOptions ...fx.Option) {
b.BoostrapApp(bootstrapOptions...).Run()
}
func (b *Bootstrapper) BoostrapTestApp(t testing.TB, bootstrapOptions ...fx.Option) *fxtest.App {
t.Setenv("APP_ENV", "test")
return fxtest.New(
t,
fx.NopLogger,
fx.Options(b.defaultOptions...),
fx.Options(bootstrapOptions...),
)
}
func (b *Bootstrapper) BoostrapAndRunTestApp(t testing.TB, bootstrapOptions ...fx.Option) {
b.BoostrapTestApp(t, bootstrapOptions...).RequireStart().RequireStop()
}