-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
129 lines (105 loc) · 3.4 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
package main
import (
"flag"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
)
var (
version = "dev" // is set during build process
// Default values
defaultLogLevel = getEnv("LOG_LEVEL", "info")
defaultLogJSON = os.Getenv("LOG_JSON") != ""
defaultListenAddr = getEnv("PROXY_LISTEN_ADDR", "localhost:25590")
defaultTimeoutMs = getEnvInt("BUILDER_TIMEOUT_MS", 2000) // timeout for all the requests to the builders
// Flags
logJSON = flag.Bool("json", defaultLogJSON, "log in JSON format instead of text")
logLevel = flag.String("loglevel", defaultLogLevel, "log-level: trace, debug, info, warn/warning, error, fatal, panic")
listenAddr = flag.String("addr", defaultListenAddr, "listen-address for builder proxy server")
builderURLs = flag.String("builders", "", "builder urls - single entry or comma-separated list (scheme://host)")
builderTimeoutMs = flag.Int("request-timeout", defaultTimeoutMs, "timeout for requests to a builder [ms]")
proxyURLs = flag.String("proxies", "", "proxy urls - other proxies to forward BN requests to (scheme://host)")
proxyTimeoutMs = flag.Int("proxy-request-timeout", defaultTimeoutMs, "timeout for redundant beacon node requests to another proxy [ms]")
)
var log = logrus.WithField("module", "sync-proxy")
func main() {
flag.Parse()
logrus.SetOutput(os.Stdout)
if *logJSON {
log.Logger.SetFormatter(&logrus.JSONFormatter{})
} else {
log.Logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
}
if *logLevel != "" {
lvl, err := logrus.ParseLevel(*logLevel)
if err != nil {
log.Fatalf("Invalid loglevel: %s", *logLevel)
}
logrus.SetLevel(lvl)
}
log.Infof("sync-proxy %s", version)
builders := parseURLs(*builderURLs)
if len(builders) == 0 {
log.Fatal("No builder urls specified")
}
log.WithField("builders", builders).Infof("using %d builders", len(builders))
builderTimeout := time.Duration(*builderTimeoutMs) * time.Millisecond
proxies := parseURLs(*proxyURLs)
log.WithField("proxies", proxies).Infof("using %d proxies", len(proxies))
proxyTimeout := time.Duration(*proxyTimeoutMs) * time.Millisecond
// Create a new proxy service.
opts := ProxyServiceOpts{
ListenAddr: *listenAddr,
Builders: builders,
BuilderTimeout: builderTimeout,
Proxies: proxies,
ProxyTimeout: proxyTimeout,
Log: log,
}
proxyService, err := NewProxyService(opts)
if err != nil {
log.WithError(err).Fatal("failed creating the server")
}
log.Println("listening on", *listenAddr)
log.Fatal(proxyService.StartHTTPServer())
}
func getEnv(key string, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return defaultValue
}
func getEnvInt(key string, defaultValue int) int {
if value, ok := os.LookupEnv(key); ok {
val, err := strconv.Atoi(value)
if err == nil {
return val
}
}
return defaultValue
}
func parseURLs(urls string) []*url.URL {
ret := []*url.URL{}
for _, entry := range strings.Split(urls, ",") {
rawURL := strings.TrimSpace(entry)
if rawURL == "" {
continue
}
// Add protocol scheme prefix if it does not exist.
if !strings.HasPrefix(rawURL, "http") {
rawURL = "http://" + rawURL
}
// Parse the provided URL.
url, err := url.ParseRequestURI(rawURL)
if err != nil {
log.WithError(err).WithField("url", entry).Fatal("Invalid URL")
}
ret = append(ret, url)
}
return ret
}