-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(relayer, eventindexer): move code to pkg folder, use new api sub…
…command for eventindexer/relayer (#15502)
- Loading branch information
1 parent
47b07bb
commit 99dd1d2
Showing
140 changed files
with
710 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
|
||
nethttp "net/http" | ||
|
||
"github.com/ethereum/go-ethereum/ethclient" | ||
"github.com/labstack/echo/v4" | ||
"github.com/taikoxyz/taiko-mono/packages/eventindexer/pkg/http" | ||
"github.com/taikoxyz/taiko-mono/packages/eventindexer/pkg/repo" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type API struct { | ||
httpPort uint64 | ||
srv *http.Server | ||
|
||
ctx context.Context | ||
} | ||
|
||
func (api *API) Start() error { | ||
api.ctx = context.Background() | ||
go func() { | ||
if err := api.srv.Start(fmt.Sprintf(":%v", api.httpPort)); err != nethttp.ErrServerClosed { | ||
slog.Error("http srv start", "error", err.Error()) | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func (api *API) Name() string { | ||
return "api" | ||
} | ||
|
||
func (api *API) InitFromCli(ctx context.Context, c *cli.Context) error { | ||
cfg, err := NewConfigFromCliContext(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return InitFromConfig(ctx, api, cfg) | ||
} | ||
|
||
// nolint: funlen | ||
func InitFromConfig(ctx context.Context, api *API, cfg *Config) error { | ||
db, err := cfg.OpenDBFunc() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
eventRepository, err := repo.NewEventRepository(db) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
chartRepository, err := repo.NewChartRepository(db) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
statRepository, err := repo.NewStatRepository(db) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nftBalanceRepository, err := repo.NewNFTBalanceRepository(db) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ethClient, err := ethclient.Dial(cfg.RPCUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
srv, err := http.NewServer(http.NewServerOpts{ | ||
EventRepo: eventRepository, | ||
StatRepo: statRepository, | ||
NFTBalanceRepo: nftBalanceRepository, | ||
ChartRepo: chartRepository, | ||
Echo: echo.New(), | ||
CorsOrigins: cfg.CORSOrigins, | ||
EthClient: ethClient, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
api.srv = srv | ||
api.httpPort = cfg.HTTPPort | ||
|
||
return nil | ||
} | ||
|
||
func (api *API) Close(ctx context.Context) { | ||
if err := api.srv.Shutdown(ctx); err != nil { | ||
slog.Error("srv shutdown", "error", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package api | ||
|
||
import ( | ||
"database/sql" | ||
"strings" | ||
|
||
"github.com/taikoxyz/taiko-mono/packages/eventindexer/cmd/flags" | ||
"github.com/taikoxyz/taiko-mono/packages/eventindexer/pkg/db" | ||
"github.com/urfave/cli/v2" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
type DB interface { | ||
DB() (*sql.DB, error) | ||
GormDB() *gorm.DB | ||
} | ||
|
||
type Config struct { | ||
// db configs | ||
DatabaseUsername string | ||
DatabasePassword string | ||
DatabaseName string | ||
DatabaseHost string | ||
DatabaseMaxIdleConns uint64 | ||
DatabaseMaxOpenConns uint64 | ||
DatabaseMaxConnLifetime uint64 | ||
RPCUrl string | ||
HTTPPort uint64 | ||
MetricsHTTPPort uint64 | ||
ETHClientTimeout uint64 | ||
CORSOrigins []string | ||
OpenDBFunc func() (DB, error) | ||
} | ||
|
||
// NewConfigFromCliContext creates a new config instance from command line flags. | ||
func NewConfigFromCliContext(c *cli.Context) (*Config, error) { | ||
cors := make([]string, 0) | ||
|
||
cors = append(cors, strings.Split(c.String(flags.CORSOrigins.Name), ",")...) | ||
|
||
return &Config{ | ||
DatabaseUsername: c.String(flags.DatabaseUsername.Name), | ||
DatabasePassword: c.String(flags.DatabasePassword.Name), | ||
DatabaseName: c.String(flags.DatabaseName.Name), | ||
DatabaseHost: c.String(flags.DatabaseHost.Name), | ||
DatabaseMaxIdleConns: c.Uint64(flags.DatabaseMaxIdleConns.Name), | ||
DatabaseMaxOpenConns: c.Uint64(flags.DatabaseMaxOpenConns.Name), | ||
DatabaseMaxConnLifetime: c.Uint64(flags.DatabaseConnMaxLifetime.Name), | ||
HTTPPort: c.Uint64(flags.HTTPPort.Name), | ||
MetricsHTTPPort: c.Uint64(flags.MetricsHTTPPort.Name), | ||
CORSOrigins: cors, | ||
RPCUrl: c.String(flags.APIRPCUrl.Name), | ||
OpenDBFunc: func() (DB, error) { | ||
return db.OpenDBConnection(db.DBConnectionOpts{ | ||
Name: c.String(flags.DatabaseUsername.Name), | ||
Password: c.String(flags.DatabasePassword.Name), | ||
Database: c.String(flags.DatabaseName.Name), | ||
Host: c.String(flags.DatabaseHost.Name), | ||
MaxIdleConns: c.Uint64(flags.DatabaseMaxIdleConns.Name), | ||
MaxOpenConns: c.Uint64(flags.DatabaseMaxOpenConns.Name), | ||
MaxConnLifetime: c.Uint64(flags.DatabaseConnMaxLifetime.Name), | ||
OpenFunc: func(dsn string) (*db.DB, error) { | ||
gormDB, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ | ||
Logger: logger.Default.LogMode(logger.Silent), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return db.New(gormDB), nil | ||
}, | ||
}) | ||
}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package api | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/taikoxyz/taiko-mono/packages/eventindexer/cmd/flags" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var ( | ||
httpPort = "1000" | ||
metricsHttpPort = "1001" | ||
corsOrigins = "http://localhost:3000,http://localhost:3001" | ||
databaseMaxIdleConns = "10" | ||
databaseMaxOpenConns = "10" | ||
databaseMaxConnLifetime = "30" | ||
) | ||
|
||
func setupApp() *cli.App { | ||
app := cli.NewApp() | ||
app.Flags = flags.APIFlags | ||
app.Action = func(ctx *cli.Context) error { | ||
_, err := NewConfigFromCliContext(ctx) | ||
return err | ||
} | ||
|
||
return app | ||
} | ||
|
||
func TestNewConfigFromCliContext(t *testing.T) { | ||
app := setupApp() | ||
|
||
app.Action = func(ctx *cli.Context) error { | ||
c, err := NewConfigFromCliContext(ctx) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, "dbuser", c.DatabaseUsername) | ||
assert.Equal(t, "dbpass", c.DatabasePassword) | ||
assert.Equal(t, "dbname", c.DatabaseName) | ||
assert.Equal(t, "dbhost", c.DatabaseHost) | ||
assert.Equal(t, "rpcUrl", c.RPCUrl) | ||
assert.Equal(t, uint64(1000), c.HTTPPort) | ||
assert.Equal(t, uint64(1001), c.MetricsHTTPPort) | ||
assert.Equal(t, uint64(10), c.DatabaseMaxIdleConns) | ||
assert.Equal(t, uint64(10), c.DatabaseMaxOpenConns) | ||
assert.Equal(t, uint64(30), c.DatabaseMaxConnLifetime) | ||
assert.NotNil(t, c.OpenDBFunc) | ||
|
||
return err | ||
} | ||
|
||
assert.Nil(t, app.Run([]string{ | ||
"TestNewConfigFromCliContext", | ||
"--" + flags.DatabaseUsername.Name, "dbuser", | ||
"--" + flags.DatabasePassword.Name, "dbpass", | ||
"--" + flags.DatabaseHost.Name, "dbhost", | ||
"--" + flags.DatabaseName.Name, "dbname", | ||
"--" + flags.APIRPCUrl.Name, "rpcUrl", | ||
"--" + flags.HTTPPort.Name, httpPort, | ||
"--" + flags.MetricsHTTPPort.Name, metricsHttpPort, | ||
"--" + flags.CORSOrigins.Name, corsOrigins, | ||
"--" + flags.DatabaseMaxIdleConns.Name, databaseMaxIdleConns, | ||
"--" + flags.DatabaseMaxOpenConns.Name, databaseMaxOpenConns, | ||
"--" + flags.DatabaseConnMaxLifetime.Name, databaseMaxConnLifetime, | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package flags | ||
|
||
import "github.com/urfave/cli/v2" | ||
|
||
// required flags | ||
var ( | ||
APIRPCUrl = &cli.StringFlag{ | ||
Name: "rpcUrl", | ||
Usage: "RPC URL for the source chain", | ||
Required: true, | ||
Category: commonCategory, | ||
EnvVars: []string{"RPC_URL"}, | ||
} | ||
) | ||
|
||
// optional flags | ||
var ( | ||
HTTPPort = &cli.Uint64Flag{ | ||
Name: "http.port", | ||
Usage: "Port to run http server on", | ||
Category: indexerCategory, | ||
Required: false, | ||
Value: 4102, | ||
EnvVars: []string{"HTTP_PORT"}, | ||
} | ||
CORSOrigins = &cli.StringFlag{ | ||
Name: "http.corsOrigins", | ||
Usage: "Comma-delinated list of cors origins", | ||
Required: false, | ||
Value: "*", | ||
Category: indexerCategory, | ||
} | ||
) | ||
|
||
var APIFlags = MergeFlags(CommonFlags, []cli.Flag{ | ||
APIRPCUrl, | ||
HTTPPort, | ||
CORSOrigins, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
99dd1d2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
bridge-ui-v2-internal – ./packages/bridge-ui-v2
bridge-ui-v2-internal-git-alpha-6-taikoxyz.vercel.app
bridge-ui-v2-internal-taikoxyz.vercel.app
bridge-ui-v2-internal.vercel.app
99dd1d2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
bridge-ui-v2-a6 – ./packages/bridge-ui-v2
bridge-ui-v2-a6-taikoxyz.vercel.app
bridge-ui-v2-a6-git-alpha-6-taikoxyz.vercel.app
bridge-ui-v2-a6.vercel.app
bridge.katla.taiko.xyz