29 lines
946 B
Go

package middleware
import (
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
// CORS returns a middleware that handles CORS requests
// It only enables CORS in development mode
func CORS() gin.HandlerFunc {
// Check if we're in development mode (you can use an environment variable for this)
if os.Getenv("GIN_MODE") != "release" {
// CORS configuration for development
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://localhost:3000"} // Frontend URL
config.AllowCredentials = true // Important for cookies/auth
config.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}
config.AllowHeaders = []string{"Origin", "Content-Type", "Accept", "Authorization"}
return cors.New(config)
}
// In production, no CORS middleware is needed if frontend and backend are served from the same domain
return func(c *gin.Context) {
c.Next()
}
}