From 744e741022b2cbfc8f76f4c6f9bb8a7b60ac6ce6 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 17 Oct 2023 01:18:19 +0800 Subject: [PATCH] refact(mp): remove middleware for shop,media,datadictionary etc api access --- api/mp/dictionary.api | 2 +- api/mp/market/media.api | 2 +- api/mp/market/store.api | 2 +- api/mp/product/product.api | 2 +- api/mp/product/productcategory.api | 2 +- api/mp/product/productstatistics.api | 2 +- .../plugin/listpluginfrontendrouteshandler.go | 21 +++ internal/handler/plugin/listpluginhandler.go | 28 +++ .../handler/plugin/registerpluginhandler.go | 28 +++ internal/handler/routes.go | 174 ++++++++---------- .../system/health/healthcheckhandler.go | 28 +++ .../plugin/listpluginfrontendrouteslogic.go | 30 +++ internal/logic/plugin/listpluginlogic.go | 30 +++ internal/logic/plugin/registerpluginlogic.go | 30 +++ .../logic/system/health/healthchecklogic.go | 30 +++ 15 files changed, 311 insertions(+), 100 deletions(-) create mode 100644 internal/handler/plugin/listpluginfrontendrouteshandler.go create mode 100644 internal/handler/plugin/listpluginhandler.go create mode 100644 internal/handler/plugin/registerpluginhandler.go create mode 100644 internal/handler/system/health/healthcheckhandler.go create mode 100644 internal/logic/plugin/listpluginfrontendrouteslogic.go create mode 100644 internal/logic/plugin/listpluginlogic.go create mode 100644 internal/logic/plugin/registerpluginlogic.go create mode 100644 internal/logic/system/health/healthchecklogic.go diff --git a/api/mp/dictionary.api b/api/mp/dictionary.api index 9d156232..2e29f1c8 100644 --- a/api/mp/dictionary.api +++ b/api/mp/dictionary.api @@ -13,7 +13,7 @@ import "../admin/dictionary.api" @server( group: mp/dictionary prefix: /api/v1/mp/dictionary - middleware: MPCustomerJWTAuth +// middleware: MPCustomerJWTAuth ) service PowerX { diff --git a/api/mp/market/media.api b/api/mp/market/media.api index 9a3c7629..94de3aba 100644 --- a/api/mp/market/media.api +++ b/api/mp/market/media.api @@ -13,7 +13,7 @@ import "../../admin/crm/market/media.api" @server( group: mp/crm/market/media prefix: /api/v1/mp/market - middleware: MPCustomerJWTAuth +// middleware: MPCustomerJWTAuth ) service PowerX { diff --git a/api/mp/market/store.api b/api/mp/market/store.api index 93579731..1a221aab 100644 --- a/api/mp/market/store.api +++ b/api/mp/market/store.api @@ -14,7 +14,7 @@ import "../../admin/crm/market/store.api" @server( group: mp/crm/market/store prefix: /api/v1/mp/market - middleware: MPCustomerJWTAuth +// middleware: MPCustomerJWTAuth ) diff --git a/api/mp/product/product.api b/api/mp/product/product.api index ea34bfd0..44168138 100644 --- a/api/mp/product/product.api +++ b/api/mp/product/product.api @@ -14,7 +14,7 @@ import "../../admin/crm/product/product.api" @server( group: mp/crm/product prefix: /api/v1/mp/product - middleware: MPCustomerJWTAuth +// middleware: MPCustomerJWTAuth ) diff --git a/api/mp/product/productcategory.api b/api/mp/product/productcategory.api index b41c795f..03d1e879 100644 --- a/api/mp/product/productcategory.api +++ b/api/mp/product/productcategory.api @@ -14,7 +14,7 @@ import "../../admin/crm/product/productstatistics.api" @server( group: mp/crm/product prefix: /api/v1/mp/product - middleware: MPCustomerJWTAuth +// middleware: MPCustomerJWTAuth ) service PowerX { diff --git a/api/mp/product/productstatistics.api b/api/mp/product/productstatistics.api index be6f9ff7..8957e029 100644 --- a/api/mp/product/productstatistics.api +++ b/api/mp/product/productstatistics.api @@ -11,7 +11,7 @@ info( @server( group: mp/crm/product/productstatistics prefix: /api/v1/mp/product - middleware: MPCustomerJWTAuth +// middleware: MPCustomerJWTAuth ) service PowerX { diff --git a/internal/handler/plugin/listpluginfrontendrouteshandler.go b/internal/handler/plugin/listpluginfrontendrouteshandler.go new file mode 100644 index 00000000..227547ae --- /dev/null +++ b/internal/handler/plugin/listpluginfrontendrouteshandler.go @@ -0,0 +1,21 @@ +package plugin + +import ( + "net/http" + + "PowerX/internal/logic/plugin" + "PowerX/internal/svc" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func ListPluginFrontendRoutesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + l := plugin.NewListPluginFrontendRoutesLogic(r.Context(), svcCtx) + resp, err := l.ListPluginFrontendRoutes() + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/internal/handler/plugin/listpluginhandler.go b/internal/handler/plugin/listpluginhandler.go new file mode 100644 index 00000000..c741f529 --- /dev/null +++ b/internal/handler/plugin/listpluginhandler.go @@ -0,0 +1,28 @@ +package plugin + +import ( + "net/http" + + "PowerX/internal/logic/plugin" + "PowerX/internal/svc" + "PowerX/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func ListPluginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.ListPluginRequest + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := plugin.NewListPluginLogic(r.Context(), svcCtx) + resp, err := l.ListPlugin(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/internal/handler/plugin/registerpluginhandler.go b/internal/handler/plugin/registerpluginhandler.go new file mode 100644 index 00000000..04d56f3b --- /dev/null +++ b/internal/handler/plugin/registerpluginhandler.go @@ -0,0 +1,28 @@ +package plugin + +import ( + "net/http" + + "PowerX/internal/logic/plugin" + "PowerX/internal/svc" + "PowerX/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func RegisterPluginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.RegisterPluginRequest + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := plugin.NewRegisterPluginLogic(r.Context(), svcCtx) + resp, err := l.RegisterPlugin(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/internal/handler/routes.go b/internal/handler/routes.go index b94efa5f..31a75d34 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -58,6 +58,8 @@ import ( mpcrmtradeorder "PowerX/internal/handler/mp/crm/trade/order" mpcrmtradepayment "PowerX/internal/handler/mp/crm/trade/payment" mpdictionary "PowerX/internal/handler/mp/dictionary" + plugin "PowerX/internal/handler/plugin" + systemhealth "PowerX/internal/handler/system/health" webcustomerauth "PowerX/internal/handler/web/customer/auth" webcustomerauthoa "PowerX/internal/handler/web/customer/auth/oa" webinfoorganizationcategory "PowerX/internal/handler/web/infoorganization/category" @@ -1524,31 +1526,28 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { ) server.AddRoutes( - rest.WithMiddlewares( - []rest.Middleware{serverCtx.MPCustomerJWTAuth}, - []rest.Route{ - { - Method: http.MethodGet, - Path: "/types/page-list", - Handler: mpdictionary.ListDictionaryPageTypesHandler(serverCtx), - }, - { - Method: http.MethodGet, - Path: "/types/:type", - Handler: mpdictionary.GetDictionaryTypeHandler(serverCtx), - }, - { - Method: http.MethodGet, - Path: "/items", - Handler: mpdictionary.ListDictionaryItemsHandler(serverCtx), - }, - { - Method: http.MethodGet, - Path: "/items/:type/:key", - Handler: mpdictionary.GetDictionaryItemHandler(serverCtx), - }, - }..., - ), + []rest.Route{ + { + Method: http.MethodGet, + Path: "/types/page-list", + Handler: mpdictionary.ListDictionaryPageTypesHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/types/:type", + Handler: mpdictionary.GetDictionaryTypeHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/items", + Handler: mpdictionary.ListDictionaryItemsHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/items/:type/:key", + Handler: mpdictionary.GetDictionaryItemHandler(serverCtx), + }, + }, rest.WithPrefix("/api/v1/mp/dictionary"), ) @@ -1574,30 +1573,24 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { ) server.AddRoutes( - rest.WithMiddlewares( - []rest.Middleware{serverCtx.MPCustomerJWTAuth}, - []rest.Route{ - { - Method: http.MethodGet, - Path: "/stores/page-list", - Handler: mpcrmmarketstore.ListStoresPageHandler(serverCtx), - }, - }..., - ), + []rest.Route{ + { + Method: http.MethodGet, + Path: "/stores/page-list", + Handler: mpcrmmarketstore.ListStoresPageHandler(serverCtx), + }, + }, rest.WithPrefix("/api/v1/mp/market"), ) server.AddRoutes( - rest.WithMiddlewares( - []rest.Middleware{serverCtx.MPCustomerJWTAuth}, - []rest.Route{ - { - Method: http.MethodGet, - Path: "/medias/page-list", - Handler: mpcrmmarketmedia.ListMediasPageHandler(serverCtx), - }, - }..., - ), + []rest.Route{ + { + Method: http.MethodGet, + Path: "/medias/page-list", + Handler: mpcrmmarketmedia.ListMediasPageHandler(serverCtx), + }, + }, rest.WithPrefix("/api/v1/mp/market"), ) @@ -1621,40 +1614,34 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { ) server.AddRoutes( - rest.WithMiddlewares( - []rest.Middleware{serverCtx.MPCustomerJWTAuth}, - []rest.Route{ - { - Method: http.MethodGet, - Path: "/products/page-list", - Handler: mpcrmproduct.ListProductsPageHandler(serverCtx), - }, - { - Method: http.MethodGet, - Path: "/products/:id", - Handler: mpcrmproduct.GetProductHandler(serverCtx), - }, - }..., - ), + []rest.Route{ + { + Method: http.MethodGet, + Path: "/products/page-list", + Handler: mpcrmproduct.ListProductsPageHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/products/:id", + Handler: mpcrmproduct.GetProductHandler(serverCtx), + }, + }, rest.WithPrefix("/api/v1/mp/product"), ) server.AddRoutes( - rest.WithMiddlewares( - []rest.Middleware{serverCtx.MPCustomerJWTAuth}, - []rest.Route{ - { - Method: http.MethodGet, - Path: "/product-category-tree", - Handler: mpcrmproduct.ListProductCategoryTreeHandler(serverCtx), - }, - { - Method: http.MethodGet, - Path: "/product-categories", - Handler: mpcrmproduct.ListProductCategoriesHandler(serverCtx), - }, - }..., - ), + []rest.Route{ + { + Method: http.MethodGet, + Path: "/product-category-tree", + Handler: mpcrmproduct.ListProductCategoryTreeHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/product-categories", + Handler: mpcrmproduct.ListProductCategoriesHandler(serverCtx), + }, + }, rest.WithPrefix("/api/v1/mp/product"), ) @@ -1693,21 +1680,18 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { ) server.AddRoutes( - rest.WithMiddlewares( - []rest.Middleware{serverCtx.MPCustomerJWTAuth}, - []rest.Route{ - { - Method: http.MethodGet, - Path: "/product-statistics/page-list", - Handler: mpcrmproductproductstatistics.ListProductStatisticsPageHandler(serverCtx), - }, - { - Method: http.MethodGet, - Path: "/product-statistics/:id", - Handler: mpcrmproductproductstatistics.GetProductStatisticsHandler(serverCtx), - }, - }..., - ), + []rest.Route{ + { + Method: http.MethodGet, + Path: "/product-statistics/page-list", + Handler: mpcrmproductproductstatistics.ListProductStatisticsPageHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/product-statistics/:id", + Handler: mpcrmproductproductstatistics.GetProductStatisticsHandler(serverCtx), + }, + }, rest.WithPrefix("/api/v1/mp/product"), ) @@ -2158,19 +2142,20 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { { Method: http.MethodPost, Path: "/plugin/v1/plugins", - Handler: RegisterPluginHandler(serverCtx), + Handler: plugin.RegisterPluginHandler(serverCtx), }, { Method: http.MethodGet, Path: "/plugin/v1/plugins", - Handler: ListPluginHandler(serverCtx), + Handler: plugin.ListPluginHandler(serverCtx), }, { Method: http.MethodGet, Path: "/plugin/v1/frontend-routes", - Handler: ListPluginFrontendRoutesHandler(serverCtx), + Handler: plugin.ListPluginFrontendRoutesHandler(serverCtx), }, }, + rest.WithPrefix("/api/v1"), ) server.AddRoutes( @@ -2178,8 +2163,9 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { { Method: http.MethodGet, Path: "/health", - Handler: HealthCheckHandler(serverCtx), + Handler: systemhealth.HealthCheckHandler(serverCtx), }, }, + rest.WithPrefix("/api/v1/system"), ) } diff --git a/internal/handler/system/health/healthcheckhandler.go b/internal/handler/system/health/healthcheckhandler.go new file mode 100644 index 00000000..7886563b --- /dev/null +++ b/internal/handler/system/health/healthcheckhandler.go @@ -0,0 +1,28 @@ +package health + +import ( + "net/http" + + "PowerX/internal/logic/system/health" + "PowerX/internal/svc" + "PowerX/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func HealthCheckHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.HealthCheckRequest + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := health.NewHealthCheckLogic(r.Context(), svcCtx) + resp, err := l.HealthCheck(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/internal/logic/plugin/listpluginfrontendrouteslogic.go b/internal/logic/plugin/listpluginfrontendrouteslogic.go new file mode 100644 index 00000000..b416dbd4 --- /dev/null +++ b/internal/logic/plugin/listpluginfrontendrouteslogic.go @@ -0,0 +1,30 @@ +package plugin + +import ( + "context" + + "PowerX/internal/svc" + "PowerX/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ListPluginFrontendRoutesLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewListPluginFrontendRoutesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPluginFrontendRoutesLogic { + return &ListPluginFrontendRoutesLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *ListPluginFrontendRoutesLogic) ListPluginFrontendRoutes() (resp *types.ListPluginFrontendRoutesReply, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/internal/logic/plugin/listpluginlogic.go b/internal/logic/plugin/listpluginlogic.go new file mode 100644 index 00000000..0a624042 --- /dev/null +++ b/internal/logic/plugin/listpluginlogic.go @@ -0,0 +1,30 @@ +package plugin + +import ( + "context" + + "PowerX/internal/svc" + "PowerX/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ListPluginLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewListPluginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPluginLogic { + return &ListPluginLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *ListPluginLogic) ListPlugin(req *types.ListPluginRequest) (resp *types.ListPluginReply, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/internal/logic/plugin/registerpluginlogic.go b/internal/logic/plugin/registerpluginlogic.go new file mode 100644 index 00000000..b5c7ebd4 --- /dev/null +++ b/internal/logic/plugin/registerpluginlogic.go @@ -0,0 +1,30 @@ +package plugin + +import ( + "context" + + "PowerX/internal/svc" + "PowerX/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type RegisterPluginLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewRegisterPluginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterPluginLogic { + return &RegisterPluginLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *RegisterPluginLogic) RegisterPlugin(req *types.RegisterPluginRequest) (resp *types.RegisterPluginReply, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/internal/logic/system/health/healthchecklogic.go b/internal/logic/system/health/healthchecklogic.go new file mode 100644 index 00000000..988352ef --- /dev/null +++ b/internal/logic/system/health/healthchecklogic.go @@ -0,0 +1,30 @@ +package health + +import ( + "context" + + "PowerX/internal/svc" + "PowerX/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type HealthCheckLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewHealthCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HealthCheckLogic { + return &HealthCheckLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *HealthCheckLogic) HealthCheck(req *types.HealthCheckRequest) (resp *types.HealthCheckReply, err error) { + // todo: add your logic here and delete this line + + return +}