ai-courseware/eino-project/internal/server/http.go

62 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}