Serve API only via cli

This commit is contained in:
Eduard S
2021-02-26 12:40:00 +01:00
parent 91c96eb429
commit 982899efed
2 changed files with 124 additions and 58 deletions

View File

@@ -143,6 +143,42 @@ func cmdRun(c *cli.Context) error {
return nil return nil
} }
func cmdServeAPI(c *cli.Context) error {
cfg, err := parseCli(c)
if err != nil {
return tracerr.Wrap(fmt.Errorf("error parsing flags and config: %w", err))
}
node, err := node.NewNode(cfg.mode, cfg.node)
if err != nil {
return tracerr.Wrap(fmt.Errorf("error starting node: %w", err))
}
node.Start()
stopCh := make(chan interface{})
// catch ^C to send the stop signal
ossig := make(chan os.Signal, 1)
signal.Notify(ossig, os.Interrupt)
const forceStopCount = 3
go func() {
n := 0
for sig := range ossig {
if sig == os.Interrupt {
log.Info("Received Interrupt Signal")
stopCh <- nil
n++
if n == forceStopCount {
log.Fatalf("Received %v Interrupt Signals", forceStopCount)
}
}
}
}()
<-stopCh
node.Stop()
return nil
}
func cmdDiscard(c *cli.Context) error { func cmdDiscard(c *cli.Context) error {
_cfg, err := parseCli(c) _cfg, err := parseCli(c)
if err != nil { if err != nil {
@@ -304,6 +340,12 @@ func main() {
Usage: "Run the hermez-node in the indicated mode", Usage: "Run the hermez-node in the indicated mode",
Action: cmdRun, Action: cmdRun,
}, },
{
Name: "serveapi",
Aliases: []string{},
Usage: "Serve the API only",
Action: cmdServeAPI,
},
{ {
Name: "discard", Name: "discard",
Aliases: []string{}, Aliases: []string{},

View File

@@ -44,6 +44,13 @@ type ForgeBatchGasCost struct {
L2Tx uint64 `validate:"required"` L2Tx uint64 `validate:"required"`
} }
// CoordinatorAPI specifies the configuration parameters of the API in mode
// coordinator
type CoordinatorAPI struct {
// Coordinator enables the coordinator API endpoints
Coordinator bool
}
// Coordinator is the coordinator specific configuration. // Coordinator is the coordinator specific configuration.
type Coordinator struct { type Coordinator struct {
// ForgerAddress is the address under which this coordinator is forging // ForgerAddress is the address under which this coordinator is forging
@@ -193,10 +200,7 @@ type Coordinator struct {
// ForgeBatch transaction. // ForgeBatch transaction.
ForgeBatchGasCost ForgeBatchGasCost `validate:"required"` ForgeBatchGasCost ForgeBatchGasCost `validate:"required"`
} `validate:"required"` } `validate:"required"`
API struct { API CoordinatorAPI `validate:"required"`
// Coordinator enables the coordinator API endpoints
Coordinator bool
} `validate:"required"`
Debug struct { Debug struct {
// BatchPath if set, specifies the path where batchInfo is stored // BatchPath if set, specifies the path where batchInfo is stored
// in JSON in every step/update of the pipeline // in JSON in every step/update of the pipeline
@@ -211,26 +215,29 @@ type Coordinator struct {
} }
} }
// Node is the hermez node configuration. // NodeAPI specifies the configuration parameters of the API
type Node struct { type NodeAPI struct {
PriceUpdater struct { // Address where the API will listen if set
// Interval between price updater calls Address string
Interval Duration `valudate:"required"` // Explorer enables the Explorer API endpoints
// URL of the token prices provider Explorer bool
URL string `valudate:"required"` // UpdateMetricsInterval is the interval between updates of the
// Type of the API of the token prices provider // API metrics
Type string `valudate:"required"` UpdateMetricsInterval Duration
} `validate:"required"` // UpdateRecommendedFeeInterval is the interval between updates of the
StateDB struct { // recommended fees
// Path where the synchronizer StateDB is stored UpdateRecommendedFeeInterval Duration
Path string `validate:"required"` // Maximum concurrent connections allowed between API and SQL
// Keep is the number of checkpoints to keep MaxSQLConnections int `validate:"required"`
Keep int `validate:"required"` // SQLConnectionTimeout is the maximum amount of time that an API request
} `validate:"required"` // can wait to stablish a SQL connection
// It's possible to use diferentiated SQL connections for read/write. SQLConnectionTimeout Duration
// If the read configuration is not provided, the write one it's going to be used }
// for both reads and writes
PostgreSQL struct { // It's possible to use diferentiated SQL connections for read/write.
// If the read configuration is not provided, the write one it's going to be used
// for both reads and writes
type PostgreSQL struct {
// Port of the PostgreSQL write server // Port of the PostgreSQL write server
PortWrite int `validate:"required"` PortWrite int `validate:"required"`
// Host of the PostgreSQL write server // Host of the PostgreSQL write server
@@ -251,7 +258,38 @@ type Node struct {
PasswordRead string PasswordRead string
// Name of the PostgreSQL read server database // Name of the PostgreSQL read server database
NameRead string NameRead string
}
// NodeDebug specifies debug configuration parameters
type NodeDebug struct {
// APIAddress is the address where the debugAPI will listen if
// set
APIAddress string
// MeddlerLogs enables meddler debug mode, where unused columns and struct
// fields will be logged
MeddlerLogs bool
// GinDebugMode sets Gin-Gonic (the web framework) to run in
// debug mode
GinDebugMode bool
}
// Node is the hermez node configuration.
type Node struct {
PriceUpdater struct {
// Interval between price updater calls
Interval Duration `valudate:"required"`
// URL of the token prices provider
URL string `valudate:"required"`
// Type of the API of the token prices provider
Type string `valudate:"required"`
} `validate:"required"` } `validate:"required"`
StateDB struct {
// Path where the synchronizer StateDB is stored
Path string `validate:"required"`
// Keep is the number of checkpoints to keep
Keep int `validate:"required"`
} `validate:"required"`
PostgreSQL PostgreSQL `validate:"required"`
Web3 struct { Web3 struct {
// URL is the URL of the web3 ethereum-node RPC server // URL is the URL of the web3 ethereum-node RPC server
URL string `validate:"required"` URL string `validate:"required"`
@@ -282,37 +320,23 @@ type Node struct {
// TokenHEZ address // TokenHEZ address
TokenHEZName string `validate:"required"` TokenHEZName string `validate:"required"`
} `validate:"required"` } `validate:"required"`
API struct { API NodeAPI `validate:"required"`
// Address where the API will listen if set Debug NodeDebug `validate:"required"`
Address string
// Explorer enables the Explorer API endpoints
Explorer bool
// UpdateMetricsInterval is the interval between updates of the
// API metrics
UpdateMetricsInterval Duration
// UpdateRecommendedFeeInterval is the interval between updates of the
// recommended fees
UpdateRecommendedFeeInterval Duration
// Maximum concurrent connections allowed between API and SQL
MaxSQLConnections int `validate:"required"`
// SQLConnectionTimeout is the maximum amount of time that an API request
// can wait to stablish a SQL connection
SQLConnectionTimeout Duration
} `validate:"required"`
Debug struct {
// APIAddress is the address where the debugAPI will listen if
// set
APIAddress string
// MeddlerLogs enables meddler debug mode, where unused columns and struct
// fields will be logged
MeddlerLogs bool
// GinDebugMode sets Gin-Gonic (the web framework) to run in
// debug mode
GinDebugMode bool
}
Coordinator Coordinator `validate:"-"` Coordinator Coordinator `validate:"-"`
} }
type APIServer struct {
API NodeAPI `validate:"required"`
PostgreSQL PostgreSQL `validate:"required"`
Coordinator struct {
API struct {
// Coordinator enables the coordinator API endpoints
Coordinator bool
} `validate:"required"`
} `validate:"required"`
Debug NodeDebug `validate:"required"`
}
// Load loads a generic config. // Load loads a generic config.
func Load(path string, cfg interface{}) error { func Load(path string, cfg interface{}) error {
bs, err := ioutil.ReadFile(path) //nolint:gosec bs, err := ioutil.ReadFile(path) //nolint:gosec