Monorepo for Tangled tangled.org

knotserver: add cors headers as a middleware function

Signed-off-by: @nekomimi.pet <ana@nekoimimi.pet>

authored by @nekomimi.pet and committed by Tangled 18b3b141 a59a052b

Changed files
+18
knotserver
+18
knotserver/middleware.go
··· 33 33 ) 34 34 }) 35 35 } 36 + 37 + func (h *Knot) CORS(next http.Handler) http.Handler { 38 + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 39 + // Set CORS headers 40 + w.Header().Set("Access-Control-Allow-Origin", "*") 41 + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") 42 + w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") 43 + w.Header().Set("Access-Control-Max-Age", "86400") 44 + 45 + // Handle preflight requests 46 + if r.Method == "OPTIONS" { 47 + w.WriteHeader(http.StatusOK) 48 + return 49 + } 50 + 51 + next.ServeHTTP(w, r) 52 + }) 53 + }