150 lines
3.8 KiB
Go
150 lines
3.8 KiB
Go
package system
|
|
|
|
import (
|
|
"git.echol.cn/loser/ai_proxy/server/global"
|
|
"git.echol.cn/loser/ai_proxy/server/model/common/response"
|
|
"git.echol.cn/loser/ai_proxy/server/model/system/request"
|
|
"git.echol.cn/loser/ai_proxy/server/service"
|
|
"git.echol.cn/loser/ai_proxy/server/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type ApiApi struct{}
|
|
|
|
var apiService = service.ServiceGroupApp.SystemServiceGroup.ApiService
|
|
|
|
// CreateApi 创建API
|
|
// @Tags System
|
|
// @Summary 创建API
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body request.CreateApiRequest true "API信息"
|
|
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
|
// @Router /v1/system/api [post]
|
|
func (a *ApiApi) CreateApi(c *gin.Context) {
|
|
var req request.CreateApiRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
err := apiService.CreateApi(&req)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("创建API失败!", zap.Error(err))
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("创建成功", c)
|
|
}
|
|
|
|
// UpdateApi 更新API
|
|
// @Tags System
|
|
// @Summary 更新API
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param data body request.UpdateApiRequest true "API信息"
|
|
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
|
// @Router /v1/system/api [put]
|
|
func (a *ApiApi) UpdateApi(c *gin.Context) {
|
|
var req request.UpdateApiRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
err := apiService.UpdateApi(&req)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("更新成功", c)
|
|
}
|
|
|
|
// DeleteApi 删除API
|
|
// @Tags System
|
|
// @Summary 删除API
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param id path uint true "API ID"
|
|
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
|
// @Router /v1/system/api/:id [delete]
|
|
func (a *ApiApi) DeleteApi(c *gin.Context) {
|
|
id := utils.GetUintParam(c, "id")
|
|
|
|
err := apiService.DeleteApi(id)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("删除成功", c)
|
|
}
|
|
|
|
// GetApiList 获取API列表
|
|
// @Tags System
|
|
// @Summary 获取API列表
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param page query int false "页码"
|
|
// @Param pageSize query int false "每页数量"
|
|
// @Param path query string false "API路径"
|
|
// @Param apiGroup query string false "API分组"
|
|
// @Param method query string false "请求方法"
|
|
// @Success 200 {object} response.Response{data=response.PageResult} "获取成功"
|
|
// @Router /v1/system/api/list [get]
|
|
func (a *ApiApi) GetApiList(c *gin.Context) {
|
|
var req request.GetApiListRequest
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
if req.Page == 0 {
|
|
req.Page = 1
|
|
}
|
|
if req.PageSize == 0 {
|
|
req.PageSize = 10
|
|
}
|
|
|
|
list, total, err := apiService.GetApiList(&req)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithDetailed(response.PageResult{
|
|
List: list,
|
|
Total: total,
|
|
Page: req.Page,
|
|
PageSize: req.PageSize,
|
|
}, "获取成功", c)
|
|
}
|
|
|
|
// GetApiById 根据ID获取API
|
|
// @Tags System
|
|
// @Summary 根据ID获取API
|
|
// @Security ApiKeyAuth
|
|
// @accept application/json
|
|
// @Produce application/json
|
|
// @Param id path uint true "API ID"
|
|
// @Success 200 {object} response.Response{data=response.ApiInfo} "获取成功"
|
|
// @Router /v1/system/api/:id [get]
|
|
func (a *ApiApi) GetApiById(c *gin.Context) {
|
|
id := utils.GetUintParam(c, "id")
|
|
|
|
info, err := apiService.GetApiById(id)
|
|
if err != nil {
|
|
response.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
response.OkWithData(info, c)
|
|
}
|