From 41e935bc65530716de039abe3de88c3555823f23 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Thu, 31 Aug 2023 16:17:32 +0800 Subject: [PATCH] fix --- server/info/etc/info.yaml | 11 ++ server/info/info.go | 36 +++++++ server/info/internal/config/config.go | 14 +++ server/info/internal/handler/infohandler.go | 35 +++++++ server/info/internal/handler/routes.go | 22 ++++ server/info/internal/logic/infologic.go | 105 ++++++++++++++++++++ server/info/internal/svc/servicecontext.go | 32 ++++++ server/info/internal/types/types.go | 79 +++++++++++++++ server_api/info.api | 21 ++++ 9 files changed, 355 insertions(+) create mode 100644 server/info/etc/info.yaml create mode 100644 server/info/info.go create mode 100644 server/info/internal/config/config.go create mode 100644 server/info/internal/handler/infohandler.go create mode 100644 server/info/internal/handler/routes.go create mode 100644 server/info/internal/logic/infologic.go create mode 100644 server/info/internal/svc/servicecontext.go create mode 100644 server/info/internal/types/types.go create mode 100644 server_api/info.api diff --git a/server/info/etc/info.yaml b/server/info/etc/info.yaml new file mode 100644 index 00000000..a0afb690 --- /dev/null +++ b/server/info/etc/info.yaml @@ -0,0 +1,11 @@ +Name: info +Host: 0.0.0.0 +Port: 9988 +Timeout: 15000 #服务超时时间(毫秒) +ReplicaId: 200 +SourceMysql: fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen +SourceRabbitMq: amqp://rabbit001:rabbit001129@110.41.19.98:5672 +Auth: + AccessSecret: fusen2023 + AccessExpire: 2592000 + RefreshAfter: 1592000 diff --git a/server/info/info.go b/server/info/info.go new file mode 100644 index 00000000..4571550d --- /dev/null +++ b/server/info/info.go @@ -0,0 +1,36 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + "time" + + "fusenapi/utils/auth" + + "fusenapi/server/info/internal/config" + "fusenapi/server/info/internal/handler" + "fusenapi/server/info/internal/svc" + + "github.com/zeromicro/go-zero/core/conf" + "github.com/zeromicro/go-zero/rest" +) + +var configFile = flag.String("f", "etc/info.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + conf.MustLoad(*configFile, &c) + c.Timeout = int64(time.Second * 15) + server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { + })) + defer server.Stop() + + ctx := svc.NewServiceContext(c) + handler.RegisterHandlers(server, ctx) + + fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) + server.Start() +} diff --git a/server/info/internal/config/config.go b/server/info/internal/config/config.go new file mode 100644 index 00000000..37f72e53 --- /dev/null +++ b/server/info/internal/config/config.go @@ -0,0 +1,14 @@ +package config + +import ( + "fusenapi/server/info/internal/types" + + "github.com/zeromicro/go-zero/rest" +) + +type Config struct { + rest.RestConf + SourceMysql string + Auth types.Auth + SourceRabbitMq string +} diff --git a/server/info/internal/handler/infohandler.go b/server/info/internal/handler/infohandler.go new file mode 100644 index 00000000..dd754672 --- /dev/null +++ b/server/info/internal/handler/infohandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/info/internal/logic" + "fusenapi/server/info/internal/svc" + "fusenapi/server/info/internal/types" +) + +func InfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.UserInfoRequest + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewInfoLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.Info(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/info/internal/handler/routes.go b/server/info/internal/handler/routes.go new file mode 100644 index 00000000..7fd32e51 --- /dev/null +++ b/server/info/internal/handler/routes.go @@ -0,0 +1,22 @@ +// Code generated by goctl. DO NOT EDIT. +package handler + +import ( + "net/http" + + "fusenapi/server/info/internal/svc" + + "github.com/zeromicro/go-zero/rest" +) + +func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodPost, + Path: "/api/info/user", + Handler: InfoHandler(serverCtx), + }, + }, + ) +} diff --git a/server/info/internal/logic/infologic.go b/server/info/internal/logic/infologic.go new file mode 100644 index 00000000..bad3438a --- /dev/null +++ b/server/info/internal/logic/infologic.go @@ -0,0 +1,105 @@ +package logic + +import ( + "encoding/json" + "fmt" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "strings" + + "context" + + "fusenapi/server/info/internal/svc" + "fusenapi/server/info/internal/types" + + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" +) + +type InfoLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InfoLogic { + return &InfoLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *InfoLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 这个与表名强关联 +var ModuleTable map[string]string = map[string]string{ + "userinfo": "fs_user_info", + "material": "fs_user_material", +} + +type InfoType struct { + Id int64 `json:"id"` + Module string `json:"module"` + Metadata string `json:"metadata"` + // CreateAt time.Time `json:"ctime"` +} + +func (l *InfoLogic) Info(req *types.UserInfoRequest, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + var cond string + switch userinfo.GetIdType() { + case auth.IDTYPE_User: + cond = fmt.Sprintf("user_id = %d", userinfo.UserId) + case auth.IDTYPE_Guest: + cond = fmt.Sprintf("guest_id = %d", userinfo.GuestId) + default: + cond = "user_id = 0 and guest_id = 0" + } + + var metadict map[string]any = make(map[string]any) + + for _, module := range req.Module { + mlist := strings.Split(module, ".") + if len(mlist) == 0 { + return resp.SetStatusWithMessage(basic.CodeApiErr, fmt.Sprintf("%s format error", module)) + } + + mtable := mlist[0] + tname, ok := ModuleTable[mtable] + if !ok { + return resp.SetStatusWithMessage(basic.CodeApiErr, fmt.Sprintf("%s format error, table %s not found", module, tname)) + } + + sqlstr := fmt.Sprintf("select id, module, metadata from %s where %s ", tname, cond) + var info InfoType + err := l.svcCtx.MysqlConn.Raw(sqlstr).Scan(&info).Error + if err == gorm.ErrRecordNotFound { + continue + } + + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error()) + } + + var metadata map[string]any = make(map[string]any) + err = json.Unmarshal([]byte(info.Metadata), &metadata) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error()) + } + metadict[info.Module] = metadata + } + + return resp.SetStatus(basic.CodeOK, metadict) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *InfoLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/info/internal/svc/servicecontext.go b/server/info/internal/svc/servicecontext.go new file mode 100644 index 00000000..4c2a1431 --- /dev/null +++ b/server/info/internal/svc/servicecontext.go @@ -0,0 +1,32 @@ +package svc + +import ( + "fusenapi/server/info/internal/config" + "fusenapi/shared" + + "fusenapi/initalize" + "fusenapi/model/gmodel" + + "gorm.io/gorm" +) + +type ServiceContext struct { + Config config.Config + + SharedState *shared.SharedState + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + RabbitMq *initalize.RabbitMqHandle +} + +func NewServiceContext(c config.Config) *ServiceContext { + conn := initalize.InitMysql(c.SourceMysql) + + return &ServiceContext{ + Config: c, + MysqlConn: conn, + SharedState: nil, + AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), + } +} diff --git a/server/info/internal/types/types.go b/server/info/internal/types/types.go new file mode 100644 index 00000000..6edce7d4 --- /dev/null +++ b/server/info/internal/types/types.go @@ -0,0 +1,79 @@ +// Code generated by goctl. DO NOT EDIT. +package types + +import ( + "fusenapi/utils/basic" +) + +type UserInfoRequest struct { + Module []string `json:"module"` +} + +type Request struct { +} + +type Response struct { + Code int `json:"code"` + Message string `json:"msg"` + Data interface{} `json:"data"` +} + +type Auth struct { + AccessSecret string `json:"accessSecret"` + AccessExpire int64 `json:"accessExpire"` + RefreshAfter int64 `json:"refreshAfter"` +} + +type File struct { + Filename string `fsfile:"filename"` + Header map[string][]string `fsfile:"header"` + Size int64 `fsfile:"size"` + Data []byte `fsfile:"data"` +} + +type Meta struct { + TotalCount int64 `json:"totalCount"` + PageCount int64 `json:"pageCount"` + CurrentPage int `json:"currentPage"` + PerPage int `json:"perPage"` +} + +// Set 设置Response的Code和Message值 +func (resp *Response) Set(Code int, Message string) *Response { + return &Response{ + Code: Code, + Message: Message, + } +} + +// Set 设置整个Response +func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response { + return &Response{ + Code: Code, + Message: Message, + Data: Data, + } +} + +// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 +func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp +} + +// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数 +func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response { + newResp := &Response{ + Code: sr.Code, + Message: msg, + } + if len(data) == 1 { + newResp.Data = data[0] + } + return newResp +} diff --git a/server_api/info.api b/server_api/info.api new file mode 100644 index 00000000..966ab832 --- /dev/null +++ b/server_api/info.api @@ -0,0 +1,21 @@ +syntax = "v1" + +info ( + title: // TODO: add title + desc: // TODO: add description + author: "" + email: "" +) + +import "basic.api" + +service info { + @handler InfoHandler + post /api/info/user(UserInfoRequest) returns (response); +} + +type ( + UserInfoRequest { + Module []string `json:"module"` + } +) \ No newline at end of file