-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
api.go
72 lines (56 loc) 路 1.29 KB
/
api.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
package api
import (
"context"
"fmt"
nethttp "net/http"
"github.com/labstack/echo/v4"
"github.com/taikoxyz/taiko-mono/packages/blobstorage/pkg/http"
"github.com/taikoxyz/taiko-mono/packages/blobstorage/pkg/repo"
"github.com/urfave/cli/v2"
"golang.org/x/exp/slog"
)
type API struct {
srv *http.Server
port int
}
func (a *API) InitFromCli(ctx context.Context, c *cli.Context) error {
cfg, err := NewConfigFromCliContext(c)
if err != nil {
return err
}
return InitFromConfig(ctx, a, cfg)
}
// InitFromConfig inits a new Server from a provided Config struct
func InitFromConfig(ctx context.Context, a *API, cfg *Config) (err error) {
db, err := cfg.OpenDBFunc()
if err != nil {
return err
}
blobHashRepo, err := repo.NewBlobHashRepository(db)
if err != nil {
return err
}
srv, err := http.NewServer(http.NewServerOpts{
BlobHashRepo: blobHashRepo,
Echo: echo.New(),
})
if err != nil {
return err
}
a.srv = srv
a.port = int(cfg.Port)
return nil
}
func (a *API) Start() error {
go func() {
if err := a.srv.Start(fmt.Sprintf(":%v", a.port)); err != nil && err != nethttp.ErrServerClosed {
slog.Error("error starting server", "error", err)
}
}()
return nil
}
func (a *API) Close(ctx context.Context) {
}
func (a *API) Name() string {
return "server"
}