fusenapi/server/collection/internal/logic/testailogic.go
laodaming 8dc98d4aca fix
2023-10-25 14:27:47 +08:00

58 lines
1.6 KiB
Go

package logic
import (
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/curl"
"math/rand"
"net/http"
"time"
"context"
"fusenapi/server/collection/internal/svc"
"fusenapi/server/collection/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type TestAiLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewTestAiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TestAiLogic {
return &TestAiLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *TestAiLogic) TestAi(req *types.TestAiReq, userinfo *auth.UserInfo, w http.ResponseWriter) (resp *basic.Response) {
lenAiHost := len(l.svcCtx.Config.BLMService.Urls)
if lenAiHost == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "ai host list is 0")
}
hostIndex := rand.Intn(lenAiHost) - 1
var resultBLM constants.BLMServiceUrlResult
logx.Info("正在请求:" + l.svcCtx.Config.BLMService.Urls[hostIndex])
err := curl.NewClient(l.ctx, &curl.Config{
BaseUrl: l.svcCtx.Config.BLMService.Urls[hostIndex],
Url: constants.BLMServiceUrlLogoCombine,
RequireTimeout: time.Second * 15,
}).PostJson(req.Data, &resultBLM)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "request failed")
}
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *TestAiLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }