58 lines
1.7 KiB
Go
58 lines
1.7 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, logger log.Logger) *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)
|
||
|
||
// 添加SSE流式聊天的自定义路由
|
||
srv.HandleFunc("/api/chat/stream", customerService.HandleStreamChat)
|
||
|
||
// WebSocket 聊天路由(/api/chat/ws)
|
||
srv.HandleFunc("/api/chat/ws", func(w nethttp.ResponseWriter, r *nethttp.Request) {
|
||
upgrader := websocket.Upgrader{CheckOrigin: func(r *nethttp.Request) bool { return true }}
|
||
conn, err := upgrader.Upgrade(w, r, nil)
|
||
if err != nil {
|
||
return
|
||
}
|
||
customerService.HandleWebSocketChat(conn)
|
||
})
|
||
|
||
route := srv.Route("/api")
|
||
// 商品查询 Agent 路由
|
||
route.POST("/agents/product/query", agentService.HandleProductQuery)
|
||
// 订单诊断工作流(直接调用流水线输出)
|
||
// route.POST("/api/workflow/order/diagnosis", customerService.HandleOrderDiagnosis)
|
||
|
||
return srv
|
||
}
|