/
run.go
134 lines (113 loc) · 3.42 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/metadium/rosetta-metadium/configuration"
"github.com/metadium/rosetta-metadium/metadium"
"github.com/metadium/rosetta-metadium/services"
"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 = 6000 * 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 = 20000 * time.Second
// idleTimeout is the maximum amount of time to wait for the
// next request when keep-alives are enabled.
idleTimeout = 20000 * time.Second
)
var (
runCmd = &cobra.Command{
Use: "run",
Short: "Run rosetta-metadium",
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(
metadium.OperationTypes,
metadium.HistoricalBalanceSupported,
[]*types.NetworkIdentifier{cfg.Network},
metadium.CallMethods,
metadium.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 *metadium.Client
if cfg.Mode == configuration.Online {
if !cfg.RemoteGmet {
g.Go(func() error {
return metadium.StartGmet(ctx, cfg.GmetArguments, g)
})
}
var err error
client, err = metadium.NewClient(cfg.GmetURL, cfg.Params, cfg.SkipGmetAdmin)
if err != nil {
return fmt.Errorf("%w: cannot initialize metadium 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-metadium halted")
}
return err
}