Skip to content

Commit 13e1849

Browse files
authored
Feat/cors (#42)
* add cors server config * bug when deleting future withdrawals
1 parent 05f8a32 commit 13e1849

File tree

8 files changed

+73
-27
lines changed

8 files changed

+73
-27
lines changed

bot/bot.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/initia-labs/opinit-bots/db"
1616
"github.com/initia-labs/opinit-bots/executor"
1717
executortypes "github.com/initia-labs/opinit-bots/executor/types"
18-
"github.com/initia-labs/opinit-bots/server"
1918
)
2019

2120
func LoadJsonConfig(path string, config bottypes.Config) error {
@@ -46,7 +45,6 @@ func NewBot(botType bottypes.BotType, logger *zap.Logger, homePath string, confi
4645
if err != nil {
4746
return nil, err
4847
}
49-
server := server.NewServer()
5048

5149
switch botType {
5250
case bottypes.BotTypeExecutor:
@@ -55,14 +53,14 @@ func NewBot(botType bottypes.BotType, logger *zap.Logger, homePath string, confi
5553
if err != nil {
5654
return nil, err
5755
}
58-
return executor.NewExecutor(cfg, db, server, logger.Named("executor"), homePath), nil
56+
return executor.NewExecutor(cfg, db, logger.Named("executor"), homePath), nil
5957
case bottypes.BotTypeChallenger:
6058
cfg := &challengertypes.Config{}
6159
err := LoadJsonConfig(configPath, cfg)
6260
if err != nil {
6361
return nil, err
6462
}
65-
return challenger.NewChallenger(cfg, db, server, logger.Named("challenger"), homePath), nil
63+
return challenger.NewChallenger(cfg, db, logger.Named("challenger"), homePath), nil
6664
}
6765
return nil, errors.New("not providing bot name")
6866
}

challenger/challenger.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Challenger struct {
4646
latestChallenges []challengertypes.Challenge
4747
}
4848

49-
func NewChallenger(cfg *challengertypes.Config, db types.DB, sv *server.Server, logger *zap.Logger, homePath string) *Challenger {
49+
func NewChallenger(cfg *challengertypes.Config, db types.DB, logger *zap.Logger, homePath string) *Challenger {
5050
err := cfg.Validate()
5151
if err != nil {
5252
panic(err)
@@ -67,7 +67,7 @@ func NewChallenger(cfg *challengertypes.Config, db types.DB, sv *server.Server,
6767

6868
cfg: cfg,
6969
db: db,
70-
server: sv,
70+
server: server.NewServer(cfg.Server),
7171
logger: logger,
7272

7373
homePath: homePath,
@@ -162,7 +162,7 @@ func (c *Challenger) Start(ctx context.Context) error {
162162
defer func() {
163163
c.logger.Info("api server stopped")
164164
}()
165-
return c.server.Start(c.cfg.ListenAddress)
165+
return c.server.Start()
166166
})
167167

168168
errGrp.Go(func() error {

challenger/types/config.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55

66
nodetypes "github.com/initia-labs/opinit-bots/node/types"
7+
servertypes "github.com/initia-labs/opinit-bots/server/types"
78
)
89

910
type NodeConfig struct {
@@ -29,8 +30,8 @@ type Config struct {
2930
// Version is the version used to build output root.
3031
Version uint8 `json:"version"`
3132

32-
// ListenAddress is the address to listen for incoming requests.
33-
ListenAddress string `json:"listen_address"`
33+
// Server is the configuration for the server.
34+
Server servertypes.ServerConfig `json:"server"`
3435

3536
// L1Node is the configuration for the l1 node.
3637
L1Node NodeConfig `json:"l1_node"`
@@ -53,8 +54,14 @@ type Config struct {
5354

5455
func DefaultConfig() *Config {
5556
return &Config{
56-
Version: 1,
57-
ListenAddress: "localhost:3001",
57+
Version: 1,
58+
59+
Server: servertypes.ServerConfig{
60+
Address: "localhost:3001",
61+
AllowOrigins: "*",
62+
AllowHeaders: "Origin, Content-Type, Accept",
63+
AllowMethods: "GET",
64+
},
5865

5966
L1Node: NodeConfig{
6067
ChainID: "testnet-l1-1",
@@ -82,8 +89,8 @@ func (cfg Config) Validate() error {
8289
return errors.New("only version 1 is supported")
8390
}
8491

85-
if cfg.ListenAddress == "" {
86-
return errors.New("listen address is required")
92+
if err := cfg.Server.Validate(); err != nil {
93+
return err
8794
}
8895

8996
if err := cfg.L1Node.Validate(); err != nil {

executor/child/withdraw.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ func (ch *Child) GetLastAddressIndex(address string) (lastIndex uint64, err erro
300300

301301
func (ch *Child) DeleteFutureWithdrawals(fromSequence uint64) error {
302302
return ch.DB().PrefixedIterate(executortypes.WithdrawalKey, nil, func(key, _ []byte) (bool, error) {
303+
if len(key) != len(executortypes.WithdrawalKey)+1+8 {
304+
return false, nil
305+
}
303306
sequence := dbtypes.ToUint64Key(key[len(key)-8:])
304307
if sequence >= fromSequence {
305308
err := ch.DB().Delete(key)

executor/executor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type Executor struct {
4141
homePath string
4242
}
4343

44-
func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logger *zap.Logger, homePath string) *Executor {
44+
func NewExecutor(cfg *executortypes.Config, db types.DB, logger *zap.Logger, homePath string) *Executor {
4545
err := cfg.Validate()
4646
if err != nil {
4747
panic(err)
@@ -66,7 +66,7 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg
6666

6767
cfg: cfg,
6868
db: db,
69-
server: sv,
69+
server: server.NewServer(cfg.Server),
7070
logger: logger,
7171

7272
homePath: homePath,
@@ -135,7 +135,7 @@ func (ex *Executor) Start(ctx context.Context) error {
135135
defer func() {
136136
ex.logger.Info("api server stopped")
137137
}()
138-
return ex.server.Start(ex.cfg.ListenAddress)
138+
return ex.server.Start()
139139
})
140140
ex.host.Start(ctx)
141141
ex.child.Start(ctx)

executor/types/config.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types"
88
nodetypes "github.com/initia-labs/opinit-bots/node/types"
9+
10+
servertypes "github.com/initia-labs/opinit-bots/server/types"
911
)
1012

1113
type NodeConfig struct {
@@ -34,8 +36,8 @@ type Config struct {
3436
// Version is the version used to build output root.
3537
Version uint8 `json:"version"`
3638

37-
// ListenAddress is the address to listen for incoming requests.
38-
ListenAddress string `json:"listen_address"`
39+
// Server is the configuration for the server.
40+
Server servertypes.ServerConfig `json:"server"`
3941

4042
// L1Node is the configuration for the l1 node.
4143
L1Node NodeConfig `json:"l1_node"`
@@ -84,8 +86,14 @@ type Config struct {
8486

8587
func DefaultConfig() *Config {
8688
return &Config{
87-
Version: 1,
88-
ListenAddress: "localhost:3000",
89+
Version: 1,
90+
91+
Server: servertypes.ServerConfig{
92+
Address: "localhost:3000",
93+
AllowOrigins: "*",
94+
AllowHeaders: "Origin, Content-Type, Accept",
95+
AllowMethods: "GET",
96+
},
8997

9098
L1Node: NodeConfig{
9199
ChainID: "testnet-l1-1",
@@ -138,8 +146,8 @@ func (cfg Config) Validate() error {
138146
return errors.New("only version 1 is supported")
139147
}
140148

141-
if cfg.ListenAddress == "" {
142-
return errors.New("listen address is required")
149+
if err := cfg.Server.Validate(); err != nil {
150+
return err
143151
}
144152

145153
if err := cfg.L1Node.Validate(); err != nil {

server/server.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
package server
22

3-
import "github.com/gofiber/fiber/v2"
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/gofiber/fiber/v2/middleware/cors"
6+
"github.com/initia-labs/opinit-bots/server/types"
7+
)
48

59
type Server struct {
10+
address string
611
*fiber.App
712
}
813

9-
func NewServer() *Server {
14+
func NewServer(cfg types.ServerConfig) *Server {
15+
app := fiber.New()
16+
app.Use(cors.New(cors.Config{
17+
AllowOrigins: cfg.AllowOrigins,
18+
AllowHeaders: cfg.AllowHeaders,
19+
AllowMethods: cfg.AllowMethods,
20+
}))
21+
1022
return &Server{
11-
fiber.New(),
23+
address: cfg.Address,
24+
App: app,
1225
}
1326
}
1427

15-
func (s *Server) Start(address string) error {
16-
return s.Listen(address)
28+
func (s *Server) Start() error {
29+
return s.Listen(s.address)
1730
}
1831

1932
func (s *Server) RegisterQuerier(path string, fn func(c *fiber.Ctx) error) {

server/types/config.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package types
2+
3+
import "errors"
4+
5+
type ServerConfig struct {
6+
Address string `json:"address"`
7+
AllowOrigins string `json:"allow_origins"`
8+
AllowHeaders string `json:"allow_headers"`
9+
AllowMethods string `json:"allow_methods"`
10+
}
11+
12+
func (s ServerConfig) Validate() error {
13+
if s.Address == "" {
14+
return errors.New("address is required")
15+
}
16+
return nil
17+
}

0 commit comments

Comments
 (0)