Skip to content

Commit c79ad57

Browse files
committed
finish binary
1 parent fa5b113 commit c79ad57

File tree

5 files changed

+148
-1
lines changed

5 files changed

+148
-1
lines changed

coordinator/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ coordinator_cron:
3434
coordinator_tool:
3535
go build -ldflags "-X scroll-tech/common/version.ZkVersion=${ZK_VERSION}" -o $(PWD)/build/bin/coordinator_tool ./cmd/tool
3636

37+
coordinator_proxy:
38+
go build -ldflags "-X scroll-tech/common/version.ZkVersion=${ZK_VERSION}" -o $(PWD)/build/bin/coordinator_proxy ./cmd/proxy
39+
40+
3741
localsetup: coordinator_api ## Local setup: build coordinator_api, copy config, and setup releases
3842
@echo "Copying configuration files..."
3943
cp -r $(PWD)/conf $(PWD)/build/bin/

coordinator/cmd/proxy/app/app.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}

coordinator/cmd/proxy/app/flags.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package app
2+
3+
import "github.com/urfave/cli/v2"
4+
5+
var (
6+
apiFlags = []cli.Flag{
7+
// http flags
8+
&httpEnabledFlag,
9+
&httpListenAddrFlag,
10+
&httpPortFlag,
11+
}
12+
// httpEnabledFlag enable rpc server.
13+
httpEnabledFlag = cli.BoolFlag{
14+
Name: "http",
15+
Usage: "Enable the HTTP-RPC server",
16+
Value: false,
17+
}
18+
// httpListenAddrFlag set the http address.
19+
httpListenAddrFlag = cli.StringFlag{
20+
Name: "http.addr",
21+
Usage: "HTTP-RPC server listening interface",
22+
Value: "localhost",
23+
}
24+
// httpPortFlag set http.port.
25+
httpPortFlag = cli.IntFlag{
26+
Name: "http.port",
27+
Usage: "HTTP-RPC server listening port",
28+
Value: 8590,
29+
}
30+
)

coordinator/cmd/proxy/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "scroll-tech/coordinator/cmd/proxy/app"
4+
5+
func main() {
6+
app.Run()
7+
}

coordinator/conf/config_proxy.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
}
2323
]
2424
}
25-
},
25+
},
2626
"coordinators": {
2727
"sepolia": {
2828
"base_url": "http://localhost:8555",

0 commit comments

Comments
 (0)