diff --git a/controller/service/iml.go b/controller/service/iml.go index e95a8238..7227e6b7 100644 --- a/controller/service/iml.go +++ b/controller/service/iml.go @@ -326,7 +326,33 @@ func (i *imlServiceController) Create(ctx *gin.Context, teamID string, input *se if input.Kind == "ai" { return i.createAIService(ctx, teamID, input) } - return i.module.Create(ctx, teamID, input) + var err error + var info *service_dto.Service + err = i.transaction.Transaction(ctx, func(txCtx context.Context) error { + info, err = i.module.Create(txCtx, teamID, input) + if err != nil { + return err + } + path := fmt.Sprintf("/%s/", strings.Trim(input.Prefix, "/")) + _, err = i.routerModule.Create(txCtx, info.Id, &router_dto.Create{ + Id: uuid.New().String(), + Name: "", + Path: path + "*", + Methods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch, http.MethodOptions}, + Description: "auto create by create service", + Protocols: []string{"http", "https"}, + MatchRules: nil, + Upstream: "", + Proxy: &router_dto.InputProxy{ + Path: path, + Timeout: 30000, + Retry: 0, + }, + Disable: false, + }) + return err + }) + return info, err } func (i *imlServiceController) Edit(ctx *gin.Context, id string, input *service_dto.EditService) (*service_dto.Service, error) { diff --git a/controller/system/iml.go b/controller/system/iml.go index 1c1269d5..1b9c0434 100644 --- a/controller/system/iml.go +++ b/controller/system/iml.go @@ -265,7 +265,8 @@ func (i *imlInitController) OnInit() { return fmt.Errorf("create default team error: %v", err) } // 创建Rest服务 - _, err = i.serviceModule.Create(ctx, info.Id, &service_dto.CreateService{ + restPath := "/rest-demo" + serviceInfo, err := i.serviceModule.Create(ctx, info.Id, &service_dto.CreateService{ Name: "REST Demo Service", Prefix: "/rest-demo", Description: "Auto created By APIPark", @@ -277,6 +278,26 @@ func (i *imlInitController) OnInit() { if err != nil { return fmt.Errorf("create default service error: %v", err) } + path := fmt.Sprintf("/%s/", strings.Trim(restPath, "/")) + _, err = i.routerModule.Create(ctx, serviceInfo.Id, &router_dto.Create{ + Id: uuid.NewString(), + Name: "", + Path: path + "*", + Methods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch, http.MethodOptions}, + Description: "auto create by create service", + Protocols: []string{"http", "https"}, + MatchRules: nil, + Upstream: "", + Proxy: &router_dto.InputProxy{ + Path: path, + Timeout: 30000, + Retry: 0, + }, + Disable: false, + }) + if err != nil { + return fmt.Errorf("create default router error: %v", err) + } // 创建AI服务 err = i.createAIService(ctx, info.Id, &service_dto.CreateService{ Name: "AI Demo Service", diff --git a/module/api-doc/api_doc.go b/module/api-doc/api_doc.go index 022dd64f..d83e74cd 100644 --- a/module/api-doc/api_doc.go +++ b/module/api-doc/api_doc.go @@ -3,6 +3,7 @@ package api_doc import ( "context" "errors" + api_doc_dto "github.com/APIParkLab/APIPark/module/api-doc/dto" api_doc "github.com/APIParkLab/APIPark/service/api-doc" "github.com/APIParkLab/APIPark/service/service" @@ -20,16 +21,19 @@ type imlAPIDocModule struct { } func (i *imlAPIDocModule) UpdateDoc(ctx context.Context, serviceId string, input *api_doc_dto.UpdateDoc) (*api_doc_dto.ApiDocDetail, error) { - _, err := i.serviceService.Get(ctx, serviceId) + info, err := i.serviceService.Get(ctx, serviceId) if err != nil { return nil, err } if input.Id == "" { input.Id = uuid.New().String() } + // 每个API加上前缀 + err = i.apiDocService.UpdateDoc(ctx, serviceId, &api_doc.UpdateDoc{ ID: input.Id, Content: input.Content, + Prefix: info.Prefix, }) if err != nil { return nil, err diff --git a/service/api-doc/iml.go b/service/api-doc/iml.go index 7828ed9a..b385b90c 100644 --- a/service/api-doc/iml.go +++ b/service/api-doc/iml.go @@ -76,6 +76,17 @@ func (i *imlAPIDocService) UpdateDoc(ctx context.Context, serviceId string, inpu if err := doc.Valid(); err != nil { return err } + if input.Prefix != "" { + err = doc.AddPrefixInAll(input.Prefix) + if err != nil { + return err + } + } + data, err := doc.Marshal() + if err != nil { + return err + } + input.Content = string(data) info, err := i.store.First(ctx, map[string]interface{}{ "service": serviceId, @@ -94,9 +105,9 @@ func (i *imlAPIDocService) UpdateDoc(ctx context.Context, serviceId string, inpu APICount: doc.APICount(), }) } + info.Content = input.Content info.Updater = operator info.UpdateAt = time.Now() - info.Content = input.Content info.APICount = doc.APICount() return i.store.Save(ctx, info) } diff --git a/service/api-doc/model.go b/service/api-doc/model.go index b9791f24..204bc3f3 100644 --- a/service/api-doc/model.go +++ b/service/api-doc/model.go @@ -5,6 +5,7 @@ import "time" type UpdateDoc struct { ID string Content string + Prefix string } type Doc struct { diff --git a/service/api-doc/service.go b/service/api-doc/service.go index 819cff14..abb8d4fb 100644 --- a/service/api-doc/service.go +++ b/service/api-doc/service.go @@ -3,10 +3,14 @@ package api_doc import ( "context" "fmt" + "reflect" + "strings" + + yaml "gopkg.in/yaml.v3" + "github.com/APIParkLab/APIPark/service/universally/commit" "github.com/eolinker/go-common/autowire" "github.com/getkin/kin-openapi/openapi3" - "reflect" ) type IAPIDocService interface { @@ -65,15 +69,6 @@ func (d *DocLoader) Valid() error { if d.openAPI3Doc.Paths == nil { return fmt.Errorf("openAPI3Doc.Paths is nil") } - //err := d.openAPI3Doc.Validate(openapi3Loader.Context) - //if err != nil { - // openAPI2Doc, err := openapi2conv.FromV3(d.openAPI3Doc) - // if err != nil { - // return err - // } - // validate. - // - //} return nil } @@ -110,3 +105,30 @@ func (d *DocLoader) APICount() int64 { } return count } + +func (d *DocLoader) AddPrefixInAll(prefix string) error { + prefix = strings.TrimSpace(prefix) + if prefix == "" || d.openAPI3Doc == nil || d.openAPI3Doc.Paths == nil { + return nil + } + + prefix = fmt.Sprintf("/%s/", strings.Trim(prefix, "/")) + for path, item := range d.openAPI3Doc.Paths.Map() { + path = strings.TrimSpace(path) + if strings.HasPrefix(path, prefix) { + continue + } + newPath := fmt.Sprintf("%s%s", prefix, strings.TrimPrefix(path, "/")) + d.openAPI3Doc.Paths.Delete(path) + d.openAPI3Doc.Paths.Set(newPath, item) + } + return nil +} + +func (d *DocLoader) Marshal() ([]byte, error) { + result, err := d.openAPI3Doc.MarshalYAML() + if err != nil { + return nil, err + } + return yaml.Marshal(result) +}