-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
55 lines (47 loc) · 1.37 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
package main
import (
"net/http"
"net/http/httputil"
"net/url"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/bitsbeats/prometheus-acls/internal/auth"
"github.com/bitsbeats/prometheus-acls/internal/config"
"github.com/bitsbeats/prometheus-acls/internal/labeler"
)
func main() {
// config
cfg, err := config.Parse()
if err != nil {
log.WithError(err).Fatalf("unable to load config")
}
// mux
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/-/ready", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
// auth
a, err := auth.NewAuth(cfg, "/oauth/")
if err != nil {
log.WithError(err).Fatalf("unable to setup auth")
}
mux.HandleFunc("/oauth/login", a.LoginHandler)
mux.HandleFunc("/oauth/callback", a.CallbackHandler)
// reverse proxy
u, err := url.Parse(cfg.PrometheusURL)
if err != nil {
log.WithError(err).Fatalf("unable to parse prometheus url")
}
proxy := httputil.NewSingleHostReverseProxy(u)
l := labeler.NewLabeler()
promacl := l.PromACLMiddlewareFor(u)
// authprotect -> acls -> prometheus
mux.Handle("/", a.Middleware(promacl(proxy)))
// serve
log.WithField("listen", cfg.Listen).Info("listening")
err = http.ListenAndServe(cfg.Listen, mux)
if err != nil {
log.WithError(err).Fatalf("unable to start webserver")
}
}