|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "os/signal" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | + "github.com/prometheus/client_golang/prometheus" |
| 14 | + "github.com/scroll-tech/go-ethereum/log" |
| 15 | + "github.com/urfave/cli/v2" |
| 16 | + |
| 17 | + "scroll-tech/common/utils" |
| 18 | + "scroll-tech/common/version" |
| 19 | + |
| 20 | + "scroll-tech/coordinator/internal/config" |
| 21 | + "scroll-tech/coordinator/internal/controller/proxy" |
| 22 | + "scroll-tech/coordinator/internal/route" |
| 23 | +) |
| 24 | + |
| 25 | +var app *cli.App |
| 26 | + |
| 27 | +func init() { |
| 28 | + // Set up coordinator app info. |
| 29 | + app = cli.NewApp() |
| 30 | + app.Action = action |
| 31 | + app.Name = "coordinator proxy" |
| 32 | + app.Usage = "Proxy for multiple Scroll L2 Coordinators" |
| 33 | + app.Version = version.Version |
| 34 | + app.Flags = append(app.Flags, utils.CommonFlags...) |
| 35 | + app.Flags = append(app.Flags, apiFlags...) |
| 36 | + app.Before = func(ctx *cli.Context) error { |
| 37 | + return utils.LogSetup(ctx) |
| 38 | + } |
| 39 | + // Register `coordinator-test` app for integration-test. |
| 40 | + utils.RegisterSimulation(app, utils.CoordinatorAPIApp) |
| 41 | +} |
| 42 | + |
| 43 | +func action(ctx *cli.Context) error { |
| 44 | + cfgFile := ctx.String(utils.ConfigFileFlag.Name) |
| 45 | + cfg, err := config.NewProxyConfig(cfgFile) |
| 46 | + if err != nil { |
| 47 | + log.Crit("failed to load config file", "config file", cfgFile, "error", err) |
| 48 | + } |
| 49 | + |
| 50 | + //observability.Server(ctx, db) |
| 51 | + registry := prometheus.DefaultRegisterer |
| 52 | + |
| 53 | + apiSrv := server(ctx, cfg, registry) |
| 54 | + |
| 55 | + log.Info( |
| 56 | + "Start coordinator api successfully.", |
| 57 | + "version", version.Version, |
| 58 | + ) |
| 59 | + |
| 60 | + // Catch CTRL-C to ensure a graceful shutdown. |
| 61 | + interrupt := make(chan os.Signal, 1) |
| 62 | + signal.Notify(interrupt, os.Interrupt) |
| 63 | + |
| 64 | + // Wait until the interrupt signal is received from an OS signal. |
| 65 | + <-interrupt |
| 66 | + log.Info("start shutdown coordinator proxy server ...") |
| 67 | + |
| 68 | + closeCtx, cancelExit := context.WithTimeout(context.Background(), 5*time.Second) |
| 69 | + defer cancelExit() |
| 70 | + if err = apiSrv.Shutdown(closeCtx); err != nil { |
| 71 | + log.Warn("shutdown coordinator proxy server failure", "error", err) |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + <-closeCtx.Done() |
| 76 | + log.Info("coordinator proxy server exiting success") |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func server(ctx *cli.Context, cfg *config.ProxyConfig, reg prometheus.Registerer) *http.Server { |
| 81 | + router := gin.New() |
| 82 | + proxy.InitController(cfg, reg) |
| 83 | + route.ProxyRoute(router, cfg, reg) |
| 84 | + port := ctx.String(httpPortFlag.Name) |
| 85 | + srv := &http.Server{ |
| 86 | + Addr: fmt.Sprintf(":%s", port), |
| 87 | + Handler: router, |
| 88 | + ReadHeaderTimeout: time.Minute, |
| 89 | + } |
| 90 | + |
| 91 | + go func() { |
| 92 | + if runServerErr := srv.ListenAndServe(); runServerErr != nil && !errors.Is(runServerErr, http.ErrServerClosed) { |
| 93 | + log.Crit("run coordinator proxy http server failure", "error", runServerErr) |
| 94 | + } |
| 95 | + }() |
| 96 | + return srv |
| 97 | +} |
| 98 | + |
| 99 | +// Run coordinator. |
| 100 | +func Run() { |
| 101 | + // RunApp the coordinator. |
| 102 | + if err := app.Run(os.Args); err != nil { |
| 103 | + _, _ = fmt.Fprintln(os.Stderr, err) |
| 104 | + os.Exit(1) |
| 105 | + } |
| 106 | +} |
0 commit comments