[优化] response 结构调整

This commit is contained in:
ryan
2026-06-05 11:32:10 +08:00
parent a5257be319
commit 4dc4c745c8
9 changed files with 164 additions and 96 deletions
@@ -0,0 +1,82 @@
package response
import (
"net/http"
"github.com/gin-gonic/gin"
)
const invalidParamsMessage = "参数错误"
// RespondSuccess sends a successful response with data
func RespondSuccess(c *gin.Context, data any) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": data,
})
}
// RespondSuccessWithExtras sends a successful response with data and extra fields
func RespondSuccessWithExtras(c *gin.Context, data any, extras gin.H) {
payload := gin.H{
"success": true,
"message": "",
"data": data,
}
for key, value := range extras {
payload[key] = value
}
c.JSON(http.StatusOK, payload)
}
// RespondSuccessMessage sends a successful response with a custom message
func RespondSuccessMessage(c *gin.Context, message string) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": message,
})
}
// RespondFailure sends a failed response with http.StatusOK and a failure message
func RespondFailure(c *gin.Context, message string) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": message,
})
}
// RespondBadRequest sends a bad request response (400)
func RespondBadRequest(c *gin.Context, message string) {
if message == "" {
message = invalidParamsMessage
}
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": message,
})
}
// RespondUnauthorized sends an unauthorized response (401)
func RespondUnauthorized(c *gin.Context, message string) {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": message,
})
}
// RespondForbidden sends a forbidden response (403)
func RespondForbidden(c *gin.Context, message string) {
c.JSON(http.StatusForbidden, gin.H{
"success": false,
"message": message,
})
}
// RespondErrorWithStatus sends a response with target HTTP status code and a message
func RespondErrorWithStatus(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"success": false,
"message": message,
})
}
+8 -36
View File
@@ -4,63 +4,35 @@ import (
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"openflare/common/response"
)
const invalidParamsMessage = "参数错误"
func respondSuccess(c *gin.Context, data any) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": data,
})
response.RespondSuccess(c, data)
}
func respondSuccessWithExtras(c *gin.Context, data any, extras gin.H) {
payload := gin.H{
"success": true,
"message": "",
"data": data,
}
for key, value := range extras {
payload[key] = value
}
c.JSON(http.StatusOK, payload)
response.RespondSuccessWithExtras(c, data, extras)
}
func respondSuccessMessage(c *gin.Context, message string) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": message,
})
response.RespondSuccessMessage(c, message)
}
func respondFailure(c *gin.Context, message string) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": message,
})
response.RespondFailure(c, message)
}
func respondBadRequest(c *gin.Context, message string) {
if message == "" {
message = invalidParamsMessage
}
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": message,
})
response.RespondBadRequest(c, message)
}
func respondUnauthorized(c *gin.Context, message string) {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": message,
})
response.RespondUnauthorized(c, message)
}
func decodeJSONBody(body io.Reader, target any) error {
+3 -9
View File
@@ -2,7 +2,7 @@ package middleware
import (
"github.com/gin-gonic/gin"
"net/http"
"openflare/common/response"
"openflare/service"
)
@@ -11,10 +11,7 @@ func AgentAuth() func(c *gin.Context) {
token := c.GetHeader("X-Agent-Token")
node, err := service.AuthenticateAccessToken(token)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,Agent Token 无效",
})
response.RespondUnauthorized(c, "无权进行此操作,Agent Token 无效")
c.Abort()
return
}
@@ -32,10 +29,7 @@ func AgentRegisterAuth() func(c *gin.Context) {
return
}
if err := service.ValidateDiscoveryToken(token); err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,注册 Token 无效",
})
response.RespondUnauthorized(c, "无权进行此操作,注册 Token 无效")
c.Abort()
return
}
+8 -29
View File
@@ -1,8 +1,8 @@
package middleware
import (
"net/http"
"openflare/common"
"openflare/common/response"
"openflare/model"
jwt "github.com/appleboy/gin-jwt/v2"
@@ -14,20 +14,14 @@ const OpenFlareTokenHeader = "OpenFlare-Token"
func authHelper(c *gin.Context, minRole int) {
tokenStr := c.GetHeader(OpenFlareTokenHeader)
if tokenStr == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,未登录或 token 无效",
})
response.RespondUnauthorized(c, "无权进行此操作,未登录或 token 无效")
c.Abort()
return
}
token, err := JWTMiddleware.ParseTokenString(tokenStr)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,token 无效: " + err.Error(),
})
response.RespondUnauthorized(c, "无权进行此操作,token 无效: "+err.Error())
c.Abort()
return
}
@@ -35,10 +29,7 @@ func authHelper(c *gin.Context, minRole int) {
claims := jwt.ExtractClaimsFromToken(token)
id, ok := claims["id"].(float64)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,token 格式错误",
})
response.RespondUnauthorized(c, "无权进行此操作,token 格式错误")
c.Abort()
return
}
@@ -47,37 +38,25 @@ func authHelper(c *gin.Context, minRole int) {
dbErr := model.DB.Select([]string{"id", "username", "display_name", "role", "status", "token"}).
First(dbUser, "id = ?", int(id)).Error
if dbErr != nil || dbUser.Username == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,用户不存在",
})
response.RespondUnauthorized(c, "无权进行此操作,用户不存在")
c.Abort()
return
}
if dbUser.Token != tokenStr {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,token 已失效或已登出",
})
response.RespondUnauthorized(c, "无权进行此操作,token 已失效或已登出")
c.Abort()
return
}
if dbUser.Status == common.UserStatusDisabled {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "用户已被封禁",
})
response.RespondFailure(c, "用户已被封禁")
c.Abort()
return
}
if int(dbUser.Role) < minRole {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无权进行此操作,权限不足",
})
response.RespondFailure(c, "无权进行此操作,权限不足")
c.Abort()
return
}
+2 -4
View File
@@ -3,6 +3,7 @@ package middleware
import (
"log"
"openflare/common"
"openflare/common/response"
"openflare/model"
"time"
@@ -57,10 +58,7 @@ func InitJWTMiddleware() {
return data != nil
},
Unauthorized: func(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"success": false,
"message": "无权进行此操作,未登录或 token 无效: " + message,
})
response.RespondErrorWithStatus(c, code, "无权进行此操作,未登录或 token 无效: "+message)
},
TokenLookup: "header: OpenFlare-Token",
TokenHeadName: "", // Empty for raw token value directly
+3 -9
View File
@@ -2,7 +2,7 @@ package middleware
import (
"github.com/gin-gonic/gin"
"net/http"
"openflare/common/response"
"openflare/service"
)
@@ -13,18 +13,12 @@ func RelayAuth() func(c *gin.Context) {
token := c.GetHeader("X-Agent-Token")
node, err := service.AuthenticateAccessToken(token)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,Agent Token 无效",
})
response.RespondUnauthorized(c, "无权进行此操作,Agent Token 无效")
c.Abort()
return
}
if node.NodeType != "tunnel_relay" {
c.JSON(http.StatusForbidden, gin.H{
"success": false,
"message": "此节点不是 TunnelRelay 类型",
})
response.RespondForbidden(c, "此节点不是 TunnelRelay 类型")
c.Abort()
return
}
+3 -9
View File
@@ -1,7 +1,7 @@
package middleware
import (
"net/http"
"openflare/common/response"
"openflare/service"
"github.com/gin-gonic/gin"
@@ -15,18 +15,12 @@ func TunnelAuth() func(c *gin.Context) {
token := c.GetHeader("X-Tunnel-Token")
node, err := service.AuthenticateAccessToken(token)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,Tunnel Token 无效",
})
response.RespondUnauthorized(c, "无权进行此操作,Tunnel Token 无效")
c.Abort()
return
}
if node.NodeType != "tunnel_client" {
c.JSON(http.StatusForbidden, gin.H{
"success": false,
"message": "此节点不是 TunnelClient 类型",
})
response.RespondForbidden(c, "此节点不是 TunnelClient 类型")
c.Abort()
return
}