-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
87 lines (70 loc) · 2.03 KB
/
app.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
package main
import (
"flag"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/monitor"
"github.com/gofiber/fiber/v2/middleware/proxy"
"github.com/gofiber/fiber/v2/middleware/recover"
"flare-node-proxy/flags"
"flare-node-proxy/logging"
"flare-node-proxy/middlewares"
"flare-node-proxy/utils"
"flare-node-proxy/whitelist"
log "github.com/sirupsen/logrus"
)
func main() {
logging.Setup()
// Use an external setup function in order
// to configure the app in tests as well
app := Setup()
// Listen on port 3000 by default or port defined via cli flags
go func() {
log.Fatal(app.Listen(*flags.Port)) // go run app.go -port=:3000
}()
whitelist.InitPriceSubmitterWhitelist(flags.WhitelistFilePath)
<-make(chan struct{})
}
func Setup() *fiber.App {
// Parse command-line flags
flag.Parse()
if !*flags.Prod {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
// Create fiber app
app := fiber.New(fiber.Config{
Prefork: *flags.Prod, // go run app.go -prod
AppName: "Flare Node Proxy",
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
DisableStartupMessage: true,
})
// Standard middlewares
app.Use(recover.New())
if !*flags.Prod {
app.Use(logger.New(logger.Config{
Format: "[${ip}]:${port} ${status} ${latency} - ${method} ${path}\n",
}))
}
// Enable monitor from Fiber
if *flags.Enablemonitor {
app.Get("/flareproxy/metrics", monitor.New(monitor.Config{Title: "Flare Node Proxy Metrics Page"}))
log.Info("Monitor enabled")
}
// Block transactions to PriceSubmitter
app.Post("*", middlewares.BlockPriceSubmitter)
// Proxy requests to specified host
app.All("*", func(c *fiber.Ctx) error {
url := *flags.Endpoint + c.Path()
if err := proxy.Do(c, url); err != nil {
return err
}
return nil
})
port, host := utils.ParseAddr(*flags.Port, app.Config().Network)
log.Infof("Server listenning on %s:%s", host, port)
return app
}