mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-10 17:26:38 +08:00
refactor(openflare): migrate console APIs to v1 and centralize routing
Move OpenFlare management endpoints to /api/v1/custom/openflare with Wavelet response envelopes and Abort* error handling. Keep agent, relay, and flared protocol routes on /api/* with the legacy compat format. - Add apiutil helpers and Swagger annotations for console handlers - Register all OpenFlare routes in internal/router/openflare (not apps) - Switch frontend services to OpenFlareBaseService and v1 paths - Remove dead auth/compat code and legacy-base.service.ts - Update integration tests and changelog
This commit is contained in:
@@ -4,37 +4,54 @@
|
||||
package origin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/openflare/compat"
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/openflare/apiutil"
|
||||
"github.com/Rain-kl/Wavelet/internal/common/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
||||
func handleLogicError(c *gin.Context, err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
compat.Fail(c, errOriginNotFound)
|
||||
return true
|
||||
}
|
||||
compat.Fail(c, err.Error())
|
||||
return true
|
||||
return apiutil.AbortNotFoundIfMissing(c, err, errOriginNotFound)
|
||||
}
|
||||
|
||||
// GetOrigins 列出全部源站。
|
||||
// @Summary 获取源站列表
|
||||
// @Description 返回所有源站及关联代理规则数量,需要管理员权限
|
||||
// @Tags openflare-origin
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Success 200 {object} response.Any{data=[]origin.View} "源站列表"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或不存在"
|
||||
// @Router /api/v1/custom/openflare/origins [get]
|
||||
func GetOrigins(c *gin.Context) {
|
||||
origins, err := ListOrigins(c.Request.Context())
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
compat.OK(c, origins)
|
||||
c.JSON(http.StatusOK, response.OK(origins))
|
||||
}
|
||||
|
||||
// GetOrigin 获取源站详情。
|
||||
// @Summary 获取源站详情
|
||||
// @Description 返回指定源站信息及关联代理规则摘要,需要管理员权限
|
||||
// @Tags openflare-origin
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param id path int true "源站 ID"
|
||||
// @Success 200 {object} response.Any{data=origin.DetailView} "源站详情"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或源站不存在"
|
||||
// @Router /api/v1/custom/openflare/origins/{id} [get]
|
||||
func GetOrigin(c *gin.Context) {
|
||||
id, ok := compat.IDParam(c)
|
||||
id, ok := apiutil.IDParam(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -42,47 +59,83 @@ func GetOrigin(c *gin.Context) {
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
compat.OK(c, detail)
|
||||
c.JSON(http.StatusOK, response.OK(detail))
|
||||
}
|
||||
|
||||
// CreateOriginHandler 创建源站。
|
||||
// @Summary 创建源站
|
||||
// @Description 创建新的上游源站记录,需要管理员权限
|
||||
// @Tags openflare-origin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param body body origin.Input true "源站参数"
|
||||
// @Success 200 {object} response.Any{data=origin.View} "创建成功"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或不存在"
|
||||
// @Router /api/v1/custom/openflare/origins [post]
|
||||
func CreateOriginHandler(c *gin.Context) {
|
||||
var input Input
|
||||
if !compat.BindJSON(c, &input) {
|
||||
if !apiutil.BindJSON(c, &input) {
|
||||
return
|
||||
}
|
||||
origin, err := CreateOrigin(c.Request.Context(), input)
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
compat.OK(c, origin)
|
||||
c.JSON(http.StatusOK, response.OK(origin))
|
||||
}
|
||||
|
||||
// UpdateOriginHandler 更新源站。
|
||||
// @Summary 更新源站
|
||||
// @Description 更新指定源站的配置信息,需要管理员权限
|
||||
// @Tags openflare-origin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param id path int true "源站 ID"
|
||||
// @Param body body origin.Input true "源站参数"
|
||||
// @Success 200 {object} response.Any{data=origin.View} "更新成功"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或源站不存在"
|
||||
// @Router /api/v1/custom/openflare/origins/{id}/update [post]
|
||||
func UpdateOriginHandler(c *gin.Context) {
|
||||
id, ok := compat.IDParam(c)
|
||||
id, ok := apiutil.IDParam(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var input Input
|
||||
if !compat.BindJSON(c, &input) {
|
||||
if !apiutil.BindJSON(c, &input) {
|
||||
return
|
||||
}
|
||||
origin, err := UpdateOrigin(c.Request.Context(), id, input)
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
compat.OK(c, origin)
|
||||
c.JSON(http.StatusOK, response.OK(origin))
|
||||
}
|
||||
|
||||
// DeleteOriginHandler 删除源站。
|
||||
// @Summary 删除源站
|
||||
// @Description 删除指定源站记录,需要管理员权限
|
||||
// @Tags openflare-origin
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param id path int true "源站 ID"
|
||||
// @Success 200 {object} response.Any{data=string} "删除成功"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或源站不存在"
|
||||
// @Router /api/v1/custom/openflare/origins/{id}/delete [post]
|
||||
func DeleteOriginHandler(c *gin.Context) {
|
||||
id, ok := compat.IDParam(c)
|
||||
id, ok := apiutil.IDParam(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := DeleteOrigin(c.Request.Context(), id); handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
compat.OK(c, nil)
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OKNil())
|
||||
}
|
||||
Reference in New Issue
Block a user