-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
219 lines (192 loc) · 6.82 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"os"
"runtime"
"time"
"github.com/alecthomas/kong"
"github.com/microcosm-cc/bluemonday"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
"go.uber.org/zap"
// GRPC Services
pbauth "github.com/fivenet-app/fivenet/gen/go/proto/services/auth"
pbcalendar "github.com/fivenet-app/fivenet/gen/go/proto/services/calendar"
pbcentrum "github.com/fivenet-app/fivenet/gen/go/proto/services/centrum"
pbcitizenstore "github.com/fivenet-app/fivenet/gen/go/proto/services/citizenstore"
pbcompletor "github.com/fivenet-app/fivenet/gen/go/proto/services/completor"
pbdmv "github.com/fivenet-app/fivenet/gen/go/proto/services/dmv"
pbdocstore "github.com/fivenet-app/fivenet/gen/go/proto/services/docstore"
pbjobs "github.com/fivenet-app/fivenet/gen/go/proto/services/jobs"
pblivemapper "github.com/fivenet-app/fivenet/gen/go/proto/services/livemapper"
pbmessenger "github.com/fivenet-app/fivenet/gen/go/proto/services/messenger"
pbnotificator "github.com/fivenet-app/fivenet/gen/go/proto/services/notificator"
pbqualifications "github.com/fivenet-app/fivenet/gen/go/proto/services/qualifications"
pbrector "github.com/fivenet-app/fivenet/gen/go/proto/services/rector"
pbstats "github.com/fivenet-app/fivenet/gen/go/proto/services/stats"
// Modules
"github.com/fivenet-app/fivenet/gen/go/proto/services/centrum/centrumbot"
"github.com/fivenet-app/fivenet/gen/go/proto/services/centrum/centrummanager"
"github.com/fivenet-app/fivenet/gen/go/proto/services/centrum/centrumstate"
"github.com/fivenet-app/fivenet/internal/modules"
"github.com/fivenet-app/fivenet/pkg/config"
"github.com/fivenet-app/fivenet/pkg/config/appconfig"
"github.com/fivenet-app/fivenet/pkg/coords/postals"
"github.com/fivenet-app/fivenet/pkg/croner"
"github.com/fivenet-app/fivenet/pkg/discord"
"github.com/fivenet-app/fivenet/pkg/events"
"github.com/fivenet-app/fivenet/pkg/grpc"
"github.com/fivenet-app/fivenet/pkg/grpc/auth"
"github.com/fivenet-app/fivenet/pkg/grpc/auth/userinfo"
"github.com/fivenet-app/fivenet/pkg/htmlsanitizer"
"github.com/fivenet-app/fivenet/pkg/lang"
"github.com/fivenet-app/fivenet/pkg/mstlystcdata"
"github.com/fivenet-app/fivenet/pkg/notifi"
"github.com/fivenet-app/fivenet/pkg/perms"
"github.com/fivenet-app/fivenet/pkg/server"
"github.com/fivenet-app/fivenet/pkg/server/admin"
"github.com/fivenet-app/fivenet/pkg/server/api"
"github.com/fivenet-app/fivenet/pkg/server/audit"
"github.com/fivenet-app/fivenet/pkg/server/filestore"
"github.com/fivenet-app/fivenet/pkg/server/images"
"github.com/fivenet-app/fivenet/pkg/server/oauth2"
"github.com/fivenet-app/fivenet/pkg/server/wk"
"github.com/fivenet-app/fivenet/pkg/storage"
"github.com/fivenet-app/fivenet/pkg/tracker"
"github.com/fivenet-app/fivenet/query"
)
type Context struct{}
type ServerCmd struct{}
func (c *ServerCmd) Run(ctx *Context) error {
fxOpts := getFxBaseOpts(cli.StartTimeout)
fxOpts = append(fxOpts, fx.Invoke(func(server.HTTPServer) {}))
app := fx.New(fxOpts...)
app.Run()
return nil
}
type WorkerCmd struct {
ModuleAuditRetention bool `help:"Start Audit log retention module" default:"true"`
ModuleDiscordBot bool `help:"Start Discord bot module" default:"true"`
ModuleCentrumBot bool `help:"Start Centrum bot module" default:"true"`
ModuleCentrumHousekeeper bool `help:"Start Centrum Housekeeper module" default:"true"`
ModuleUserTracker bool `help:"Start User tracker module" default:"true"`
}
func (c *WorkerCmd) Run(ctx *Context) error {
fxOpts := getFxBaseOpts(cli.StartTimeout)
if c.ModuleAuditRetention {
fxOpts = append(fxOpts, fx.Invoke(func(*audit.Retention) {}))
}
if c.ModuleCentrumBot {
fxOpts = append(fxOpts, fx.Invoke(func(*centrumbot.Manager) {}))
}
if c.ModuleCentrumHousekeeper {
fxOpts = append(fxOpts, fx.Invoke(func(*centrummanager.Housekeeper) {}))
}
if c.ModuleDiscordBot {
fxOpts = append(fxOpts, fx.Invoke(func(*discord.Bot) {}))
}
if c.ModuleUserTracker {
fxOpts = append(fxOpts, fx.Invoke(func(*tracker.Manager) {}))
}
// Only run cron agent in worker
fxOpts = append(fxOpts, fx.Invoke(func(*croner.Agent) {}))
app := fx.New(fxOpts...)
app.Run()
return nil
}
var cli struct {
Config string `help:"Alternative config file (env var: FIVENET_CONFIG_FILE)"`
StartTimeout time.Duration `help:"App start timeout duration" default:"180s"`
Server ServerCmd `cmd:"" help:"Run FiveNet server."`
Worker WorkerCmd `cmd:"" help:"Run FiveNet worker."`
}
func getFxBaseOpts(startTimeout time.Duration) []fx.Option {
return []fx.Option{
fx.WithLogger(func(log *zap.Logger) fxevent.Logger {
return &fxevent.ZapLogger{Logger: log}
}),
fx.StartTimeout(startTimeout),
admin.Module,
appconfig.Module,
audit.Module,
audit.RetentionModule,
auth.AuthModule,
auth.PermsModule,
auth.TokenMgrModule,
centrumbot.Module,
config.Module,
croner.AgentModule,
croner.HandlerModule,
croner.Module,
croner.SchedulerModule,
discord.BotModule,
events.Module,
grpc.ServerModule,
htmlsanitizer.Module,
lang.Module,
centrummanager.HousekeeperModule,
centrummanager.Module,
modules.LoggerModule,
modules.TracerProviderModule,
perms.Module,
query.Module,
server.HTTPEngineModule,
server.HTTPServerModule,
centrumstate.StateModule,
storage.Module,
fx.Provide(
mstlystcdata.NewCache,
mstlystcdata.NewEnricher,
mstlystcdata.NewSearcher,
mstlystcdata.NewUserAwareEnricher,
notifi.New,
postals.New,
tracker.New,
tracker.NewManager,
userinfo.NewUIRetriever,
// HTTP Services
server.AsService(api.New),
server.AsService(filestore.New),
server.AsService(images.New),
server.AsService(oauth2.New),
server.AsService(wk.New),
),
// GRPC Services
fx.Provide(
grpc.AsService(pbauth.NewServer),
grpc.AsService(pbcalendar.NewServer),
grpc.AsService(pbcentrum.NewServer),
grpc.AsService(pbcitizenstore.NewServer),
grpc.AsService(pbcompletor.NewServer),
grpc.AsService(pbdmv.NewServer),
grpc.AsService(pbdocstore.NewServer),
grpc.AsService(pbjobs.NewServer),
grpc.AsService(pblivemapper.NewServer),
grpc.AsService(pbmessenger.NewServer),
grpc.AsService(pbnotificator.NewServer),
grpc.AsService(pbqualifications.NewServer),
grpc.AsService(pbrector.NewServer),
grpc.AsService(pbstats.NewServer),
),
fx.Invoke(func(*bluemonday.Policy) {}),
fx.Invoke(func(admin.AdminServer) {}),
fx.Invoke(func(croner.ICron) {}),
}
}
func main() {
// https://github.com/DataDog/go-profiler-notes/blob/main/block.md#overhead
// Thanks, to the authors of this document!
runtime.SetBlockProfileRate(20000)
runtime.SetMutexProfileFraction(100)
ctx := kong.Parse(&cli)
// Cli flag overrides env var
if cli.Config != "" {
if err := os.Setenv("FIVENET_CONFIG_FILE", cli.Config); err != nil {
panic(err)
}
}
if cli.StartTimeout <= 0 {
cli.StartTimeout = 180 * time.Second
}
err := ctx.Run(&Context{})
ctx.FatalIfErrorf(err)
}