2023-07-12 00:50:31 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2024-02-07 00:02:02 +03:00
|
|
|
"golang.org/x/exp/slog"
|
2023-07-15 01:17:02 +03:00
|
|
|
|
2023-07-12 00:50:31 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2024-02-07 00:02:02 +03:00
|
|
|
|
|
|
|
oplog "github.com/ethereum-optimism/optimism/op-service/log"
|
|
|
|
"github.com/ethereum-optimism/optimism/op-ufm/pkg/config"
|
|
|
|
"github.com/ethereum-optimism/optimism/op-ufm/pkg/service"
|
2023-07-12 00:50:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
GitVersion = ""
|
|
|
|
GitCommit = ""
|
|
|
|
GitDate = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-07 00:02:02 +03:00
|
|
|
oplog.SetGlobalLogHandler(slog.NewJSONHandler(
|
|
|
|
os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
2023-07-12 00:50:31 +03:00
|
|
|
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Info("initializing",
|
|
|
|
"version", GitVersion,
|
|
|
|
"commit", GitCommit,
|
|
|
|
"date", GitDate)
|
2023-07-12 00:50:31 +03:00
|
|
|
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
log.Crit("must specify a config file on the command line")
|
|
|
|
}
|
|
|
|
cfg := initConfig(os.Args[1])
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
svc := service.New(cfg)
|
|
|
|
svc.Start(ctx)
|
|
|
|
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
recvSig := <-sig
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Info("caught signal, shutting down",
|
|
|
|
"signal", recvSig)
|
2023-07-12 00:50:31 +03:00
|
|
|
|
|
|
|
svc.Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initConfig(cfgFile string) *config.Config {
|
|
|
|
cfg, err := config.New(cfgFile)
|
|
|
|
if err != nil {
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Crit("error reading config file",
|
|
|
|
"file", cfgFile,
|
|
|
|
"err", err)
|
2023-07-12 00:50:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// update log level from config
|
2024-02-07 00:02:02 +03:00
|
|
|
logLevel, err := oplog.LevelFromString(cfg.LogLevel)
|
2023-07-12 00:50:31 +03:00
|
|
|
if err != nil {
|
2024-02-07 00:02:02 +03:00
|
|
|
logLevel = log.LevelInfo
|
2023-07-12 00:50:31 +03:00
|
|
|
if cfg.LogLevel != "" {
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Warn("invalid server.log_level",
|
|
|
|
"log_level", cfg.LogLevel)
|
2023-07-12 00:50:31 +03:00
|
|
|
}
|
|
|
|
}
|
2024-02-07 00:02:02 +03:00
|
|
|
oplog.SetGlobalLogHandler(slog.NewJSONHandler(
|
|
|
|
os.Stdout, &slog.HandlerOptions{Level: logLevel}))
|
2023-07-12 00:50:31 +03:00
|
|
|
|
|
|
|
// readable parsed config
|
|
|
|
jsonCfg, _ := json.MarshalIndent(cfg, "", " ")
|
|
|
|
fmt.Printf("%s", string(jsonCfg))
|
|
|
|
|
|
|
|
err = cfg.Validate()
|
|
|
|
if err != nil {
|
2023-08-31 23:31:34 +03:00
|
|
|
log.Crit("invalid config",
|
|
|
|
"err", err)
|
2023-07-12 00:50:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|