62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package server
|
||
|
||
import (
|
||
v1 "eino-project/api/customer/v1"
|
||
"eino-project/internal/conf"
|
||
"eino-project/internal/service"
|
||
nethttp "net/http"
|
||
|
||
"github.com/gorilla/websocket"
|
||
|
||
"github.com/go-kratos/kratos/v2/log"
|
||
"github.com/go-kratos/kratos/v2/middleware/recovery"
|
||
"github.com/go-kratos/kratos/v2/transport/http"
|
||
)
|
||
|
||
// NewHTTPServer new an HTTP server.
|
||
func NewHTTPServer(
|
||
c *conf.Server,
|
||
customerService *service.CustomerService,
|
||
agentService *service.AgentService,
|
||
workflowService *service.WorkflowService,
|
||
logger log.Logger,
|
||
chatService *service.ChatService,
|
||
) *http.Server {
|
||
var opts = []http.ServerOption{
|
||
http.Middleware(
|
||
recovery.Recovery(),
|
||
),
|
||
}
|
||
if c.Http.Network != "" {
|
||
opts = append(opts, http.Network(c.Http.Network))
|
||
}
|
||
if c.Http.Addr != "" {
|
||
opts = append(opts, http.Address(c.Http.Addr))
|
||
}
|
||
if c.Http.Timeout != nil {
|
||
opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration()))
|
||
}
|
||
srv := http.NewServer(opts...)
|
||
|
||
// 注册HTTP路由
|
||
v1.RegisterCustomerServiceHTTPServer(srv, customerService)
|
||
|
||
route := srv.Route("/api")
|
||
// 商品查询 Agent 路由
|
||
route.POST("/agents/product/query", agentService.HandleProductQuery)
|
||
// 产品工作流(Graph 编排)
|
||
route.POST("/workflow/product/query", workflowService.HandleProductWorkflow)
|
||
// WebSocket 聊天路由(/api/chat/ws)
|
||
route.GET("/chat/ws", func(ctx http.Context) error {
|
||
upgrader := websocket.Upgrader{CheckOrigin: func(r *nethttp.Request) bool { return true }}
|
||
conn, err := upgrader.Upgrade(ctx.Response(), ctx.Request(), nil)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
chatService.HandleWebSocketChat(conn)
|
||
return nil
|
||
})
|
||
|
||
return srv
|
||
}
|