Skip to content

Commit a1bdc04

Browse files
authored
Merge pull request #149 from APIParkLab/feature/aibug-fix
add default router when create new rest service
2 parents d36c663 + 0a1b081 commit a1bdc04

File tree

6 files changed

+99
-14
lines changed

6 files changed

+99
-14
lines changed

controller/service/iml.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,33 @@ func (i *imlServiceController) Create(ctx *gin.Context, teamID string, input *se
326326
if input.Kind == "ai" {
327327
return i.createAIService(ctx, teamID, input)
328328
}
329-
return i.module.Create(ctx, teamID, input)
329+
var err error
330+
var info *service_dto.Service
331+
err = i.transaction.Transaction(ctx, func(txCtx context.Context) error {
332+
info, err = i.module.Create(txCtx, teamID, input)
333+
if err != nil {
334+
return err
335+
}
336+
path := fmt.Sprintf("/%s/", strings.Trim(input.Prefix, "/"))
337+
_, err = i.routerModule.Create(txCtx, info.Id, &router_dto.Create{
338+
Id: uuid.New().String(),
339+
Name: "",
340+
Path: path + "*",
341+
Methods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch, http.MethodOptions},
342+
Description: "auto create by create service",
343+
Protocols: []string{"http", "https"},
344+
MatchRules: nil,
345+
Upstream: "",
346+
Proxy: &router_dto.InputProxy{
347+
Path: path,
348+
Timeout: 30000,
349+
Retry: 0,
350+
},
351+
Disable: false,
352+
})
353+
return err
354+
})
355+
return info, err
330356
}
331357

332358
func (i *imlServiceController) Edit(ctx *gin.Context, id string, input *service_dto.EditService) (*service_dto.Service, error) {

controller/system/iml.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ func (i *imlInitController) OnInit() {
265265
return fmt.Errorf("create default team error: %v", err)
266266
}
267267
// 创建Rest服务
268-
_, err = i.serviceModule.Create(ctx, info.Id, &service_dto.CreateService{
268+
restPath := "/rest-demo"
269+
serviceInfo, err := i.serviceModule.Create(ctx, info.Id, &service_dto.CreateService{
269270
Name: "REST Demo Service",
270271
Prefix: "/rest-demo",
271272
Description: "Auto created By APIPark",
@@ -277,6 +278,26 @@ func (i *imlInitController) OnInit() {
277278
if err != nil {
278279
return fmt.Errorf("create default service error: %v", err)
279280
}
281+
path := fmt.Sprintf("/%s/", strings.Trim(restPath, "/"))
282+
_, err = i.routerModule.Create(ctx, serviceInfo.Id, &router_dto.Create{
283+
Id: uuid.NewString(),
284+
Name: "",
285+
Path: path + "*",
286+
Methods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch, http.MethodOptions},
287+
Description: "auto create by create service",
288+
Protocols: []string{"http", "https"},
289+
MatchRules: nil,
290+
Upstream: "",
291+
Proxy: &router_dto.InputProxy{
292+
Path: path,
293+
Timeout: 30000,
294+
Retry: 0,
295+
},
296+
Disable: false,
297+
})
298+
if err != nil {
299+
return fmt.Errorf("create default router error: %v", err)
300+
}
280301
// 创建AI服务
281302
err = i.createAIService(ctx, info.Id, &service_dto.CreateService{
282303
Name: "AI Demo Service",

module/api-doc/api_doc.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api_doc
33
import (
44
"context"
55
"errors"
6+
67
api_doc_dto "github.com/APIParkLab/APIPark/module/api-doc/dto"
78
api_doc "github.com/APIParkLab/APIPark/service/api-doc"
89
"github.com/APIParkLab/APIPark/service/service"
@@ -20,16 +21,19 @@ type imlAPIDocModule struct {
2021
}
2122

2223
func (i *imlAPIDocModule) UpdateDoc(ctx context.Context, serviceId string, input *api_doc_dto.UpdateDoc) (*api_doc_dto.ApiDocDetail, error) {
23-
_, err := i.serviceService.Get(ctx, serviceId)
24+
info, err := i.serviceService.Get(ctx, serviceId)
2425
if err != nil {
2526
return nil, err
2627
}
2728
if input.Id == "" {
2829
input.Id = uuid.New().String()
2930
}
31+
// 每个API加上前缀
32+
3033
err = i.apiDocService.UpdateDoc(ctx, serviceId, &api_doc.UpdateDoc{
3134
ID: input.Id,
3235
Content: input.Content,
36+
Prefix: info.Prefix,
3337
})
3438
if err != nil {
3539
return nil, err

service/api-doc/iml.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ func (i *imlAPIDocService) UpdateDoc(ctx context.Context, serviceId string, inpu
7676
if err := doc.Valid(); err != nil {
7777
return err
7878
}
79+
if input.Prefix != "" {
80+
err = doc.AddPrefixInAll(input.Prefix)
81+
if err != nil {
82+
return err
83+
}
84+
}
85+
data, err := doc.Marshal()
86+
if err != nil {
87+
return err
88+
}
89+
input.Content = string(data)
7990

8091
info, err := i.store.First(ctx, map[string]interface{}{
8192
"service": serviceId,
@@ -94,9 +105,9 @@ func (i *imlAPIDocService) UpdateDoc(ctx context.Context, serviceId string, inpu
94105
APICount: doc.APICount(),
95106
})
96107
}
108+
info.Content = input.Content
97109
info.Updater = operator
98110
info.UpdateAt = time.Now()
99-
info.Content = input.Content
100111
info.APICount = doc.APICount()
101112
return i.store.Save(ctx, info)
102113
}

service/api-doc/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "time"
55
type UpdateDoc struct {
66
ID string
77
Content string
8+
Prefix string
89
}
910

1011
type Doc struct {

service/api-doc/service.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package api_doc
33
import (
44
"context"
55
"fmt"
6+
"reflect"
7+
"strings"
8+
9+
yaml "gopkg.in/yaml.v3"
10+
611
"github.com/APIParkLab/APIPark/service/universally/commit"
712
"github.com/eolinker/go-common/autowire"
813
"github.com/getkin/kin-openapi/openapi3"
9-
"reflect"
1014
)
1115

1216
type IAPIDocService interface {
@@ -65,15 +69,6 @@ func (d *DocLoader) Valid() error {
6569
if d.openAPI3Doc.Paths == nil {
6670
return fmt.Errorf("openAPI3Doc.Paths is nil")
6771
}
68-
//err := d.openAPI3Doc.Validate(openapi3Loader.Context)
69-
//if err != nil {
70-
// openAPI2Doc, err := openapi2conv.FromV3(d.openAPI3Doc)
71-
// if err != nil {
72-
// return err
73-
// }
74-
// validate.
75-
//
76-
//}
7772
return nil
7873
}
7974

@@ -110,3 +105,30 @@ func (d *DocLoader) APICount() int64 {
110105
}
111106
return count
112107
}
108+
109+
func (d *DocLoader) AddPrefixInAll(prefix string) error {
110+
prefix = strings.TrimSpace(prefix)
111+
if prefix == "" || d.openAPI3Doc == nil || d.openAPI3Doc.Paths == nil {
112+
return nil
113+
}
114+
115+
prefix = fmt.Sprintf("/%s/", strings.Trim(prefix, "/"))
116+
for path, item := range d.openAPI3Doc.Paths.Map() {
117+
path = strings.TrimSpace(path)
118+
if strings.HasPrefix(path, prefix) {
119+
continue
120+
}
121+
newPath := fmt.Sprintf("%s%s", prefix, strings.TrimPrefix(path, "/"))
122+
d.openAPI3Doc.Paths.Delete(path)
123+
d.openAPI3Doc.Paths.Set(newPath, item)
124+
}
125+
return nil
126+
}
127+
128+
func (d *DocLoader) Marshal() ([]byte, error) {
129+
result, err := d.openAPI3Doc.MarshalYAML()
130+
if err != nil {
131+
return nil, err
132+
}
133+
return yaml.Marshal(result)
134+
}

0 commit comments

Comments
 (0)