mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-08 08:36:37 +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 proxy_route
|
||||
|
||||
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, errProxyRouteNotFound)
|
||||
return true
|
||||
}
|
||||
compat.Fail(c, err.Error())
|
||||
return true
|
||||
return apiutil.AbortNotFoundIfMissing(c, err, errProxyRouteNotFound)
|
||||
}
|
||||
|
||||
// GetProxyRoutes 列出全部代理规则。
|
||||
// @Summary 获取代理规则列表
|
||||
// @Description 返回所有代理规则配置,需要管理员权限
|
||||
// @Tags openflare-proxy-route
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Success 200 {object} response.Any{data=[]proxy_route.View} "代理规则列表"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或不存在"
|
||||
// @Router /api/v1/custom/openflare/proxy-routes [get]
|
||||
func GetProxyRoutes(c *gin.Context) {
|
||||
routes, err := ListProxyRoutes(c.Request.Context())
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
compat.OK(c, routes)
|
||||
c.JSON(http.StatusOK, response.OK(routes))
|
||||
}
|
||||
|
||||
// GetProxyRouteHandler 获取代理规则详情。
|
||||
// @Summary 获取代理规则详情
|
||||
// @Description 返回指定代理规则的完整配置,需要管理员权限
|
||||
// @Tags openflare-proxy-route
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param id path int true "代理规则 ID"
|
||||
// @Success 200 {object} response.Any{data=proxy_route.View} "代理规则详情"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或规则不存在"
|
||||
// @Router /api/v1/custom/openflare/proxy-routes/{id} [get]
|
||||
func GetProxyRouteHandler(c *gin.Context) {
|
||||
id, ok := compat.IDParam(c)
|
||||
id, ok := apiutil.IDParam(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -42,47 +59,83 @@ func GetProxyRouteHandler(c *gin.Context) {
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
compat.OK(c, route)
|
||||
c.JSON(http.StatusOK, response.OK(route))
|
||||
}
|
||||
|
||||
// CreateProxyRouteHandler 创建代理规则。
|
||||
// @Summary 创建代理规则
|
||||
// @Description 创建新的反向代理规则,需要管理员权限
|
||||
// @Tags openflare-proxy-route
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param body body proxy_route.Input true "代理规则参数"
|
||||
// @Success 200 {object} response.Any{data=proxy_route.View} "创建成功"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或不存在"
|
||||
// @Router /api/v1/custom/openflare/proxy-routes [post]
|
||||
func CreateProxyRouteHandler(c *gin.Context) {
|
||||
var input Input
|
||||
if !compat.BindJSON(c, &input) {
|
||||
if !apiutil.BindJSON(c, &input) {
|
||||
return
|
||||
}
|
||||
route, err := CreateProxyRoute(c.Request.Context(), input)
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
compat.OK(c, route)
|
||||
c.JSON(http.StatusOK, response.OK(route))
|
||||
}
|
||||
|
||||
// UpdateProxyRouteHandler 更新代理规则。
|
||||
// @Summary 更新代理规则
|
||||
// @Description 更新指定代理规则的配置,需要管理员权限
|
||||
// @Tags openflare-proxy-route
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param id path int true "代理规则 ID"
|
||||
// @Param body body proxy_route.Input true "代理规则参数"
|
||||
// @Success 200 {object} response.Any{data=proxy_route.View} "更新成功"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或规则不存在"
|
||||
// @Router /api/v1/custom/openflare/proxy-routes/{id}/update [post]
|
||||
func UpdateProxyRouteHandler(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
|
||||
}
|
||||
route, err := UpdateProxyRoute(c.Request.Context(), id, input)
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
compat.OK(c, route)
|
||||
c.JSON(http.StatusOK, response.OK(route))
|
||||
}
|
||||
|
||||
// DeleteProxyRouteHandler 删除代理规则。
|
||||
// @Summary 删除代理规则
|
||||
// @Description 删除指定代理规则,需要管理员权限
|
||||
// @Tags openflare-proxy-route
|
||||
// @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/proxy-routes/{id}/delete [post]
|
||||
func DeleteProxyRouteHandler(c *gin.Context) {
|
||||
id, ok := compat.IDParam(c)
|
||||
id, ok := apiutil.IDParam(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := DeleteProxyRoute(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