-
Notifications
You must be signed in to change notification settings - Fork 22
/
bouncer.go
66 lines (58 loc) · 1.54 KB
/
bouncer.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
package main
import (
"os"
. "github.com/fbonalair/traefik-crowdsec-bouncer/config"
"github.com/fbonalair/traefik-crowdsec-bouncer/controler"
"github.com/gin-contrib/logger"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"strings"
)
var logLevel = OptionalEnv("CROWDSEC_BOUNCER_LOG_LEVEL", "1")
var trustedProxiesList = strings.Split(OptionalEnv("TRUSTED_PROXIES", "0.0.0.0/0"), ",")
func main() {
ValidateEnv()
router, err := setupRouter()
if err != nil {
log.Fatal().Err(err).Msgf("An error occurred while starting webserver")
return
}
err = router.Run()
if err != nil {
log.Fatal().Err(err).Msgf("An error occurred while starting bouncer")
return
}
}
func setupRouter() (*gin.Engine, error) {
// logger framework
if gin.IsDebugging() {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Logger = log.Output(
zerolog.ConsoleWriter{
Out: os.Stderr,
NoColor: false,
TimeFormat: zerolog.TimeFieldFormat,
},
)
}
level, err := zerolog.ParseLevel(logLevel)
if err != nil {
return nil, err
}
zerolog.SetGlobalLevel(level)
// Web framework
router := gin.New()
err = router.SetTrustedProxies(trustedProxiesList)
if err != nil {
return nil, err
}
router.Use(logger.SetLogger(
logger.WithSkipPath([]string{"/api/v1/ping", "/api/v1/healthz"}),
))
router.GET("/api/v1/ping", controler.Ping)
router.GET("/api/v1/healthz", controler.Healthz)
router.GET("/api/v1/forwardAuth", controler.ForwardAuth)
router.GET("/api/v1/metrics", controler.Metrics)
return router, nil
}