Compare commits
No commits in common. "ec21193f66a9611a3ecf7e4f1f4d94b02adb4666" and "b344ad77b666b4f3217aa21098c8a50cf1d13f78" have entirely different histories.
ec21193f66
...
b344ad77b6
|
|
@ -17,5 +17,4 @@ var ProviderSetBiz = wire.NewSet(
|
||||||
handle.NewHandle,
|
handle.NewHandle,
|
||||||
do.NewDo,
|
do.NewDo,
|
||||||
do.NewHandle,
|
do.NewHandle,
|
||||||
NewTaskBiz,
|
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
package biz
|
|
||||||
|
|
||||||
import (
|
|
||||||
"ai_scheduler/internal/data/impl"
|
|
||||||
"ai_scheduler/internal/data/model"
|
|
||||||
"ai_scheduler/internal/entitys"
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"xorm.io/builder"
|
|
||||||
|
|
||||||
"ai_scheduler/internal/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TaskBiz struct {
|
|
||||||
taskRepo *impl.TaskImpl
|
|
||||||
conf *config.Config
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTaskBiz(conf *config.Config, taskRepo *impl.TaskImpl) *TaskBiz {
|
|
||||||
return &TaskBiz{
|
|
||||||
taskRepo: taskRepo,
|
|
||||||
conf: conf,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// taskList 功能列表
|
|
||||||
func (t *TaskBiz) TaskList(ctx context.Context, req *entitys.TaskRequest) (list []model.AiTask, err error) {
|
|
||||||
cond := builder.NewCond()
|
|
||||||
cond = cond.And(builder.Eq{"status": 1})
|
|
||||||
|
|
||||||
cond = cond.And(builder.Eq{"sys_id": req.SysId})
|
|
||||||
err = t.taskRepo.GetRangeToMapStruct(&cond, &list)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
@ -18,7 +18,3 @@ type SessionListRequest struct {
|
||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
PageSize int `json:"page_size"`
|
PageSize int `json:"page_size"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TaskRequest struct {
|
|
||||||
SysId int32 `json:"sys_id"`
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -10,22 +10,14 @@ import (
|
||||||
"github.com/gofiber/fiber/v2/middleware/recover"
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPServer struct {
|
|
||||||
app *fiber.App
|
|
||||||
service *services.ChatService
|
|
||||||
session *services.SessionService
|
|
||||||
gateway *gateway.Gateway
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHTTPServer(
|
func NewHTTPServer(
|
||||||
service *services.ChatService,
|
service *services.ChatService,
|
||||||
session *services.SessionService,
|
session *services.SessionService,
|
||||||
task *services.TaskService,
|
|
||||||
gateway *gateway.Gateway,
|
gateway *gateway.Gateway,
|
||||||
) *fiber.App {
|
) *fiber.App {
|
||||||
//构建 server
|
//构建 server
|
||||||
app := initRoute()
|
app := initRoute()
|
||||||
router.SetupRoutes(app, service, session, task, gateway)
|
router.SetupRoutes(app, service, session, gateway)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,15 +14,8 @@ import (
|
||||||
"github.com/gofiber/websocket/v2"
|
"github.com/gofiber/websocket/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RouterServer struct {
|
|
||||||
app *fiber.App
|
|
||||||
service *services.ChatService
|
|
||||||
session *services.SessionService
|
|
||||||
gateway *gateway.Gateway
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetupRoutes 设置路由
|
// SetupRoutes 设置路由
|
||||||
func SetupRoutes(app *fiber.App, ChatService *services.ChatService, sessionService *services.SessionService, task *services.TaskService, gateway *gateway.Gateway) {
|
func SetupRoutes(app *fiber.App, ChatService *services.ChatService, sessionService *services.SessionService, gateway *gateway.Gateway) {
|
||||||
app.Use(func(c *fiber.Ctx) error {
|
app.Use(func(c *fiber.Ctx) error {
|
||||||
// 设置 CORS 头
|
// 设置 CORS 头
|
||||||
c.Set("Access-Control-Allow-Origin", "*")
|
c.Set("Access-Control-Allow-Origin", "*")
|
||||||
|
|
@ -37,40 +30,9 @@ func SetupRoutes(app *fiber.App, ChatService *services.ChatService, sessionServi
|
||||||
// 继续处理后续中间件或路由
|
// 继续处理后续中间件或路由
|
||||||
return c.Next()
|
return c.Next()
|
||||||
})
|
})
|
||||||
//socket
|
routerHttp(app, sessionService, gateway)
|
||||||
routerSocket(app, ChatService)
|
routerSocket(app, ChatService)
|
||||||
|
|
||||||
r := app.Group("api/v1/")
|
|
||||||
registerResponse(r)
|
|
||||||
// 注册 CORS 中间件
|
|
||||||
r.Get("/health", func(c *fiber.Ctx) error {
|
|
||||||
c.Response().SetBody([]byte("1"))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
r.Post("/session/init", sessionService.SessionInit) // 会话初始化,不存在则创建,存在则返回会话ID和默认条数会话历史
|
|
||||||
r.Post("/session/list", sessionService.SessionList)
|
|
||||||
r.Post("/sys/tasks", task.Tasks)
|
|
||||||
//广播
|
|
||||||
r.Get("/broadcast", func(ctx *fiber.Ctx) error {
|
|
||||||
action := ctx.Query("action")
|
|
||||||
uid := ctx.Query("uid")
|
|
||||||
msg := ctx.Query("msg")
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case "sendToAll":
|
|
||||||
gateway.SendToAll([]byte(msg))
|
|
||||||
return ctx.SendString("sent to all")
|
|
||||||
case "sendToUid":
|
|
||||||
if uid == "" {
|
|
||||||
return ctx.Status(400).SendString("missing uid")
|
|
||||||
}
|
|
||||||
gateway.SendToUid(uid, []byte(msg))
|
|
||||||
return ctx.SendString(fmt.Sprintf("sent to uid %s", uid))
|
|
||||||
default:
|
|
||||||
return ctx.Status(400).SendString("unknown action")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func routerSocket(app *fiber.App, chatService *services.ChatService) {
|
func routerSocket(app *fiber.App, chatService *services.ChatService) {
|
||||||
|
|
@ -96,6 +58,39 @@ var bufferPool = &sync.Pool{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func routerHttp(app *fiber.App, sessionService *services.SessionService, gateway *gateway.Gateway) {
|
||||||
|
r := app.Group("api/v1/")
|
||||||
|
registerResponse(r)
|
||||||
|
// 注册 CORS 中间件
|
||||||
|
r.Get("/health", func(c *fiber.Ctx) error {
|
||||||
|
c.Response().SetBody([]byte("1"))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
r.Post("/session/init", sessionService.SessionInit) // 会话初始化,不存在则创建,存在则返回会话ID和默认条数会话历史
|
||||||
|
r.Post("/session/list", sessionService.SessionList)
|
||||||
|
//广播
|
||||||
|
r.Get("/broadcast", func(ctx *fiber.Ctx) error {
|
||||||
|
action := ctx.Query("action")
|
||||||
|
uid := ctx.Query("uid")
|
||||||
|
msg := ctx.Query("msg")
|
||||||
|
|
||||||
|
switch action {
|
||||||
|
case "sendToAll":
|
||||||
|
gateway.SendToAll([]byte(msg))
|
||||||
|
return ctx.SendString("sent to all")
|
||||||
|
case "sendToUid":
|
||||||
|
if uid == "" {
|
||||||
|
return ctx.Status(400).SendString("missing uid")
|
||||||
|
}
|
||||||
|
gateway.SendToUid(uid, []byte(msg))
|
||||||
|
return ctx.SendString(fmt.Sprintf("sent to uid %s", uid))
|
||||||
|
default:
|
||||||
|
return ctx.Status(400).SendString("unknown action")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func registerResponse(router fiber.Router) {
|
func registerResponse(router fiber.Router) {
|
||||||
// 自定义返回
|
// 自定义返回
|
||||||
router.Use(func(c *fiber.Ctx) error {
|
router.Use(func(c *fiber.Ctx) error {
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,7 @@ package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"ai_scheduler/internal/gateway"
|
"ai_scheduler/internal/gateway"
|
||||||
|
|
||||||
"github.com/google/wire"
|
"github.com/google/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ProviderSetServices = wire.NewSet(NewChatService, NewSessionService, gateway.NewGateway, NewTaskService)
|
var ProviderSetServices = wire.NewSet(NewChatService, NewSessionService, gateway.NewGateway)
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package services
|
||||||
import (
|
import (
|
||||||
"ai_scheduler/internal/biz"
|
"ai_scheduler/internal/biz"
|
||||||
"ai_scheduler/internal/entitys"
|
"ai_scheduler/internal/entitys"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
package services
|
|
||||||
|
|
||||||
import (
|
|
||||||
"ai_scheduler/internal/biz"
|
|
||||||
"ai_scheduler/internal/entitys"
|
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TaskService struct {
|
|
||||||
taskBiz *biz.TaskBiz
|
|
||||||
chatBiz *biz.ChatHistoryBiz
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTaskService(sessionBiz *biz.SessionBiz, taskBiz *biz.TaskBiz) *TaskService {
|
|
||||||
return &TaskService{
|
|
||||||
taskBiz: taskBiz,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tasks 任务列表
|
|
||||||
func (s *TaskService) Tasks(c *fiber.Ctx) error {
|
|
||||||
req := &entitys.TaskRequest{}
|
|
||||||
if err := c.BodyParser(req); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := s.taskBiz.TaskList(c.Context(), req)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(result)
|
|
||||||
}
|
|
||||||
|
|
@ -89,15 +89,6 @@ func (k DataTemp) GetRange(cond *builder.Cond) (list []map[string]interface{}, e
|
||||||
return list, err
|
return list, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k DataTemp) GetRangeToMapStruct(cond *builder.Cond, data interface{}) (err error) {
|
|
||||||
var (
|
|
||||||
query, _ = builder.ToBoundSQL(*cond)
|
|
||||||
model = k.Db.Model(k.Model).Where(query)
|
|
||||||
)
|
|
||||||
err = model.Find(data).Error
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k DataTemp) GetOneBySearch(cond *builder.Cond) (data map[string]interface{}, err error) {
|
func (k DataTemp) GetOneBySearch(cond *builder.Cond) (data map[string]interface{}, err error) {
|
||||||
query, _ := builder.ToBoundSQL(*cond)
|
query, _ := builder.ToBoundSQL(*cond)
|
||||||
err = k.Db.Model(k.Model).Where(query).Limit(1).Find(&data).Error
|
err = k.Db.Model(k.Model).Where(query).Limit(1).Find(&data).Error
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue