Add CORS middleware

Fixes: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/21
This commit is contained in:
Thorben Günther 2023-08-16 01:18:51 +02:00
parent c0a0e3c264
commit 2dcfb7270e
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED

12
main.go
View file

@ -402,6 +402,16 @@ func (br *bridge) handleWebhooks(w http.ResponseWriter, r *http.Request) {
}
}
func (br *bridge) corsMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
handler.ServeHTTP(w, r)
})
}
func (br *bridge) authMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger := br.logger.With(slog.String("url", r.URL.String()))
@ -495,6 +505,8 @@ func main() {
httpServer.Handler = bridge.authMiddleware(mux)
}
httpServer.Handler = bridge.corsMiddleware(httpServer.Handler)
if _, ok := c.(*cache.MemoryCache); ok {
go bridge.runCleanup(ctx)
}