/
run.go
119 lines (99 loc) · 2.77 KB
/
run.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
package cmd
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/azbuky/rosetta-vite/configuration"
"github.com/azbuky/rosetta-vite/services"
"github.com/azbuky/rosetta-vite/vite"
"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
const (
// readTimeout is the maximum duration for reading the entire
// request, including the body.
readTimeout = 5 * time.Second
// writeTimeout is the maximum duration before timing out
// writes of the response. It is reset whenever a new
// request's header is read.
writeTimeout = 120 * time.Second
// idleTimeout is the maximum amount of time to wait for the
// next request when keep-alives are enabled.
idleTimeout = 30 * time.Second
)
var (
runCmd = &cobra.Command{
Use: "run",
Short: "Run rosetta-vite",
RunE: runRunCmd,
}
)
func runRunCmd(cmd *cobra.Command, args []string) error {
cfg, err := configuration.LoadConfiguration()
if err != nil {
return fmt.Errorf("%w: unable to load configuration", err)
}
// The asserter automatically rejects incorrectly formatted
// requests.
asserter, err := asserter.NewServer(
vite.OperationTypes,
vite.HistoricalBalanceSupported,
[]*types.NetworkIdentifier{cfg.Network},
vite.CallMethods,
vite.IncludeMempoolCoins,
)
if err != nil {
return fmt.Errorf("%w: could not initialize server asserter", err)
}
// Start required services
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
go handleSignals([]context.CancelFunc{cancel})
g, ctx := errgroup.WithContext(ctx)
var client *vite.Client
if cfg.Mode == configuration.Online {
if !cfg.RemoteGvite {
g.Go(func() error {
return vite.StartGvite(ctx, cfg.GviteArguments, g)
})
}
var err error
client, err = vite.NewClient(cfg.GviteURL, cfg.InlineTransactions)
if err != nil {
return fmt.Errorf("%w: cannot initialize vite client", err)
}
defer client.Close()
}
router := services.NewBlockchainRouter(cfg, client, asserter)
loggedRouter := server.LoggerMiddleware(router)
corsRouter := server.CorsMiddleware(loggedRouter)
server := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
Handler: corsRouter,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
IdleTimeout: idleTimeout,
}
g.Go(func() error {
log.Printf("server listening on port %d", cfg.Port)
return server.ListenAndServe()
})
g.Go(func() error {
// If we don't shutdown server in errgroup, it will
// never stop because server.ListenAndServe doesn't
// take any context.
<-ctx.Done()
return server.Shutdown(ctx)
})
err = g.Wait()
if SignalReceived {
return errors.New("rosetta-vite halted")
}
return err
}