/
server.go
67 lines (59 loc) · 1.63 KB
/
server.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
package server
import (
"crypto/tls"
"time"
"github.com/domgolonka/foretoken/app"
"github.com/domgolonka/foretoken/lib/metrics"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/limiter"
"golang.org/x/crypto/acme/autocert"
)
func Server(app *app.App) {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache("./assets"),
}
// TLS Config
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
// Get Certificate from Let's Encrypt
GetCertificate: certManager.GetCertificate,
// By default NextProtos contains the "h2"
// This has to be removed since Fasthttp does not support HTTP/2
// Or it will cause a flood of PRI method logs
// http://webconcepts.info/concepts/http-method/PRI
NextProtos: []string{
"http/1.1", "acme-tls/1",
},
}
fapp := fiber.New(fiber.Config{
//Prefork: prefork,
ErrorHandler: Error(app),
})
if app.Config.RateLimit.Enabled {
fapp.Use(limiter.New(limiter.Config{
Next: func(c *fiber.Ctx) bool {
return c.IP() == "127.0.0.1"
},
Max: app.Config.RateLimit.Max,
Expiration: time.Duration(app.Config.RateLimit.Expiration) * time.Second,
KeyGenerator: func(c *fiber.Ctx) string {
return c.Get("x-forwarded-for")
},
LimitReached: func(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusTooManyRequests)
},
}))
}
srv := metrics.InitPrometheus(fapp)
routers(srv, app)
if app.Config.AutoTLS {
ln, err := tls.Listen("tcp", app.Config.PublicAddress, cfg)
if err != nil {
panic(err)
}
app.Logger.Fatal(fapp.Listener(ln))
} else {
app.Logger.Fatal(fapp.Listen(app.Config.PublicAddress))
}
}