🎉 初始化项目
This commit is contained in:
232
server/api/v1/app/ai_preset.go
Normal file
232
server/api/v1/app/ai_preset.go
Normal file
@@ -0,0 +1,232 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.echol.cn/loser/ai_proxy/server/global"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/app/request"
|
||||
commonRequest "git.echol.cn/loser/ai_proxy/server/model/common/request"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/common/response"
|
||||
"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 AiPresetApi struct{}
|
||||
|
||||
var aiPresetService = service.ServiceGroupApp.AppServiceGroup.AiPresetService
|
||||
|
||||
// CreateAiPreset 创建AI预设
|
||||
// @Tags AiPreset
|
||||
// @Summary 创建AI预设
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.CreateAiPresetRequest true "预设信息"
|
||||
// @Success 200 {object} response.Response{data=app.AiPreset,msg=string} "创建成功"
|
||||
// @Router /app/preset [post]
|
||||
func (a *AiPresetApi) CreateAiPreset(c *gin.Context) {
|
||||
var req request.CreateAiPresetRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 尝试获取用户ID,如果没有则使用0(公开访问)
|
||||
userId := uint(0)
|
||||
if id := utils.GetUserID(c); id > 0 {
|
||||
userId = id
|
||||
}
|
||||
preset, err := aiPresetService.CreateAiPreset(userId, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建预设失败!", zap.Error(err))
|
||||
response.FailWithMessage("创建预设失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(preset, c)
|
||||
}
|
||||
|
||||
// DeleteAiPreset 删除AI预设
|
||||
// @Tags AiPreset
|
||||
// @Summary 删除AI预设
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param id path uint true "预设ID"
|
||||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||||
// @Router /app/preset/:id [delete]
|
||||
func (a *AiPresetApi) DeleteAiPreset(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
// 尝试获取用户ID,如果没有则使用0(公开访问)
|
||||
userId := uint(0)
|
||||
if uid := utils.GetUserID(c); uid > 0 {
|
||||
userId = uid
|
||||
}
|
||||
|
||||
err := aiPresetService.DeleteAiPreset(uint(id), userId)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("删除预设失败!", zap.Error(err))
|
||||
response.FailWithMessage("删除预设失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// UpdateAiPreset 更新AI预设
|
||||
// @Tags AiPreset
|
||||
// @Summary 更新AI预设
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.UpdateAiPresetRequest true "预设信息"
|
||||
// @Success 200 {object} response.Response{data=app.AiPreset,msg=string} "更新成功"
|
||||
// @Router /app/preset [put]
|
||||
func (a *AiPresetApi) UpdateAiPreset(c *gin.Context) {
|
||||
var req request.UpdateAiPresetRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 尝试获取用户ID,如果没有则使用0(公开访问)
|
||||
userId := uint(0)
|
||||
if id := utils.GetUserID(c); id > 0 {
|
||||
userId = id
|
||||
}
|
||||
preset, err := aiPresetService.UpdateAiPreset(userId, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("更新预设失败!", zap.Error(err))
|
||||
response.FailWithMessage("更新预设失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(preset, c)
|
||||
}
|
||||
|
||||
// GetAiPreset 获取AI预设详情
|
||||
// @Tags AiPreset
|
||||
// @Summary 获取AI预设详情
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param id path uint true "预设ID"
|
||||
// @Success 200 {object} response.Response{data=app.AiPreset,msg=string} "获取成功"
|
||||
// @Router /app/preset/:id [get]
|
||||
func (a *AiPresetApi) GetAiPreset(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
// 尝试获取用户ID,如果没有则使用0(公开访问)
|
||||
userId := uint(0)
|
||||
if uid := utils.GetUserID(c); uid > 0 {
|
||||
userId = uid
|
||||
}
|
||||
|
||||
preset, err := aiPresetService.GetAiPreset(uint(id), userId)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取预设失败!", zap.Error(err))
|
||||
response.FailWithMessage("获取预设失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(preset, c)
|
||||
}
|
||||
|
||||
// GetAiPresetList 获取AI预设列表
|
||||
// @Tags AiPreset
|
||||
// @Summary 获取AI预设列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data query request.PageInfo true "分页信息"
|
||||
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
||||
// @Router /app/preset/list [get]
|
||||
func (a *AiPresetApi) GetAiPresetList(c *gin.Context) {
|
||||
var pageInfo commonRequest.PageInfo
|
||||
err := c.ShouldBindQuery(&pageInfo)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 尝试获取用户ID,如果没有则使用0(公开访问)
|
||||
userId := uint(0)
|
||||
if id := utils.GetUserID(c); id > 0 {
|
||||
userId = id
|
||||
}
|
||||
list, total, err := aiPresetService.GetAiPresetList(userId, pageInfo)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取预设列表失败!", zap.Error(err))
|
||||
response.FailWithMessage("获取预设列表失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: pageInfo.Page,
|
||||
PageSize: pageInfo.PageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
|
||||
// ImportAiPreset 导入AI预设(支持SillyTavern格式)
|
||||
// @Tags AiPreset
|
||||
// @Summary 导入AI预设
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.ImportAiPresetRequest true "导入数据"
|
||||
// @Success 200 {object} response.Response{data=app.AiPreset,msg=string} "导入成功"
|
||||
// @Router /app/preset/import [post]
|
||||
func (a *AiPresetApi) ImportAiPreset(c *gin.Context) {
|
||||
var req request.ImportAiPresetRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 尝试获取用户ID,如果没有则使用0(公开访问)
|
||||
userId := uint(0)
|
||||
if id := utils.GetUserID(c); id > 0 {
|
||||
userId = id
|
||||
}
|
||||
preset, err := aiPresetService.ImportAiPreset(userId, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("导入预设失败!", zap.Error(err))
|
||||
response.FailWithMessage("导入预设失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(preset, c)
|
||||
}
|
||||
|
||||
// ExportAiPreset 导出AI预设
|
||||
// @Tags AiPreset
|
||||
// @Summary 导出AI预设
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param id path uint true "预设ID"
|
||||
// @Success 200 {object} map[string]interface{} "导出数据"
|
||||
// @Router /app/preset/:id/export [get]
|
||||
func (a *AiPresetApi) ExportAiPreset(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
// 尝试获取用户ID,如果没有则使用0(公开访问)
|
||||
userId := uint(0)
|
||||
if uid := utils.GetUserID(c); uid > 0 {
|
||||
userId = uid
|
||||
}
|
||||
|
||||
data, err := aiPresetService.ExportAiPreset(uint(id), userId)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("导出预设失败!", zap.Error(err))
|
||||
response.FailWithMessage("导出预设失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, data)
|
||||
}
|
||||
131
server/api/v1/app/ai_preset_binding.go
Normal file
131
server/api/v1/app/ai_preset_binding.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/ai_proxy/server/global"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/app/request"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/common/response"
|
||||
"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 PresetBindingApi struct{}
|
||||
|
||||
var presetBindingService = service.ServiceGroupApp.AppServiceGroup.PresetBindingService
|
||||
|
||||
// CreateBinding 创建预设绑定
|
||||
// @Tags App
|
||||
// @Summary 创建预设绑定
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.CreateBindingRequest true "绑定信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
||||
// @Router /app/binding [post]
|
||||
func (a *PresetBindingApi) CreateBinding(c *gin.Context) {
|
||||
var req request.CreateBindingRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err := presetBindingService.CreateBinding(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建绑定失败!", zap.Error(err))
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("创建成功", c)
|
||||
}
|
||||
|
||||
// UpdateBinding 更新预设绑定
|
||||
// @Tags App
|
||||
// @Summary 更新预设绑定
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.UpdateBindingRequest true "绑定信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
||||
// @Router /app/binding [put]
|
||||
func (a *PresetBindingApi) UpdateBinding(c *gin.Context) {
|
||||
var req request.UpdateBindingRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err := presetBindingService.UpdateBinding(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// DeleteBinding 删除预设绑定
|
||||
// @Tags App
|
||||
// @Summary 删除预设绑定
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param id path uint true "绑定ID"
|
||||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||||
// @Router /app/binding/:id [delete]
|
||||
func (a *PresetBindingApi) DeleteBinding(c *gin.Context) {
|
||||
id, err := utils.StringToUint(c.Param("id"))
|
||||
if err != nil {
|
||||
response.FailWithMessage("无效的ID", c)
|
||||
return
|
||||
}
|
||||
|
||||
err = presetBindingService.DeleteBinding(id)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// GetBindingList 获取绑定列表
|
||||
// @Tags App
|
||||
// @Summary 获取绑定列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param page query int false "页码"
|
||||
// @Param pageSize query int false "每页数量"
|
||||
// @Param providerId query uint false "提供商ID"
|
||||
// @Param presetId query uint false "预设ID"
|
||||
// @Success 200 {object} response.Response{data=response.PageResult} "获取成功"
|
||||
// @Router /app/binding/list [get]
|
||||
func (a *PresetBindingApi) GetBindingList(c *gin.Context) {
|
||||
var req request.GetBindingListRequest
|
||||
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 := presetBindingService.GetBindingList(&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)
|
||||
}
|
||||
185
server/api/v1/app/ai_provider.go
Normal file
185
server/api/v1/app/ai_provider.go
Normal file
@@ -0,0 +1,185 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.echol.cn/loser/ai_proxy/server/global"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/app/request"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/common/response"
|
||||
"git.echol.cn/loser/ai_proxy/server/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AiProviderApi struct{}
|
||||
|
||||
var aiProviderService = service.ServiceGroupApp.AppServiceGroup.AiProviderService
|
||||
|
||||
// CreateAiProvider 创建AI提供商
|
||||
// @Tags AiProvider
|
||||
// @Summary 创建AI提供商
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.CreateAiProviderRequest true "提供商信息"
|
||||
// @Success 200 {object} response.Response{data=app.AiProvider,msg=string} "创建成功"
|
||||
// @Router /app/provider [post]
|
||||
func (a *AiProviderApi) CreateAiProvider(c *gin.Context) {
|
||||
var req request.CreateAiProviderRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
provider, err := aiProviderService.CreateAiProvider(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("创建提供商失败!", zap.Error(err))
|
||||
response.FailWithMessage("创建提供商失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(provider, c)
|
||||
}
|
||||
|
||||
// DeleteAiProvider 删除AI提供商
|
||||
// @Tags AiProvider
|
||||
// @Summary 删除AI提供商
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param id path uint true "提供商ID"
|
||||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||||
// @Router /app/provider/:id [delete]
|
||||
func (a *AiProviderApi) DeleteAiProvider(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
|
||||
err := aiProviderService.DeleteAiProvider(uint(id))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("删除提供商失败!", zap.Error(err))
|
||||
response.FailWithMessage("删除提供商失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// UpdateAiProvider 更新AI提供商
|
||||
// @Tags AiProvider
|
||||
// @Summary 更新AI提供商
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.UpdateAiProviderRequest true "提供商信息"
|
||||
// @Success 200 {object} response.Response{data=app.AiProvider,msg=string} "更新成功"
|
||||
// @Router /app/provider [put]
|
||||
func (a *AiProviderApi) UpdateAiProvider(c *gin.Context) {
|
||||
var req request.UpdateAiProviderRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
provider, err := aiProviderService.UpdateAiProvider(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("更新提供商失败!", zap.Error(err))
|
||||
response.FailWithMessage("更新提供商失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(provider, c)
|
||||
}
|
||||
|
||||
// GetAiProvider 获取AI提供商详情
|
||||
// @Tags AiProvider
|
||||
// @Summary 获取AI提供商详情
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param id path uint true "提供商ID"
|
||||
// @Success 200 {object} response.Response{data=app.AiProvider,msg=string} "获取成功"
|
||||
// @Router /app/provider/:id [get]
|
||||
func (a *AiProviderApi) GetAiProvider(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
|
||||
provider, err := aiProviderService.GetAiProvider(uint(id))
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取提供商失败!", zap.Error(err))
|
||||
response.FailWithMessage("获取提供商失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(provider, c)
|
||||
}
|
||||
|
||||
// GetAiProviderList 获取AI提供商列表
|
||||
// @Tags AiProvider
|
||||
// @Summary 获取AI提供商列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Success 200 {object} response.Response{data=[]app.AiProvider,msg=string} "获取成功"
|
||||
// @Router /app/provider/list [get]
|
||||
func (a *AiProviderApi) GetAiProviderList(c *gin.Context) {
|
||||
list, err := aiProviderService.GetAiProviderList()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取提供商列表失败!", zap.Error(err))
|
||||
response.FailWithMessage("获取提供商列表失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(list, c)
|
||||
}
|
||||
|
||||
// TestConnection 测试连接
|
||||
// @Tags AiProvider
|
||||
// @Summary 测试AI提供商连接
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.TestConnectionRequest true "连接信息"
|
||||
// @Success 200 {object} response.Response{data=response.TestConnectionResponse,msg=string} "测试成功"
|
||||
// @Router /app/provider/test [post]
|
||||
func (a *AiProviderApi) TestConnection(c *gin.Context) {
|
||||
var req request.TestConnectionRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := aiProviderService.TestConnection(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("测试连接失败!", zap.Error(err))
|
||||
response.FailWithMessage("测试连接失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(result, c)
|
||||
}
|
||||
|
||||
// GetModels 获取模型列表
|
||||
// @Tags AiProvider
|
||||
// @Summary 获取AI提供商的模型列表
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.GetModelsRequest true "提供商信息"
|
||||
// @Success 200 {object} response.Response{data=[]response.ModelInfo,msg=string} "获取成功"
|
||||
// @Router /app/provider/models [post]
|
||||
func (a *AiProviderApi) GetModels(c *gin.Context) {
|
||||
var req request.GetModelsRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
models, err := aiProviderService.GetModels(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取模型列表失败!", zap.Error(err))
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(models, c)
|
||||
}
|
||||
52
server/api/v1/app/ai_proxy.go
Normal file
52
server/api/v1/app/ai_proxy.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/ai_proxy/server/global"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/app/request"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/common/response"
|
||||
"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 AiProxyApi struct{}
|
||||
|
||||
var aiProxyService = service.ServiceGroupApp.AppServiceGroup.AiProxyService
|
||||
|
||||
// ChatCompletions OpenAI兼容的聊天补全接口
|
||||
// @Tags AiProxy
|
||||
// @Summary 聊天补全(OpenAI兼容)
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.ChatCompletionRequest true "聊天请求"
|
||||
// @Success 200 {object} response.ChatCompletionResponse "聊天响应"
|
||||
// @Router /v1/chat/completions [post]
|
||||
func (a *AiProxyApi) ChatCompletions(c *gin.Context) {
|
||||
var req request.ChatCompletionRequest
|
||||
err := c.ShouldBindJSON(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
userId := utils.GetUserID(c)
|
||||
|
||||
// 处理流式响应
|
||||
if req.Stream {
|
||||
// TODO: 实现流式响应
|
||||
response.FailWithMessage("流式响应暂未实现", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理普通响应
|
||||
resp, err := aiProxyService.ProcessChatCompletion(c.Request.Context(), userId, &req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("处理聊天请求失败!", zap.Error(err))
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, resp)
|
||||
}
|
||||
8
server/api/v1/app/enter.go
Normal file
8
server/api/v1/app/enter.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package app
|
||||
|
||||
type ApiGroup struct {
|
||||
AiPresetApi AiPresetApi
|
||||
AiProviderApi AiProviderApi
|
||||
AiProxyApi AiProxyApi
|
||||
PresetBindingApi PresetBindingApi
|
||||
}
|
||||
13
server/api/v1/enter.go
Normal file
13
server/api/v1/enter.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/ai_proxy/server/api/v1/app"
|
||||
"git.echol.cn/loser/ai_proxy/server/api/v1/system"
|
||||
)
|
||||
|
||||
var ApiGroupApp = new(ApiGroup)
|
||||
|
||||
type ApiGroup struct {
|
||||
SystemApiGroup system.ApiGroup
|
||||
AppApiGroup app.ApiGroup
|
||||
}
|
||||
6
server/api/v1/system/enter.go
Normal file
6
server/api/v1/system/enter.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package system
|
||||
|
||||
type ApiGroup struct {
|
||||
UserApi UserApi
|
||||
ApiApi ApiApi
|
||||
}
|
||||
149
server/api/v1/system/sys_api.go
Normal file
149
server/api/v1/system/sys_api.go
Normal file
@@ -0,0 +1,149 @@
|
||||
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)
|
||||
}
|
||||
199
server/api/v1/system/sys_user.go
Normal file
199
server/api/v1/system/sys_user.go
Normal file
@@ -0,0 +1,199 @@
|
||||
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 UserApi struct{}
|
||||
|
||||
var userService = service.ServiceGroupApp.SystemServiceGroup.UserService
|
||||
|
||||
// Login 用户登录
|
||||
// @Tags System
|
||||
// @Summary 用户登录
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.LoginRequest true "用户名, 密码"
|
||||
// @Success 200 {object} response.Response{data=response.LoginResponse} "登录成功"
|
||||
// @Router /v1/system/user/login [post]
|
||||
func (u *UserApi) Login(c *gin.Context) {
|
||||
var req request.LoginRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := userService.Login(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("登录失败!", zap.Error(err))
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(resp, c)
|
||||
}
|
||||
|
||||
// Register 用户注册
|
||||
// @Tags System
|
||||
// @Summary 用户注册
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.RegisterRequest true "用户信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "注册成功"
|
||||
// @Router /v1/system/user/register [post]
|
||||
func (u *UserApi) Register(c *gin.Context) {
|
||||
var req request.RegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
_, err := userService.Register(&req)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("注册失败!", zap.Error(err))
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("注册成功", c)
|
||||
}
|
||||
|
||||
// GetUserInfo 获取用户信息
|
||||
// @Tags System
|
||||
// @Summary 获取用户信息
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Success 200 {object} response.Response{data=response.UserInfo} "获取成功"
|
||||
// @Router /v1/system/user/info [get]
|
||||
func (u *UserApi) GetUserInfo(c *gin.Context) {
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
info, err := userService.GetUserInfo(userID)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(info, c)
|
||||
}
|
||||
|
||||
// GetUserList 获取用户列表
|
||||
// @Tags System
|
||||
// @Summary 获取用户列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param page query int false "页码"
|
||||
// @Param pageSize query int false "每页数量"
|
||||
// @Success 200 {object} response.Response{data=response.PageResult} "获取成功"
|
||||
// @Router /v1/system/user/list [get]
|
||||
func (u *UserApi) GetUserList(c *gin.Context) {
|
||||
page := utils.GetIntQuery(c, "page", 1)
|
||||
pageSize := utils.GetIntQuery(c, "pageSize", 10)
|
||||
|
||||
list, total, err := userService.GetUserList(page, pageSize)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithDetailed(response.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
}, "获取成功", c)
|
||||
}
|
||||
|
||||
// UpdateUser 更新用户
|
||||
// @Tags System
|
||||
// @Summary 更新用户
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.UpdateUserRequest true "用户信息"
|
||||
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
||||
// @Router /v1/system/user [put]
|
||||
func (u *UserApi) UpdateUser(c *gin.Context) {
|
||||
var req request.UpdateUserRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
err := userService.UpdateUser(&req)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
|
||||
// DeleteUser 删除用户
|
||||
// @Tags System
|
||||
// @Summary 删除用户
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param id path uint true "用户ID"
|
||||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||||
// @Router /v1/system/user/:id [delete]
|
||||
func (u *UserApi) DeleteUser(c *gin.Context) {
|
||||
userID := utils.GetUintParam(c, "id")
|
||||
|
||||
err := userService.DeleteUser(userID)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
|
||||
// GetAPIKey 获取API密钥
|
||||
// @Tags System
|
||||
// @Summary 获取API密钥
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Success 200 {object} response.Response{data=string} "获取成功"
|
||||
// @Router /v1/system/user/apikey [get]
|
||||
func (u *UserApi) GetAPIKey(c *gin.Context) {
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
apiKey, err := userService.GetAPIKey(userID)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(gin.H{"apiKey": apiKey}, c)
|
||||
}
|
||||
|
||||
// RegenerateAPIKey 重新生成API密钥
|
||||
// @Tags System
|
||||
// @Summary 重新生成API密钥
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Success 200 {object} response.Response{data=string} "生成成功"
|
||||
// @Router /v1/system/user/apikey/regenerate [post]
|
||||
func (u *UserApi) RegenerateAPIKey(c *gin.Context) {
|
||||
userID := utils.GetUserID(c)
|
||||
|
||||
apiKey, err := userService.RegenerateAPIKey(userID)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
response.OkWithData(gin.H{"apiKey": apiKey}, c)
|
||||
}
|
||||
Reference in New Issue
Block a user