2023-05-28 00:54:04 +00:00
|
|
|
package beacon
|
|
|
|
|
|
|
|
import (
|
2023-06-08 07:43:27 +00:00
|
|
|
"net"
|
2023-05-28 00:54:04 +00:00
|
|
|
"net/http"
|
2023-06-08 07:43:27 +00:00
|
|
|
"time"
|
2023-05-28 00:54:04 +00:00
|
|
|
|
2023-06-08 07:43:27 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/beacon/handler"
|
|
|
|
"github.com/ledgerwatch/log/v3"
|
2023-05-28 00:54:04 +00:00
|
|
|
)
|
|
|
|
|
2023-06-08 07:43:27 +00:00
|
|
|
// TODO(enriavil1): Make this configurable via flags
|
|
|
|
type RouterConfiguration struct {
|
2023-08-25 22:41:57 +00:00
|
|
|
Active bool
|
2023-06-08 07:43:27 +00:00
|
|
|
Protocol string
|
|
|
|
Address string
|
2023-05-28 00:54:04 +00:00
|
|
|
|
2023-06-08 07:43:27 +00:00
|
|
|
ReadTimeTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
2023-05-28 00:54:04 +00:00
|
|
|
}
|
|
|
|
|
2023-06-08 07:43:27 +00:00
|
|
|
func ListenAndServe(api *handler.ApiHandler, routerCfg *RouterConfiguration) {
|
|
|
|
listener, err := net.Listen(routerCfg.Protocol, routerCfg.Address)
|
|
|
|
server := &http.Server{
|
2023-08-30 23:18:12 +00:00
|
|
|
Handler: newBeaconMiddleware(api),
|
2023-06-08 07:43:27 +00:00
|
|
|
ReadTimeout: routerCfg.ReadTimeTimeout,
|
|
|
|
IdleTimeout: routerCfg.IdleTimeout,
|
|
|
|
WriteTimeout: routerCfg.IdleTimeout,
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("[Beacon API] Failed to start listening", "addr", routerCfg.Address, "err", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.Serve(listener); err != nil {
|
|
|
|
log.Warn("[Beacon API] failed to start serving", "addr", routerCfg.Address, "err", err)
|
|
|
|
}
|
2023-05-28 00:54:04 +00:00
|
|
|
}
|