-
Notifications
You must be signed in to change notification settings - Fork 11
/
handler.go
225 lines (178 loc) · 6.31 KB
/
handler.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
220
221
222
223
224
225
package backend
import (
"context"
"fmt"
"code.vegaprotocol.io/vega/libs/jsonrpc"
vgzap "code.vegaprotocol.io/vega/libs/zap"
"code.vegaprotocol.io/vega/paths"
walletapi "code.vegaprotocol.io/vega/wallet/api"
nodeapi "code.vegaprotocol.io/vega/wallet/api/node"
netstore "code.vegaprotocol.io/vega/wallet/network/store/v1"
wstore "code.vegaprotocol.io/vega/wallet/wallet/store/v1"
"code.vegaprotocol.io/vega/wallet/wallets"
"code.vegaprotocol.io/vegawallet-desktop/app"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Handler struct {
// This context needs to be kept, as advised by Wails documentation, as it's
// the one to inject on runtime methods like menu, event, dialogs, etc.
ctx context.Context
// appInitialised represents the initialization state of the application
// and is used to prevent calls to the API when the application is not
// initialised.
appInitialised bool
log *zap.Logger
// logLevel is a reference to the logger's log level to dynamically change
// it when the configuration is updated.
logLevel zap.AtomicLevel
configLoader *app.ConfigLoader
walletAPI *jsonrpc.API
currentService *serviceInfo
}
func NewHandler() (*Handler, error) {
h := &Handler{}
var err error
if err := h.initializeAppLogger(); err != nil {
return nil, err
}
h.configLoader, err = app.NewConfigLoader()
if err != nil {
return nil, fmt.Errorf("could not create the configuration loader: %w", err)
}
h.currentService = newServiceInfo()
h.appInitialised, err = h.isAppInitialised()
if err != nil {
return nil, fmt.Errorf("could not verify wheter the application is initialized or not: %w", err)
}
// If the application is not initialized, it means it's the first time the
// user is running the application. As a result, we can't load the backend
// components that require an existing configuration. The user will have
// to go through the application initialization process, that is part of
// the "on-boarding" workflow on the front-end.
if h.appInitialised {
if err := h.reloadBackendComponentsFromConfig(); err != nil {
return nil, fmt.Errorf("could not load the backend components during the application start up: %w", err)
}
}
return h, nil
}
// Startup is called during application startup
func (h *Handler) Startup(ctx context.Context) {
h.ctx = ctx
h.log.Debug("Entering Startup")
defer h.log.Debug("Leaving Startup")
}
// DOMReady is called after the front-end dom has been loaded
func (h *Handler) DOMReady(_ context.Context) {
// Add your action here
}
// Shutdown is called during application termination
func (h *Handler) Shutdown(_ context.Context) {
h.log.Debug("Entering Shutdown")
defer h.log.Debug("Leaving Shutdown")
_ = h.StopService()
}
func (h *Handler) initializeAppLogger() error {
appLogsDir, err := paths.CreateDefaultStateDirFor(paths.WalletAppLogsHome)
if err != nil {
return fmt.Errorf("could not create configuration file at %s: %w", paths.WalletAppDefaultConfigFile, err)
}
loggerConfig := vgzap.DefaultConfig()
loggerConfig = vgzap.WithFileOutputForDedicatedProcess(loggerConfig, appLogsDir)
loggerConfig = vgzap.WithJSONFormat(loggerConfig)
loggerConfig = vgzap.WithLevel(loggerConfig, zap.InfoLevel.String())
// We need to keep the logger level to dynamically change it.
h.logLevel = loggerConfig.Level
log, err := vgzap.Build(loggerConfig)
if err != nil {
return fmt.Errorf("could not setup the application logger: %w", err)
}
h.log = log.Named("backend")
return nil
}
func (h *Handler) reloadBackendComponentsFromConfig() error {
if h.currentService.IsRunning() {
h.currentService.Shutdown()
}
cfg, err := h.configLoader.GetConfig()
if err != nil {
return fmt.Errorf("could not load the application configuration: %w", err)
}
if err := h.updateAppLogLevel(cfg.LogLevel); err != nil {
return err
}
if err := h.initializeWalletAPI(cfg); err != nil {
return err
}
return nil
}
func (h *Handler) updateBackendComponentsFromConfig() error {
cfg, err := h.configLoader.GetConfig()
if err != nil {
return fmt.Errorf("could not load the application configuration: %w", err)
}
if cfg.LogLevel != h.logLevel.String() {
if err := h.updateAppLogLevel(cfg.LogLevel); err != nil {
return err
}
}
return nil
}
func (h *Handler) initializeWalletAPI(cfg app.Config) error {
netStore, err := h.getNetworksStore(cfg)
if err != nil {
return err
}
walletStore, err := h.getWalletsStore(cfg)
if err != nil {
return err
}
nodeSelectorBuilder := func(hosts []string, retries uint64) (nodeapi.Selector, error) {
nodeSelector, err := nodeapi.BuildRoundRobinSelectorWithRetryingNodes(h.log, hosts, retries)
if err != nil {
h.log.Error(fmt.Sprintf("Could not initialise the node selector for wallet API: %v", err))
return nil, fmt.Errorf("could not initialise the node selector for wallet API: %w", err)
}
return nodeSelector, nil
}
walletAPI, err := walletapi.AdminAPI(h.log.Named("json-api"), walletStore, netStore, nodeSelectorBuilder)
if err != nil {
h.log.Error("Could not initialize the wallet administration API", zap.Error(err))
return fmt.Errorf("could not initialize the wallet administration API: %w", err)
}
h.walletAPI = walletAPI
return nil
}
func (h *Handler) updateAppLogLevel(level string) error {
resolvedLevel, err := zapcore.ParseLevel(level)
if err != nil {
return fmt.Errorf("invalid log level: %w", err)
}
h.logLevel.SetLevel(resolvedLevel)
return nil
}
func (h *Handler) getNetworksStore(config app.Config) (*netstore.Store, error) {
st, err := netstore.InitialiseStore(paths.New(config.VegaHome))
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't initialise the networks store: %v", err))
return nil, fmt.Errorf("could not initialise the networks store: %w", err)
}
return st, nil
}
func (h *Handler) getWalletsStore(config app.Config) (*wstore.Store, error) {
store, err := wallets.InitialiseStore(config.VegaHome)
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't initialise the wallets store: %v", err))
return nil, fmt.Errorf("could not initialise the wallets store: %w", err)
}
return store, nil
}
func (h *Handler) appConfig() (app.Config, error) {
c, err := h.configLoader.GetConfig()
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't load the application configuration: %v", err))
return app.Config{}, fmt.Errorf("could not load the application configuration: %w", err)
}
return c, nil
}