-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
145 lines (119 loc) · 3.77 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
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
package main
import (
"context"
"errors"
"fmt"
"runtime/debug"
"github.com/sirupsen/logrus"
"github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/catake-com/multibase/backend/handler"
"github.com/catake-com/multibase/backend/module/grpc"
"github.com/catake-com/multibase/backend/module/kafka"
"github.com/catake-com/multibase/backend/module/kubernetes"
"github.com/catake-com/multibase/backend/module/project"
"github.com/catake-com/multibase/backend/module/thrift"
"github.com/catake-com/multibase/backend/state"
)
type App struct {
initialErr error
ctx context.Context
appLogger *logrus.Logger
stateStorage *state.Storage
ProjectModule *project.Module
GRPCModule *grpc.Module
ThriftModule *thrift.Module
KafkaModule *kafka.Module
KubernetesModule *kubernetes.Module
ProjectHandler *handler.ProjectHandler
}
// nolint: nonamedreturns, funlen
func NewApp(appLogger *logrus.Logger) (app *App) {
// initialize empty structs so that the corresponding methods are still correctly bound in JS in case of error
app = &App{
GRPCModule: &grpc.Module{},
ThriftModule: &thrift.Module{},
KafkaModule: &kafka.Module{},
KubernetesModule: &kubernetes.Module{},
ProjectHandler: &handler.ProjectHandler{},
}
defer func() {
if r := recover(); r != nil {
// nolint: goerr113
app.initialErr = errors.Join(
app.initialErr,
fmt.Errorf("recovered a panic: %s, stacktrace:\n%s", r, string(debug.Stack())),
)
}
}()
stateStorage, err := state.NewStorage(appLogger.WithField("component", "storage"))
if err != nil {
app.initialErr = errors.Join(app.initialErr, fmt.Errorf("failed to init a storage: %w", err))
}
projectModule, err := project.NewModule(stateStorage)
if err != nil {
app.initialErr = errors.Join(app.initialErr, fmt.Errorf("failed to init a project module: %w", err))
}
grpcModule, err := grpc.NewModule(stateStorage)
if err != nil {
app.initialErr = errors.Join(app.initialErr, fmt.Errorf("failed to init a grpc module: %w", err))
}
thriftModule, err := thrift.NewModule(stateStorage)
if err != nil {
app.initialErr = errors.Join(app.initialErr, fmt.Errorf("failed to init a thrift module: %w", err))
}
kafkaModule, err := kafka.NewModule(stateStorage, appLogger)
if err != nil {
app.initialErr = errors.Join(app.initialErr, fmt.Errorf("failed to init a kafka module: %w", err))
}
kubernetesModule, err := kubernetes.NewModule(stateStorage, appLogger)
if err != nil {
app.initialErr = errors.Join(app.initialErr, fmt.Errorf("failed to init a kubernetes module: %w", err))
}
projectHandler := handler.NewProjectHandler(
projectModule,
grpcModule,
thriftModule,
kubernetesModule,
kafkaModule,
)
app.appLogger = appLogger
app.stateStorage = stateStorage
app.ProjectModule = projectModule
app.GRPCModule = grpcModule
app.ThriftModule = thriftModule
app.KafkaModule = kafkaModule
app.KubernetesModule = kubernetesModule
app.ProjectHandler = projectHandler
return app
}
func (a *App) startup(ctx context.Context) {
if a.initialErr != nil {
_, _ = runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Multibase failed to start correctly",
Message: fmt.Sprintf("%+v", a.initialErr),
})
runtime.Quit(ctx)
}
a.ctx = ctx
a.GRPCModule.AppCtx = ctx
a.ThriftModule.AppCtx = ctx
a.KafkaModule.AppCtx = ctx
a.KubernetesModule.AppCtx = ctx
}
func (a *App) domReady(_ context.Context) {
}
func (a *App) beforeClose(_ context.Context) bool {
if err := a.stateStorage.Close(); err != nil {
a.appLogger.Errorln(err)
}
if err := a.KafkaModule.Close(); err != nil {
a.appLogger.Errorln(err)
}
if err := a.KubernetesModule.Close(); err != nil {
a.appLogger.Errorln(err)
}
return false
}
func (a *App) shutdown(_ context.Context) {
}