forked from fbonalair/traefik-crowdsec-bouncer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bouncer.go
70 lines (60 loc) · 1.46 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
67
68
69
70
package main
import (
"net/http"
"os"
"log/slog"
"github.com/l3montree-dev/traefik-crowdsec-bouncer/config"
"github.com/l3montree-dev/traefik-crowdsec-bouncer/controller"
"github.com/lmittmann/tint"
)
func getLogLevel() slog.Level {
envVar := os.Getenv("LOG_LEVEL")
if envVar == "" {
return slog.LevelInfo
}
switch envVar {
case "DEBUG":
return slog.LevelDebug
case "INFO":
return slog.LevelInfo
case "WARN":
return slog.LevelWarn
case "ERROR":
return slog.LevelError
default:
return slog.LevelInfo
}
}
func initLogger() {
loggingHandler := tint.NewHandler(os.Stdout, &tint.Options{
AddSource: true,
Level: getLogLevel(),
})
logger := slog.New(loggingHandler)
slog.SetDefault(logger)
}
func main() {
initLogger()
config.ValidateEnv()
router, err := setupRouter()
if err != nil {
slog.Error("An error occurred while starting webserver", "err", err)
return
}
controller.StartStreaming()
slog.Info("starting server on port :8080")
err = http.ListenAndServe(":8080", router)
if err != nil {
slog.Error("An error occurred while starting bouncer", "err", err)
return
}
}
func setupRouter() (*http.ServeMux, error) {
// Web framework
router := http.NewServeMux()
router.HandleFunc("GET /api/v1/ping", controller.Ping)
router.HandleFunc("GET /api/v1/healthz", controller.Healthz)
router.HandleFunc("GET /api/v1/forwardAuth", controller.ForwardAuth)
router.HandleFunc("GET /api/v1/metrics", controller.Metrics)
return router, nil
}