fix: harden platform runtime and menu navigation
This commit is contained in:
27
backend/api/internal/middleware/cors.go
Normal file
27
backend/api/internal/middleware/cors.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Package middleware 定义平台 API 自有的 HTTP 中间件。
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
allowedHeaders = "Origin, Content-Length, Content-Type, Workspace, Request-Id, Authorization, Token"
|
||||
allowedMethods = "GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS"
|
||||
)
|
||||
|
||||
// Cors 允许平台前端跨域调用 API,包括状态更新使用的 PATCH 方法。
|
||||
func Cors() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
ctx.Header("Access-Control-Allow-Origin", "*")
|
||||
ctx.Header("Access-Control-Allow-Headers", allowedHeaders)
|
||||
ctx.Header("Access-Control-Allow-Methods", allowedMethods)
|
||||
if ctx.Request.Method == http.MethodOptions {
|
||||
ctx.AbortWithStatus(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user