修复跨域问题,修改一些问题
This commit is contained in:
@@ -134,11 +134,12 @@ func EditChallenge(c *gin.Context) {
|
||||
DB := db.DBsnctf
|
||||
id := c.Param("id")
|
||||
cid, _ := strconv.Atoi(id)
|
||||
var request ChallengeRequest
|
||||
var request ChallengeRequeststrings
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
c.JSON(400, gin.H{"code": 400, "msg": "Request format wrong!"})
|
||||
return
|
||||
}
|
||||
visable, _ := strconv.Atoi(request.Visible)
|
||||
attachmentString := strings.Join(request.Attachment, ",")
|
||||
hintString := strings.Join(request.Hints, ",")
|
||||
challenge := &CrDb{
|
||||
@@ -150,7 +151,7 @@ func EditChallenge(c *gin.Context) {
|
||||
Category: request.Category,
|
||||
Tags: request.Tags,
|
||||
Hints: hintString,
|
||||
Visible: request.Visible,
|
||||
Visible: visable,
|
||||
}
|
||||
err := DB.Debug().Table("challenge").Where("id = ?", cid).Updates(challenge).Error
|
||||
if err != nil {
|
||||
|
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
func Initrouter() {
|
||||
router := gin.Default()
|
||||
router.Use(Cors())
|
||||
api_v1 := router.Group("/api/v1")
|
||||
|
||||
//公共接口(无需登录)
|
||||
@@ -93,3 +94,24 @@ func Initrouter() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 跨域
|
||||
func Cors() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
method := c.Request.Method
|
||||
origin := c.Request.Header.Get("Origin") //请求头部
|
||||
if origin != "" {
|
||||
c.Header("Access-Control-Allow-Origin", origin) //"http://172.20.10.10:8081")
|
||||
c.Header("Access-Control-Allow-Headers", "Content-Category, AccessToken, X-CSRF-Token, Authorization, Token, Content-Type")
|
||||
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PATCH, DELETE, PUT")
|
||||
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Category")
|
||||
c.Header("Access-Control-Max-Age", "172800")
|
||||
c.Header("Access-Control-Allow-Credentials", "true")
|
||||
}
|
||||
if method == "OPTIONS" {
|
||||
c.JSON(200, "ok")
|
||||
}
|
||||
// 处理请求
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
@@ -12,6 +12,19 @@ type ChallengeRequest struct {
|
||||
Hints []string `json:"hints"`
|
||||
Visible int `json:"visible"`
|
||||
}
|
||||
|
||||
type ChallengeRequeststrings struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Score int `json:"score" binding:"required"`
|
||||
Flag string `json:"flag"` // 暂时一个题只能一个flag
|
||||
Description string `json:"description"`
|
||||
Attachment []string `json:"attachment"`
|
||||
Category string `json:"category" binding:"required"`
|
||||
Tags string `json:"tags"`
|
||||
Hints []string `json:"hints"`
|
||||
Visible string `json:"visible"`
|
||||
}
|
||||
|
||||
type ChallengeRequest2 struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
|
Reference in New Issue
Block a user