-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
93 lines (80 loc) · 2.52 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
package main
import (
"context"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"runtime/debug"
"time"
"github.com/fraidev/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/marigold-dev/tzproxy/config"
"github.com/marigold-dev/tzproxy/middlewares"
"github.com/ziflex/lecho/v3"
)
func main() {
config := config.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 proxy
startProxyWithGracefulShutdown(e, config)
}
func startMetricsServer(config *config.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 *config.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)
}
}