forked from 0xProject/rpc-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (167 loc) · 4.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog/v2"
"github.com/sygmaprotocol/rpc-gateway/internal/auth"
"github.com/sygmaprotocol/rpc-gateway/internal/metrics"
"github.com/sygmaprotocol/rpc-gateway/internal/util"
"github.com/pkg/errors"
"github.com/sygmaprotocol/rpc-gateway/internal/rpcgateway"
"github.com/urfave/cli/v2"
)
// Config represents the application configuration structure,
// including metrics and gateway configurations.
type Config struct {
Metrics MetricsConfig `json:"metrics"`
Port uint `json:"port"`
Gateways []GatewayConfig `json:"gateways"`
}
type MetricsConfig struct {
Port uint `json:"port"`
}
type GatewayConfig struct {
ConfigFile string `json:"configFile"`
Name string `json:"name"`
}
func main() {
c, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
fmt.Println(strings.Join(os.Args, ","))
app := &cli.App{
Name: "rpc-gateway",
Usage: "The failover proxy for node providers.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "The JSON configuration file path with gateway configurations.",
Value: "config.json", // Default configuration file name
},
&cli.BoolFlag{
Name: "env",
Usage: "Load configuration from environment variable named GATEWAY_CONFIG.",
Value: false,
},
&cli.BoolFlag{
Name: "auth",
Usage: "Enable basic authentication.",
Value: false,
},
},
Action: func(cc *cli.Context) error {
configPath := resolveConfigPath(cc.String("config"), cc.Bool("env"))
config, err := util.LoadJSONFile[Config](configPath)
if err != nil {
return errors.Wrap(err, "failed to load config")
}
logger := configureLogger()
startMetricsServer(config.Metrics.Port)
r := chi.NewRouter()
r.Use(httplog.RequestLogger(logger))
r.Use(middleware.Recoverer)
r.Use(middleware.Heartbeat("/health"))
// Add basic auth middleware
if cc.Bool("auth") {
tokenMapJSON := os.Getenv("GATEWAY_TOKEN_MAP")
if tokenMapJSON == "" {
return errors.New("GATEWAY_TOKEN_MAP environment variable must be set for authentication")
}
var tokenMap map[string]auth.TokenInfo
if err := json.Unmarshal([]byte(tokenMapJSON), &tokenMap); err != nil {
return errors.Wrap(err, "failed to parse GATEWAY_TOKEN_MAP")
}
for _, details := range tokenMap {
if details.NumOfRequestPerSec <= 0 {
return errors.New("numOfRequestPerSec must be a positive number")
}
}
r.Use(auth.URLTokenAuth(tokenMap))
fmt.Println("Authentication configured on gateway")
}
server := &http.Server{
Addr: fmt.Sprintf(":%d", config.Port),
Handler: r,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
ReadHeaderTimeout: time.Second * 5,
}
defer server.Close()
var wg sync.WaitGroup
for _, gatewayConfig := range config.Gateways {
wg.Add(1)
go func(gwConfig GatewayConfig) {
defer wg.Done()
err := startGateway(c, gwConfig, r)
if err != nil {
fmt.Fprintf(os.Stderr, "error starting gateway '%s': %v\n", gwConfig.Name, err)
}
}(gatewayConfig)
}
fmt.Printf("Starting RPC Gateway server on port: %d\n", config.Port)
err = server.ListenAndServe()
if err != nil {
return err
}
wg.Wait()
return nil
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err)
}
}
func resolveConfigPath(config string, isENV bool) string {
if isENV {
return "GATEWAY_CONFIG"
}
return config
}
func configureLogger() *httplog.Logger {
logLevel := slog.LevelWarn
if os.Getenv("DEBUG") == "true" {
logLevel = slog.LevelDebug
}
return httplog.NewLogger("rpc-gateway", httplog.Options{
JSON: true,
RequestHeaders: true,
LogLevel: logLevel,
})
}
func startMetricsServer(port uint) {
metricsServer := metrics.NewServer(metrics.Config{Port: port})
go func() {
err := metricsServer.Start()
defer func(metricsServer *metrics.Server) {
err := metricsServer.Stop()
if err != nil {
fmt.Fprintf(os.Stderr, "error stopping metrics server: %v\n", err)
}
}(metricsServer)
if err != nil {
fmt.Fprintf(os.Stderr, "error starting metrics server: %v\n", err)
}
}()
}
func startGateway(ctx context.Context, config GatewayConfig, router *chi.Mux) error {
service, err := rpcgateway.NewRPCGatewayFromConfigFile(config.ConfigFile, router)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("%s rpc-gateway failed", config.Name))
}
err = service.Start(ctx)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("cannot start %s rpc-gateway", config.Name))
}
<-ctx.Done()
return errors.Wrap(service.Stop(ctx), fmt.Sprintf("cannot stop %s rpc-gateway", config.Name))
}