Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into feature/auth

This commit is contained in:
eson
2023-08-08 14:18:45 +08:00
115 changed files with 3189 additions and 504 deletions

View File

@@ -8,7 +8,8 @@ import (
type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
SourceMysql string
Auth types.Auth
ReplicaId uint64
SourceRabbitMq string
}

View File

@@ -11,22 +11,22 @@ import (
"fusenapi/server/render/internal/types"
)
func ToUnityHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
func GetFaceSliceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RequestToUnity
var req types.Request
userinfo, err := basic.RequestParse(w, r, svcCtx.SharedState, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewToUnityLogic(r.Context(), svcCtx)
l := logic.NewGetFaceSliceLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.ToUnity(&req, userinfo)
resp := l.GetFaceSlice(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)

View File

@@ -11,22 +11,22 @@ import (
"fusenapi/server/render/internal/types"
)
func ReadImagesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
func RenderNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RequestReadImages
var req types.RenderNotifyReq
userinfo, err := basic.RequestParse(w, r, svcCtx.SharedState, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewReadImagesLogic(r.Context(), svcCtx)
l := logic.NewRenderNotifyLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.ReadImages(&req, userinfo)
resp := l.RenderNotify(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)

View File

@@ -13,14 +13,14 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodGet,
Path: "/api/render/to-unity",
Handler: ToUnityHandler(serverCtx),
Method: http.MethodPost,
Path: "/api/render/render_notify",
Handler: RenderNotifyHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/api/render/read-images",
Handler: ReadImagesHandler(serverCtx),
Method: http.MethodPost,
Path: "/api/render/get_face_slice",
Handler: GetFaceSliceHandler(serverCtx),
},
},
)

View File

@@ -0,0 +1,69 @@
package logic
import (
"encoding/json"
"errors"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
"strings"
"context"
"fusenapi/server/render/internal/svc"
"fusenapi/server/render/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetFaceSliceLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetFaceSliceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFaceSliceLogic {
return &GetFaceSliceLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *GetFaceSliceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetFaceSliceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *GetFaceSliceLogic) GetFaceSlice(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
if !userinfo.IsUser() && !userinfo.IsGuest() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login or access cookie")
}
//获取用户素材信息
materialInfo, err := l.svcCtx.AllModels.FsUserMaterial.FindLatestOne(l.ctx, userinfo.UserId, userinfo.GuestId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "user material info is not exists")
}
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user material info")
}
if materialInfo.Metadata == nil || *materialInfo.Metadata == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "user material info`Metadata is empty")
}
var info map[string]interface{}
if err = json.Unmarshal([]byte(*materialInfo.Metadata), &info); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "invalid json format of metadata")
}
str := strings.ReplaceAll(constants.RENDER_FACE_SLICE_TEMPLATE_JSON, "{{MainColorFill}}", info["main_color_fill"].(string))
str = strings.ReplaceAll(str, "{{SecondaryColorFill}}", info["secondary_color_fill"].(string))
str = strings.ReplaceAll(str, "{{LogoMaterial}}", info["logo_material"].(string))
var rspInfo interface{}
_ = json.Unmarshal([]byte(str), &rspInfo)
return resp.SetStatusWithMessage(basic.CodeOK, "success", rspInfo)
}

View File

@@ -1,42 +0,0 @@
package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"context"
"fusenapi/server/render/internal/svc"
"fusenapi/server/render/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ReadImagesLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewReadImagesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReadImagesLogic {
return &ReadImagesLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *ReadImagesLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向
// func (l *ReadImagesLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// }
func (l *ReadImagesLogic) ReadImages(req *types.RequestReadImages, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
return resp.SetStatus(basic.CodeOK)
}

View File

@@ -0,0 +1,72 @@
package logic
import (
"context"
"encoding/json"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/websocket_data"
"fusenapi/server/render/internal/svc"
"fusenapi/server/render/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type RenderNotifyLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RenderNotifyLogic {
return &RenderNotifyLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *RenderNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *RenderNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
/*if time.Now().Unix()-120 > req.Time || req.Time > time.Now().Unix() {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param time")
}*/
if req.Info.TaskId == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param task_id")
}
if req.Info.Image == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param image")
}
/* if req.Sign == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param sign")
}*/
//验证签名 sha256
/*notifyByte, _ := json.Marshal(req.Info)
h := sha256.New()
h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time)))
signHex := h.Sum(nil)
sign := hex.EncodeToString(signHex)
if req.Sign != sign {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign")
}*/
data := websocket_data.RenderImageNotify{
TaskId: req.Info.TaskId,
Image: req.Info.Image,
}
d, _ := json.Marshal(data)
if err := l.svcCtx.RabbitMq.SendMsg(constants.RABBIT_MQ_RENDER_RESULT_DATA, d); err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeServiceErr, "failed to send data")
}
return resp.SetStatus(basic.CodeOK)
}

View File

@@ -1,42 +0,0 @@
package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"context"
"fusenapi/server/render/internal/svc"
"fusenapi/server/render/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ToUnityLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewToUnityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ToUnityLogic {
return &ToUnityLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *ToUnityLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向
// func (l *ToUnityLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// }
func (l *ToUnityLogic) ToUnity(req *types.RequestToUnity, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
return resp.SetStatus(basic.CodeOK)
}

View File

@@ -21,6 +21,7 @@ type ServiceContext struct {
MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen
RabbitMq *initalize.RabbitMqHandle
}
func NewServiceContext(c config.Config) *ServiceContext {
@@ -32,6 +33,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
MysqlConn: conn,
SharedState: StateServer,
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil),
}
}

View File

@@ -11,6 +11,17 @@ type RequestToUnity struct {
type RequestReadImages struct {
}
type RenderNotifyReq struct {
Sign string `json:"sign"`
Time int64 `json:"time"`
Info NotifyInfo `json:"info"`
}
type NotifyInfo struct {
TaskId string `json:"task_id"` //任务id
Image string `json:"image"`
}
type Request struct {
}