forked from marigold-dev/tzproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
128 lines (114 loc) · 3.55 KB
/
main.go
File metadata and controls
128 lines (114 loc) · 3.55 KB
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
package main
import (
"context"
"net/http"
"net/http/pprof"
"net"
"strings"
"os"
"os/signal"
"runtime/debug"
"time"
"log"
"github.com/fraidev/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
configpkg "github.com/mavryk-network/mvproxy/config"
"github.com/mavryk-network/mvproxy/middlewares"
"github.com/ziflex/lecho/v3"
)
func main() {
config := configpkg.NewConfig()
debug.SetGCPercent(config.ConfigFile.GC.Percent)
e := echo.New()
e.HideBanner = true
e.HidePort = true
// Middlewares
e.Logger = lecho.From(config.Logger)
e.Use(middleware.Recover())
e.Use(middleware.RequestLoggerWithConfig(*config.RequestLoggerConfig))
e.Use(middlewares.CORS(config))
e.Use(middlewares.RateLimit(config))
e.Use(middlewares.DenyIPs(config))
e.Use(middlewares.AllowRoutes(config))
e.Use(middlewares.DenyRoutes(config))
e.Use(middlewares.Cache(config))
e.Use(middlewares.Gzip(config))
e.Use(middlewares.Retry(config))
e.Use(middleware.ProxyWithConfig(*config.ProxyConfig))
// Start metrics server
startMetricsServer(config)
// Start dynamic DNS resolver for mavryk_host
go func() {
interval := 30 * time.Second
for {
var newTargets []*middleware.ProxyTarget
for _, host := range config.ConfigFile.MavrykHost {
if net.ParseIP(host) == nil && !strings.Contains(host, ":") {
// Host is a DNS name, resolve all A records
ips, err := net.LookupHost(host)
if err == nil {
for _, ip := range ips {
newTargets = append(newTargets, configpkg.HostToTarget(ip+":8732")) // default port, adjust as needed
}
}
} else {
newTargets = append(newTargets, configpkg.HostToTarget(host))
}
}
if len(newTargets) > 0 {
var urls []string
for _, t := range newTargets {
urls = append(urls, t.URL.String())
}
log.Printf("[mvproxy] Updating balancer targets: %v", urls)
if balancer, ok := config.ProxyConfig.Balancer.(interface{ SetTargets([]*middleware.ProxyTarget) }); ok {
balancer.SetTargets(newTargets)
}
}
time.Sleep(interval)
}
}()
// Start proxy
startProxyWithGracefulShutdown(e, config)
}
func startMetricsServer(config *configpkg.Config) {
if config.ConfigFile.Metrics.Enabled {
go func() {
metrics := echo.New()
if config.ConfigFile.Metrics.Pprof {
pp := http.NewServeMux()
pp.HandleFunc("/debug/pprof/", pprof.Index)
pp.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
pp.HandleFunc("/debug/pprof/profile", pprof.Profile)
pp.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
pp.HandleFunc("/debug/pprof/trace", pprof.Trace)
metrics.GET("/debug/pprof/*", echo.WrapHandler(pp))
}
metrics.GET("/metrics", echoprometheus.NewHandler())
metrics.HideBanner = true
metrics.HidePort = true
if err := metrics.Start(config.ConfigFile.Metrics.Host); err != nil && err != http.ErrServerClosed {
metrics.Logger.Fatal(err)
}
}()
}
}
func startProxyWithGracefulShutdown(e *echo.Echo, config *configpkg.Config) {
go func() {
if err := e.Start(config.ConfigFile.Host); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal("Shutting down the server")
e.Logger.Fatal(err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}