-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
243 lines (220 loc) · 5.72 KB
/
cmd.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
242
243
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/filecoin-project/lotus/build"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/node"
logging "github.com/ipfs/go-log/v2"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli/v2"
)
var authCmd = &cli.Command{
Name: "auth",
Usage: "manage API authentication",
Subcommands: []*cli.Command{
authCreateTokenCmd,
authVerifyCmd,
},
}
var authCreateTokenCmd = &cli.Command{
Name: "create-token",
Usage: "create a new API token",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "secret",
Usage: "specify the secret to use for API authentication",
Required: true,
},
&cli.StringFlag{
Name: "user",
Usage: "specify the user to associate with the token",
Required: true,
},
&cli.TimestampFlag{
Name: "expiry",
Usage: "specify the expiry time of the token",
Layout: time.RFC3339Nano,
},
},
Action: func(cctx *cli.Context) error {
var d time.Duration
if cctx.IsSet("expiry") {
expiry := cctx.Timestamp("expiry")
d = time.Until(*expiry)
}
token, err := authNew([]byte(cctx.String("secret")),
cctx.String("user"), d)
if err != nil {
return err
}
fmt.Println("token created successfully:")
fmt.Println(token)
return nil
},
}
var authVerifyCmd = &cli.Command{
Name: "verify",
Usage: "verify an API token",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "secret",
Usage: "specify the secret to use for API authentication",
Required: true,
},
&cli.StringFlag{
Name: "token",
Usage: "specify the token to verify",
Required: true,
},
},
Action: func(cctx *cli.Context) error {
user, err := authVerify([]byte(cctx.String("secret")), cctx.String("token"))
if err != nil {
return err
}
fmt.Println("token verified successfully: ", user)
return nil
},
}
var runCmd = &cli.Command{
Name: "run",
Usage: "run the extend service",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen",
Value: "127.0.0.1:8000",
Usage: "specify the address to listen on",
},
&cli.StringFlag{
Name: "db",
Value: "sqlite3:extend.db",
Usage: "specify the database URL to use, support sqlite3, mysql, postgres, https://github.com/xo/dburl?tab=readme-ov-file#example-urls",
},
&cli.StringFlag{
Name: "secret",
Usage: "specify the secret to use for API authentication, if not set, no auth will be enabled",
},
&cli.DurationFlag{
Name: "max-wait",
Usage: "[Warning] specify the maximum time to wait for messages on chain, otherwise try to replace them, only use this if you know what you are doing",
},
&cli.Int64Flag{
Name: "batch-speedup",
Usage: "specify the number of messages to speed up in a single batch",
Value: 10,
},
&cli.BoolFlag{
Name: "debug",
Value: false,
Usage: "enable debug logging",
},
},
Before: func(cctx *cli.Context) error {
if cctx.Bool("debug") {
_ = logging.SetLogLevel("extend", "debug")
} else {
_ = logging.SetLogLevel("extend", "info")
}
return nil
},
Action: func(cctx *cli.Context) error {
dbPath, err := homedir.Expand(cctx.String("db"))
if err != nil {
return fmt.Errorf("failed to expand db path: %w", err)
}
db, err := NewDB(dbPath)
if err != nil {
return err
}
fullApi, nCloser, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer nCloser()
ctx := lcli.ReqContext(cctx)
gtp, err := fullApi.ChainGetGenesis(ctx)
if err != nil {
return err
}
genesisTime := time.Unix(int64(gtp.MinTimestamp()), 0)
SetupGenesisTime(genesisTime)
nn, err := fullApi.StateNetworkName(ctx)
if err != nil {
return err
}
if err := build.UseNetworkBundle(string(nn)); err != nil {
return err
}
head, err := fullApi.ChainHead(ctx)
if err != nil {
return err
}
log.Infow("connected to lotus node",
"network", nn, "head", head.Height(), "genesis", genesisTime)
var secret []byte
var authStatus = "disabled"
if cctx.IsSet("secret") {
secret = []byte(cctx.String("secret"))
authStatus = "enabled"
}
service := NewService(ctx, db, fullApi, cctx.Duration("max-wait"), cctx.Int64("batch-speedup"))
srv := &http.Server{
Handler: NewRouter(service, secret),
Addr: cctx.String("listen"),
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
go func() {
log.Infof("starting API server at %s, authentication is %s", srv.Addr, authStatus)
if err := srv.ListenAndServe(); err != nil {
log.Error(err)
}
}()
// Monitor for shutdown.
finishCh := MonitorShutdown(
node.ShutdownHandler{Component: "api", StopFunc: srv.Shutdown},
node.ShutdownHandler{Component: "service", StopFunc: service.Shutdown},
)
<-finishCh
return nil
},
}
var testCmd = &cli.Command{
Name: "test",
Usage: "test some stuff in the context of the extend service",
Hidden: true,
Action: func(cctx *cli.Context) error {
return nil
},
}
func MonitorShutdown(handlers ...node.ShutdownHandler) <-chan struct{} {
sigCh := make(chan os.Signal, 2)
out := make(chan struct{})
go func() {
sig := <-sigCh
log.Warnw("received shutdown", "signal", sig)
log.Warn("Shutting down...")
// Call all the handlers, logging on failure and success.
for _, h := range handlers {
if err := h.StopFunc(context.TODO()); err != nil {
log.Errorf("shutting down %s failed: %s", h.Component, err)
continue
}
log.Infof("%s shut down successfully ", h.Component)
}
log.Warn("Graceful shutdown successful")
// Sync all loggers.
_ = log.Sync() //nolint:errcheck
close(out)
}()
signal.Reset(syscall.SIGTERM, syscall.SIGINT)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
return out
}