-
Notifications
You must be signed in to change notification settings - Fork 11
/
service.go
199 lines (166 loc) · 5.06 KB
/
service.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
package backend
import (
"context"
"errors"
"fmt"
"net/http"
"code.vegaprotocol.io/shared/paths"
"code.vegaprotocol.io/vegawallet/network"
netstore "code.vegaprotocol.io/vegawallet/network/store/v1"
"code.vegaprotocol.io/vegawallet/node"
"code.vegaprotocol.io/vegawallet/service"
"code.vegaprotocol.io/vegawallet/wallets"
)
type StartServiceRequest struct {
Network string `json:"network"`
}
func (r StartServiceRequest) Check() error {
if len(r.Network) == 0 {
return errors.New("network is required")
}
return nil
}
func (h *Handler) StartService(req *StartServiceRequest) (bool, error) {
h.log.Debug("Entering StartService")
defer h.log.Debug("Leaving StartService")
if h.service.IsRunning() {
h.log.Error("The service is already running")
return false, ErrServiceAlreadyRunning
}
if err := req.Check(); err != nil {
return false, err
}
config, err := h.loadAppConfig()
if err != nil {
return false, err
}
wStore, err := h.getWalletsStore(config)
if err != nil {
return false, err
}
handler := wallets.NewHandler(wStore)
netStore, err := netstore.InitialiseStore(paths.New(config.VegaHome))
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't initialise network store: %v", err))
return false, fmt.Errorf("couldn't initialise network store: %w", err)
}
exists, err := netStore.NetworkExists(req.Network)
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't verify the network existence: %v", err))
return false, fmt.Errorf("couldn't verify the network existence: %w", err)
}
if !exists {
h.log.Error(fmt.Sprintf("Network %s does not exist", req.Network))
return false, network.NewNetworkDoesNotExistError(req.Network)
}
cfg, err := getNetwork(netStore, req.Network)
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't initialise network store: %v", err))
return false, fmt.Errorf("couldn't initialise network store: %w", err)
}
logLevel := cfg.Level.String()
log, err := buildLogger(logLevel, h.configLoader.LogFilePathForSvc())
if err != nil {
return false, err
}
defer syncLogger(log)
svcStore, err := h.getServiceStore(config)
if err != nil {
return false, err
}
isInit, err := service.IsInitialised(svcStore)
if err != nil {
return false, fmt.Errorf("couldn't verify service initialisation state: %w", err)
}
if !isInit {
if err = service.InitialiseService(svcStore, false); err != nil {
return false, fmt.Errorf("couldn't initialise the service: %w", err)
}
}
auth, err := service.NewAuth(log.Named("auth"), svcStore, cfg.TokenExpiry.Get())
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't initialise authentication: %v", err))
return false, fmt.Errorf("couldn't initialise authentication: %w", err)
}
forwarder, err := node.NewForwarder(log.Named("forwarder"), cfg.API.GRPC)
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't initialise the node forwarder: %v", err))
return false, fmt.Errorf("couldn't initialise the node forwarder: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
loggingCancelFn := func() {
log.Info("Triggering cancel function")
cancel()
}
policy := service.NewExplicitConsentPolicy(ctx, h.service.ConsentRequestsChan, h.service.SentTransactionsChan)
srv, err := service.NewService(log.Named("service"), cfg, handler, auth, forwarder, policy)
if err != nil {
h.log.Error(fmt.Sprintf("Couldn't initialise the service: %v", err))
loggingCancelFn()
return false, err
}
h.service.Set(
fmt.Sprintf("%s:%v", cfg.Host, cfg.Port),
func() {
loggingCancelFn()
if err := srv.Stop(); err != nil {
h.log.Error(fmt.Sprintf("Couldn't stop the service: %v", err))
}
},
)
go func() {
h.log.Info("Starting to listen to pending request channel")
for {
select {
case <-ctx.Done():
h.log.Info("Stop listening to channels")
return
case consentRequest, ok := <-h.service.ConsentRequestsChan:
if !ok {
return
}
h.emitNewConsentRequestEvent(consentRequest)
case sentTransaction, ok := <-h.service.SentTransactionsChan:
if !ok {
return
}
h.emitTransactionSentEvent(sentTransaction)
}
}
}()
go func() {
h.log.Info("Starting the service")
if err := srv.Start(); err != nil && !errors.Is(err, http.ErrServerClosed) {
h.log.Error(fmt.Sprintf("Error while starting HTTP server: %v", err))
loggingCancelFn()
h.service.Reset()
h.log.Info("Service state has been reset")
}
}()
return true, nil
}
type GetServiceStateResponse struct {
URL string `json:"url"`
Running bool `json:"running"`
}
func (h *Handler) GetServiceState() GetServiceStateResponse {
h.log.Debug("Entering GetServiceState")
defer h.log.Debug("Leaving GetServiceState")
return GetServiceStateResponse{
URL: h.service.URL(),
Running: h.service.IsRunning(),
}
}
func (h *Handler) StopService() (bool, error) {
h.log.Debug("Entering StopService")
defer h.log.Debug("Leaving StopService")
if !h.service.IsRunning() {
h.log.Error("No service running")
return false, ErrServiceNotRunning
}
h.log.Info("Stopping the service")
h.service.Shutdown()
h.service.Reset()
h.log.Info("Service stopped")
return true, nil
}