~xenrox/ntfy-alertmanager

2dcfb7270e712a0a86975faabd2d66d8cb06a086 — Thorben Günther 8 months ago c0a0e3c
Add CORS middleware

Fixes: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/21
1 files changed, 12 insertions(+), 0 deletions(-)

M main.go
M main.go => main.go +12 -0
@@ 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)
	}