-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
98 lines (88 loc) · 2.69 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
package main
import (
"fmt"
"net/http"
_ "net/http/pprof"
"time"
"github.com/labstack/echo-contrib/prometheus"
"github.com/tonkeeper/bridge/storage/memory"
"github.com/tonkeeper/bridge/storage/pg"
"golang.org/x/exp/slices"
"golang.org/x/time/rate"
"github.com/tonkeeper/bridge/config"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
func main() {
log.Info("Bridge is running")
config.LoadConfig()
var (
dbConn db
err error
)
if config.Config.DbURI != "" {
dbConn, err = pg.NewStorage(config.Config.DbURI)
if err != nil {
log.Fatalf("db connection %v", err)
}
} else {
dbConn = memory.NewStorage()
}
http.Handle("/metrics", promhttp.Handler())
go func() {
log.Fatal(http.ListenAndServe(":9103", nil))
}()
e := echo.New()
e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
Skipper: nil,
DisableStackAll: true,
DisablePrintStack: false,
}))
e.Use(middleware.Logger())
e.Use(middleware.RateLimiterWithConfig(middleware.RateLimiterConfig{
Skipper: func(c echo.Context) bool {
if skipRateLimitsByToken(c.Request()) || c.Path() != "/bridge/message" {
return true
}
return false
},
Store: middleware.NewRateLimiterMemoryStore(rate.Limit(config.Config.RPSLimit)),
}))
e.Use(connectionsLimitMiddleware(newConnectionLimiter(config.Config.ConnectionsLimit), func(c echo.Context) bool {
if skipRateLimitsByToken(c.Request()) || c.Path() != "/bridge/events" {
return true
}
return false
}))
if config.Config.CorsEnable {
corsConfig := middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{echo.GET, echo.POST, echo.OPTIONS},
AllowHeaders: []string{"DNT", "X-CustomHeader", "Keep-Alive", "User-Agent", "X-Requested-With", "If-Modified-Since", "Cache-Control", "Content-Type", "Authorization"},
AllowCredentials: true,
MaxAge: 86400,
})
e.Use(corsConfig)
}
h := newHandler(dbConn, time.Duration(config.Config.HeartbeatInterval)*time.Second)
registerHandlers(e, h)
var existedPaths []string
for _, r := range e.Routes() {
existedPaths = append(existedPaths, r.Path)
}
p := prometheus.NewPrometheus("http", func(c echo.Context) bool {
return !slices.Contains(existedPaths, c.Path())
})
e.Use(p.HandlerFunc)
if config.Config.SelfSignedTLS {
cert, key, err := generateSelfSignedCertificate()
if err != nil {
log.Fatalf("failed to generate self signed certificate: %v", err)
}
log.Fatal(e.StartTLS(fmt.Sprintf(":%v", config.Config.Port), cert, key))
} else {
log.Fatal(e.Start(fmt.Sprintf(":%v", config.Config.Port)))
}
}