diff --git a/knotserver/middleware.go b/knotserver/middleware.go index ae6280d6..5b579bc6 100644 --- a/knotserver/middleware.go +++ b/knotserver/middleware.go @@ -33,3 +33,21 @@ func (h *Knot) RequestLogger(next http.Handler) http.Handler { ) }) } + +func (h *Knot) CORS(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Set CORS headers + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") + w.Header().Set("Access-Control-Max-Age", "86400") + + // Handle preflight requests + if r.Method == "OPTIONS" { + w.WriteHeader(http.StatusOK) + return + } + + next.ServeHTTP(w, r) + }) +}