github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/main/middleware/cors.go (about) 1 package middleware 2 3 import ( 4 "net/http" 5 6 "github.com/DapperCollectives/CAST/backend/main/shared" 7 ) 8 9 func UseCors(c shared.Config) func(http.Handler) http.Handler { 10 return func(next http.Handler) http.Handler { 11 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 12 if c.Features["useCorsMiddleware"] { 13 w.Header().Add("Access-Control-Allow-Origin", "*") 14 w.Header().Add("Access-Control-Allow-Headers", "*") 15 16 // handle preflight 17 if r.Method == "OPTIONS" { 18 w.WriteHeader(http.StatusOK) 19 return 20 } 21 } 22 // Call the next handler, which can be another middleware in the chain, or the final handler. 23 next.ServeHTTP(w, r) 24 }) 25 } 26 }