-
Notifications
You must be signed in to change notification settings - Fork 11
/
handler.go
241 lines (203 loc) · 6.56 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package backend
import (
"context"
"errors"
"fmt"
"code.vegaprotocol.io/shared/paths"
"code.vegaprotocol.io/vegawallet-desktop/backend/config"
"code.vegaprotocol.io/vegawallet-desktop/backend/proxy"
"code.vegaprotocol.io/vegawallet-desktop/backend/service"
"code.vegaprotocol.io/vegawallet/network"
netstore "code.vegaprotocol.io/vegawallet/network/store/v1"
svcstore "code.vegaprotocol.io/vegawallet/service/store/v1"
wstore "code.vegaprotocol.io/vegawallet/wallet/store/v1"
"code.vegaprotocol.io/vegawallet/wallets"
"go.uber.org/zap"
)
var (
ErrServiceAlreadyRunning = errors.New("the service is already running")
ErrServiceNotRunning = errors.New("the service is not running")
ErrConsoleAlreadyRunning = errors.New("the console proxy is already running")
ErrConsoleNotRunning = errors.New("the console proxy is not running")
ErrTokenDAppAlreadyRunning = errors.New("the token dApp proxy is already running")
ErrTokenDAppNotRunning = errors.New("the token dApp proxy is not running")
)
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
log *zap.Logger
configLoader *config.Loader
service *service.State
console *proxy.State
tokenDApp *proxy.State
}
func NewHandler() (*Handler, error) {
loader, err := config.NewLoader()
if err != nil {
return nil, fmt.Errorf("couldn't create configuration loader: %w", err)
}
var logLevel string
if cfg, err := loader.GetConfig(); err != nil {
logLevel = zap.InfoLevel.String()
} else {
logLevel = cfg.LogLevel
}
log, err := buildLogger(logLevel, loader.LogFilePathForApp())
if err != nil {
return nil, err
}
return &Handler{
log: log,
configLoader: loader,
}, nil
}
// Startup is called at application Startup
func (h *Handler) Startup(ctx context.Context) {
h.ctx = ctx
h.log.Debug("Entering Startup")
defer h.log.Debug("Leaving Startup")
h.service = service.NewState()
h.console = proxy.NewState()
h.tokenDApp = proxy.NewState()
}
// DOMReady is called after the front-end dom has been loaded
func (h *Handler) DOMReady(ctx context.Context) {
// Add your action here
}
// Shutdown is called at application termination
func (h *Handler) Shutdown(_ context.Context) {
h.log.Debug("Entering Shutdown")
defer h.log.Debug("Leaving Shutdown")
_, _ = h.StopConsole()
_, _ = h.StopTokenDApp()
_, _ = h.StopService()
}
func (h *Handler) IsAppInitialised() (bool, error) {
isConfigInit, err := h.configLoader.IsConfigInitialised()
if err != nil {
h.log.Error("Could not verify the application is initialized", zap.Error(err))
return false, fmt.Errorf("could not verify the application is initialized: %w", err)
}
if !isConfigInit {
return false, nil
}
cfg, err := h.configLoader.GetConfig()
if err != nil {
return false, err
}
netStore, err := h.getNetworksStore(cfg)
if err != nil {
return false, err
}
for _, defNet := range DefaultNetworks {
exist, err := netStore.NetworkExists(defNet.Name)
if err != nil {
continue
}
if !exist {
res, err := network.ImportNetworkFromSource(netStore, network.NewReaders(), &network.ImportNetworkFromSourceRequest{
URL: defNet.URL,
})
if err != nil {
h.log.Error("Could not import a network",
zap.String("network", defNet.Name),
zap.Error(err),
)
}
h.log.Info("Successfully Imported the network",
zap.String("network", res.Name),
zap.String("path", res.FilePath),
)
}
}
return true, nil
}
type InitialiseAppRequest struct {
VegaHome string `json:"vegaHome"`
}
func (h *Handler) InitialiseApp(req *InitialiseAppRequest) error {
h.log.Debug("Entering InitialiseApp")
defer h.log.Debug("Leaving InitialiseApp")
cfg := &config.Config{
LogLevel: zap.InfoLevel.String(),
VegaHome: req.VegaHome,
// We will opt in first. We will remove this once the on-boarding
// workflow is rework to ask for user explicit consent.
Telemetry: &config.TelemetryConfig{
ConsentAsked: false,
Enabled: true,
},
OnBoardingDone: true,
}
if err := h.configLoader.SaveConfig(*cfg); err != nil {
return err
}
netStore, err := h.getNetworksStore(*cfg)
if err != nil {
return err
}
existingNets, err := netStore.ListNetworks()
if err != nil {
return fmt.Errorf("could not list existing network: %w", err)
}
h.log.Info("Delete all existing network to ensure clean state")
for _, existingNet := range existingNets {
if err := netStore.DeleteNetwork(existingNet); err != nil {
h.log.Error("Could not delete an existing network during initialization",
zap.String("network", existingNet),
zap.Error(err),
)
}
h.log.Info("Successfully deleted the network", zap.String("network", existingNet))
}
h.log.Info("Import networks for which this software is optimized")
for _, defaultNetwork := range DefaultNetworks {
res, err := network.ImportNetworkFromSource(netStore, network.NewReaders(), &network.ImportNetworkFromSourceRequest{
URL: defaultNetwork.URL,
})
if err != nil {
h.log.Error("Could not import a network during initialization",
zap.String("network", defaultNetwork.Name),
zap.Error(err),
)
}
h.log.Info("Successfully imported the network",
zap.String("network", res.Name),
zap.String("path", res.FilePath),
)
}
return nil
}
func (h *Handler) getServiceStore(config config.Config) (*svcstore.Store, error) {
st, err := svcstore.InitialiseStore(paths.New(config.VegaHome))
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't initialise the service store: %v", err))
return nil, fmt.Errorf("couldn't initialise the service store: %w", err)
}
return st, nil
}
func (h *Handler) getNetworksStore(config config.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("couldn't initialise the networks store: %w", err)
}
return st, nil
}
func (h *Handler) getWalletsStore(config config.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("couldn't initialise the wallets store: %w", err)
}
return store, nil
}
func (h *Handler) loadAppConfig() (config.Config, error) {
c, err := h.configLoader.GetConfig()
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't load configuration: %v", err))
return config.Config{}, fmt.Errorf("couldn't load configuration: %w", err)
}
return c, nil
}