From b7307cd36dd1f16ce04e3cc819de1829ce9c728d Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Wed, 27 Nov 2024 13:51:55 +0800 Subject: [PATCH] add open api swagger --- controller/service/iml.go | 61 +++++++++++++++++++++++++++++++++++ controller/service/service.go | 2 ++ controller/strategy/iml.go | 2 +- plugins/core/service.go | 1 + 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/controller/service/iml.go b/controller/service/iml.go index 1a5e024f..c8e58239 100644 --- a/controller/service/iml.go +++ b/controller/service/iml.go @@ -7,6 +7,14 @@ import ( "strings" "time" + "github.com/eolinker/go-common/pm3" + + "github.com/APIParkLab/APIPark/module/system" + + "github.com/getkin/kin-openapi/openapi3" + + api_doc "github.com/APIParkLab/APIPark/module/api-doc" + upstream_dto "github.com/APIParkLab/APIPark/module/upstream/dto" "github.com/eolinker/eosc/log" @@ -43,11 +51,64 @@ type imlServiceController struct { docModule service.IServiceDocModule `autowired:""` aiAPIModule ai_api.IAPIModule `autowired:""` routerModule router.IRouterModule `autowired:""` + apiDocModule api_doc.IAPIDocModule `autowired:""` providerModule ai.IProviderModule `autowired:""` upstreamModule upstream.IUpstreamModule `autowired:""` + settingModule system.ISettingModule `autowired:""` transaction store.ITransaction `autowired:""` } +var ( + loader = openapi3.NewLoader() +) + +func (i *imlServiceController) Swagger(ctx *gin.Context) { + id, has := ctx.Params.Get("id") + if !has { + ctx.JSON(200, &pm3.Response{ + Code: -1, + Success: "fail", + Message: fmt.Sprintf("id is required"), + }) + return + + } + doc, err := i.apiDocModule.GetDoc(ctx, id) + if err != nil { + ctx.JSON(200, &pm3.Response{ + Code: -1, + Success: "fail", + Message: err.Error(), + }) + return + } + tmp, err := loader.LoadFromData([]byte(doc.Content)) + if err != nil { + ctx.JSON(200, &pm3.Response{ + Code: -1, + Success: "fail", + Message: err.Error(), + }) + return + } + cfg := i.settingModule.Get(ctx) + + tmp.AddServer(&openapi3.Server{ + URL: cfg.InvokeAddress, + }) + data, err := tmp.MarshalJSON() + if err != nil { + ctx.JSON(200, &pm3.Response{ + Code: -1, + Success: "fail", + Message: err.Error(), + }) + return + } + ctx.JSON(200, string(data)) + return +} + func (i *imlServiceController) Simple(ctx *gin.Context) ([]*service_dto.SimpleServiceItem, error) { return i.module.Simple(ctx) } diff --git a/controller/service/service.go b/controller/service/service.go index 6f631781..ddf657f5 100644 --- a/controller/service/service.go +++ b/controller/service/service.go @@ -26,6 +26,8 @@ type IServiceController interface { SaveServiceDoc(ctx *gin.Context, id string, input *service_dto.SaveServiceDoc) error Simple(ctx *gin.Context) ([]*service_dto.SimpleServiceItem, error) MySimple(ctx *gin.Context) ([]*service_dto.SimpleServiceItem, error) + + Swagger(ctx *gin.Context) } type IAppController interface { diff --git a/controller/strategy/iml.go b/controller/strategy/iml.go index 7f0b3e22..6c35e2da 100644 --- a/controller/strategy/iml.go +++ b/controller/strategy/iml.go @@ -197,7 +197,7 @@ func (i *imlStrategyController) CreateServiceStrategy(ctx *gin.Context, serviceI } func (i *imlStrategyController) EditStrategy(ctx *gin.Context, id string, input *strategy_dto.Edit) error { - return i.EditStrategy(ctx, id, input) + return i.strategyModule.Edit(ctx, id, input) } func (i *imlStrategyController) EnableStrategy(ctx *gin.Context, id string) error { diff --git a/plugins/core/service.go b/plugins/core/service.go index d2ff224a..11a8ee5d 100644 --- a/plugins/core/service.go +++ b/plugins/core/service.go @@ -29,5 +29,6 @@ func (p *plugin) ServiceApis() []pm3.Api { pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/service/doc", []string{"context", "query:service"}, []string{"doc"}, p.serviceController.ServiceDoc, access.SystemWorkspaceServiceViewAll, access.TeamServiceServiceIntroView), pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/service/doc", []string{"context", "query:service", "body"}, nil, p.serviceController.SaveServiceDoc, access.SystemWorkspaceServiceManagerAll, access.TeamServiceServiceIntroManager), + pm3.CreateApiSimple(http.MethodGet, "/swagger/:id", p.serviceController.Swagger), } }