package api import ( "net/http" "github.com/gin-gonic/gin" ) func CORSMiddleware() gin.HandlerFunc { return func(c *gin.Context) { origin := c.GetHeader("Origin") if origin != "" { // In production, frontend is same-origin so browsers don't send Origin header. // In dev, Next.js on :3000 needs CORS to reach Go on :3721. // Allow all origins since this is a local-first application. c.Header("Access-Control-Allow-Origin", origin) } c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Relay-Token") c.Header("Access-Control-Allow-Credentials", "true") if c.Request.Method == http.MethodOptions { c.AbortWithStatus(http.StatusNoContent) return } c.Next() } }