From 2dcfb7270e712a0a86975faabd2d66d8cb06a086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Wed, 16 Aug 2023 01:18:51 +0200 Subject: [PATCH] Add CORS middleware Fixes: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/21 --- main.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/main.go b/main.go index ce8b0c3..47b7e0d 100644 --- a/main.go +++ b/main.go @@ -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) } -- 2.44.0