28 lines
766 B
Go
28 lines
766 B
Go
|
|
// 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()
|
|||
|
|
}
|
|||
|
|
}
|