2025-11-05 15:06:07 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-06 09:31:51 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-contrib/cors"
|
2025-11-05 15:06:07 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-06 09:31:51 +00:00
|
|
|
// CORSMiddleware handles Cross-Origin Resource Sharing for frontend integration
|
2025-11-05 15:06:07 +00:00
|
|
|
func CORSMiddleware() gin.HandlerFunc {
|
2025-11-06 09:31:51 +00:00
|
|
|
return cors.New(cors.Config{
|
|
|
|
|
AllowOrigins: []string{
|
|
|
|
|
"http://localhost:3000", // Next.js development server
|
|
|
|
|
"http://localhost:3001", // Alternative frontend port
|
|
|
|
|
"https://localhost:3000", // HTTPS development
|
|
|
|
|
"https://localhost:3001", // HTTPS alternative
|
|
|
|
|
// Add production domains here when deploying
|
|
|
|
|
},
|
|
|
|
|
AllowMethods: []string{
|
|
|
|
|
"GET",
|
|
|
|
|
"POST",
|
|
|
|
|
"PUT",
|
|
|
|
|
"DELETE",
|
|
|
|
|
"OPTIONS",
|
|
|
|
|
"PATCH",
|
|
|
|
|
},
|
|
|
|
|
AllowHeaders: []string{
|
|
|
|
|
"Origin",
|
|
|
|
|
"Content-Type",
|
|
|
|
|
"Content-Length",
|
|
|
|
|
"Accept-Encoding",
|
|
|
|
|
"X-CSRF-Token",
|
|
|
|
|
"Authorization",
|
|
|
|
|
"Accept",
|
|
|
|
|
"Cache-Control",
|
|
|
|
|
"X-Requested-With",
|
|
|
|
|
},
|
|
|
|
|
ExposeHeaders: []string{
|
|
|
|
|
"Content-Length",
|
|
|
|
|
"Content-Type",
|
|
|
|
|
},
|
|
|
|
|
AllowCredentials: true,
|
|
|
|
|
MaxAge: 12 * time.Hour, // Preflight cache duration
|
|
|
|
|
})
|
2025-11-05 15:06:07 +00:00
|
|
|
}
|