Simplify HTTP Basic Authentication middleware
This commit is contained in:
parent
a908ce78ab
commit
c5575c3dc1
1 changed files with 8 additions and 10 deletions
18
main.go
18
main.go
|
@ -388,8 +388,8 @@ func (br *bridge) handleWebhooks(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (br *bridge) basicAuthMiddleware(handler http.HandlerFunc) http.HandlerFunc {
|
func (br *bridge) authMiddleware(handler http.Handler) http.Handler {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, pass, ok := r.BasicAuth()
|
user, pass, ok := r.BasicAuth()
|
||||||
if !ok {
|
if !ok {
|
||||||
br.logger.Debug("basic auth failure")
|
br.logger.Debug("basic auth failure")
|
||||||
|
@ -410,8 +410,8 @@ func (br *bridge) basicAuthMiddleware(handler http.HandlerFunc) http.HandlerFunc
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
handler(w, r)
|
handler.ServeHTTP(w, r)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (br *bridge) runCleanup(ctx context.Context) {
|
func (br *bridge) runCleanup(ctx context.Context) {
|
||||||
|
@ -463,18 +463,16 @@ func main() {
|
||||||
logger.Infof("Listening on %s, ntfy-alertmanager %s", cfg.HTTPAddress, version)
|
logger.Infof("Listening on %s, ntfy-alertmanager %s", cfg.HTTPAddress, version)
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", bridge.handleWebhooks)
|
||||||
|
mux.HandleFunc("/silences", bridge.handleSilences)
|
||||||
|
|
||||||
httpServer := &http.Server{
|
httpServer := &http.Server{
|
||||||
Addr: cfg.HTTPAddress,
|
Addr: cfg.HTTPAddress,
|
||||||
Handler: mux,
|
Handler: mux,
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.User != "" && cfg.Password != "" {
|
if cfg.User != "" && cfg.Password != "" {
|
||||||
logger.Info("Enabling HTTP Basic Authentication")
|
logger.Info("Enabling HTTP Basic Authentication")
|
||||||
mux.HandleFunc("/", bridge.basicAuthMiddleware(bridge.handleWebhooks))
|
httpServer.Handler = bridge.authMiddleware(mux)
|
||||||
mux.HandleFunc("/silences", bridge.basicAuthMiddleware(bridge.handleSilences))
|
|
||||||
} else {
|
|
||||||
mux.HandleFunc("/", bridge.handleWebhooks)
|
|
||||||
mux.HandleFunc("/silences", bridge.handleSilences)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := c.(*cache.MemoryCache); ok {
|
if _, ok := c.(*cache.MemoryCache); ok {
|
||||||
|
|
Loading…
Reference in a new issue