diff --git a/server/feishu-sync/etc/feishu-sync.yaml b/server/feishu-sync/etc/feishu-sync.yaml new file mode 100644 index 00000000..03a43b25 --- /dev/null +++ b/server/feishu-sync/etc/feishu-sync.yaml @@ -0,0 +1,10 @@ +Name: feishu-sync +Host: 0.0.0.0 +Port: 9925 +Timeout: 15000 #服务超时时间(毫秒) +SourceMysql: fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen +SourceRabbitMq: "" +Auth: + AccessSecret: fusen2023 + AccessExpire: 2592000 + RefreshAfter: 1592000 diff --git a/server/feishu-sync/feishu-sync.go b/server/feishu-sync/feishu-sync.go new file mode 100644 index 00000000..140e58b8 --- /dev/null +++ b/server/feishu-sync/feishu-sync.go @@ -0,0 +1,37 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + "time" + + "fusenapi/utils/auth" + "fusenapi/utils/fsconfig" + + "fusenapi/server/feishu-sync/internal/config" + "fusenapi/server/feishu-sync/internal/handler" + "fusenapi/server/feishu-sync/internal/svc" + + "github.com/zeromicro/go-zero/rest" +) + +var configFile = flag.String("f", "etc/feishu-sync.yaml", "the config file") + +func main() { + flag.Parse() + + var c config.Config + fsconfig.StartNacosConfig(*configFile, &c, nil) + + 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/feishu-sync/internal/config/config.go b/server/feishu-sync/internal/config/config.go new file mode 100644 index 00000000..2c9df9ad --- /dev/null +++ b/server/feishu-sync/internal/config/config.go @@ -0,0 +1,13 @@ +package config + +import ( + "fusenapi/server/feishu-sync/internal/types" + "github.com/zeromicro/go-zero/rest" +) + +type Config struct { + rest.RestConf + SourceMysql string + Auth types.Auth + SourceRabbitMq string +} diff --git a/server/feishu-sync/internal/handler/routes.go b/server/feishu-sync/internal/handler/routes.go new file mode 100644 index 00000000..f78e236b --- /dev/null +++ b/server/feishu-sync/internal/handler/routes.go @@ -0,0 +1,22 @@ +// Code generated by goctl. DO NOT EDIT. +package handler + +import ( + "net/http" + + "fusenapi/server/feishu-sync/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/feishu/ticket_webhook", + Handler: TicketWebhookHandler(serverCtx), + }, + }, + ) +} diff --git a/server/feishu-sync/internal/handler/ticketwebhookhandler.go b/server/feishu-sync/internal/handler/ticketwebhookhandler.go new file mode 100644 index 00000000..5780ca2a --- /dev/null +++ b/server/feishu-sync/internal/handler/ticketwebhookhandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/feishu-sync/internal/logic" + "fusenapi/server/feishu-sync/internal/svc" + "fusenapi/server/feishu-sync/internal/types" +) + +func TicketWebhookHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.TicketWebhookReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewTicketWebhookLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.TicketWebhook(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/feishu-sync/internal/logic/ticketwebhooklogic.go b/server/feishu-sync/internal/logic/ticketwebhooklogic.go new file mode 100644 index 00000000..e4afb84c --- /dev/null +++ b/server/feishu-sync/internal/logic/ticketwebhooklogic.go @@ -0,0 +1,43 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/feishu-sync/internal/svc" + "fusenapi/server/feishu-sync/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type TicketWebhookLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewTicketWebhookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TicketWebhookLogic { + return &TicketWebhookLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *TicketWebhookLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *TicketWebhookLogic) TicketWebhook(req *types.TicketWebhookReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *TicketWebhookLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/feishu-sync/internal/svc/servicecontext.go b/server/feishu-sync/internal/svc/servicecontext.go new file mode 100644 index 00000000..6e5b8149 --- /dev/null +++ b/server/feishu-sync/internal/svc/servicecontext.go @@ -0,0 +1,27 @@ +package svc + +import ( + "fusenapi/initalize" + "fusenapi/model/gmodel" + "fusenapi/server/feishu-sync/internal/config" + "gorm.io/gorm" +) + +type ServiceContext struct { + Config config.Config + + 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, + AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), + } +} diff --git a/server/feishu-sync/internal/types/types.go b/server/feishu-sync/internal/types/types.go new file mode 100644 index 00000000..87b1c618 --- /dev/null +++ b/server/feishu-sync/internal/types/types.go @@ -0,0 +1,88 @@ +// Code generated by goctl. DO NOT EDIT. +package types + +import ( + "fusenapi/utils/basic" +) + +type TicketWebhookReq struct { + Ts string `json:"ts"` //webhook时间 + Uuid string `json:"uuid"` //事件唯一标识 + Token string `json:"token"` //即Verification Token + Event Event `json:"event"` //事件 +} + +type Event struct { + AppId string `json:"app_id"` + AppTicket string `json:"app_ticket"` + Type string `json:"type"` +} + +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:"total_count"` + PageCount int64 `json:"page_count"` + CurrentPage int `json:"current_page"` + PerPage int `json:"per_page"` +} + +// 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/feishu-sync.api b/server_api/feishu-sync.api index ed243758..207ea8c3 100644 --- a/server_api/feishu-sync.api +++ b/server_api/feishu-sync.api @@ -9,3 +9,21 @@ info ( import "basic.api" +service feishu-sync { + //飞书ticket webhook事件接口 + @handler TicketWebhookHandler + post /api/feishu/ticket_webhook (TicketWebhookReq) returns (response); +} + +type TicketWebhookReq { + Ts string `json:"ts"` //webhook时间 + Uuid string `json:"uuid"` //事件唯一标识 + Token string `json:"token"` //即Verification Token + Event Event `json:"event"` //事件 +} + +type Event { + AppId string `json:"app_id"` + AppTicket string `json:"app_ticket"` + Type string `json:"type"` +} \ No newline at end of file