/
app.go
82 lines (64 loc) · 2.22 KB
/
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
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
package app
import (
"time"
"go.uber.org/fx"
"exusiai.dev/backend-next/internal/app/appconfig"
"exusiai.dev/backend-next/internal/app/appcontext"
"exusiai.dev/backend-next/internal/controller"
"exusiai.dev/backend-next/internal/infra"
"exusiai.dev/backend-next/internal/model/cache"
"exusiai.dev/backend-next/internal/pkg/crypto"
"exusiai.dev/backend-next/internal/pkg/logger"
"exusiai.dev/backend-next/internal/repo"
"exusiai.dev/backend-next/internal/server"
"exusiai.dev/backend-next/internal/service"
"exusiai.dev/backend-next/internal/util/reportverifs"
"exusiai.dev/backend-next/internal/workers/calcwkr"
"exusiai.dev/backend-next/internal/workers/reportwkr"
)
func Options(ctx appcontext.Ctx, additionalOpts ...fx.Option) []fx.Option {
conf, err := appconfig.Parse(ctx)
if err != nil {
panic(err)
}
// logger and configuration are the only two things that are not in the fx graph
// because some other packages need them to be initialized before fx starts
logger.Configure(conf)
baseOpts := []fx.Option{
// fx meta
fx.WithLogger(logger.Fx),
// Misc
fx.Supply(conf),
fx.Provide(crypto.NewCrypto),
// Infrastructures
infra.Module(),
// Servers
server.Module(),
// Verifiers
reportverifs.Module(),
// Repositories
repo.Module(),
// Services
service.Module(),
// Global Singleton Inits: Keep those before controllers to ensure they are initialized
// before controllers are registered as controllers are also fx#Invoke functions which
// are called in the order of their registration.
fx.Invoke(infra.SentryInit),
fx.Invoke(cache.Initialize),
// Controllers
controller.Module(controller.OptIncludeSwagger),
// Workers
fx.Invoke(calcwkr.Start),
fx.Invoke(reportwkr.Start),
// fx Extra Options
fx.StartTimeout(1 * time.Second),
// StopTimeout is not typically needed, since we're using fiber's Shutdown(),
// in which fiber has its own IdleTimeout for controlling the shutdown timeout.
// It acts as a countermeasure in case the fiber app is not properly shutting down.
fx.StopTimeout(5 * time.Minute),
}
return append(baseOpts, additionalOpts...)
}
func New(ctx appcontext.Ctx, additionalOpts ...fx.Option) *fx.App {
return fx.New(Options(ctx, additionalOpts...)...)
}