优化
This commit is contained in:
+15
-18
@@ -18,20 +18,14 @@ func NewAPIService(uc *biz.APIUsecase, settings biz.RuntimeSettings) *APIService
|
||||
}
|
||||
|
||||
func (s *APIService) NormalizeRoutePath(path string) string {
|
||||
routerPrefix := s.settings.RouterPrefix()
|
||||
if routerPrefix == "" {
|
||||
if s.settings == nil {
|
||||
return path
|
||||
}
|
||||
prefix := strings.TrimSuffix(routerPrefix, "/")
|
||||
normalized := strings.TrimPrefix(path, prefix)
|
||||
if normalized == "" {
|
||||
return "/"
|
||||
}
|
||||
return normalized
|
||||
return NormalizeRoutePath(path, s.settings.RouterPrefix())
|
||||
}
|
||||
|
||||
func apiDomain(value *dto.APIRequest) *biz.API {
|
||||
return &biz.API{ID: value.ID, Path: value.Path, Description: value.Description, APIGroup: value.APIGroup, Method: value.Method}
|
||||
func (s *APIService) apiDomain(value *dto.APIRequest) *biz.API {
|
||||
return &biz.API{ID: value.ID, Path: s.NormalizeRoutePath(value.Path), Description: value.Description, APIGroup: value.APIGroup, Method: value.Method}
|
||||
}
|
||||
|
||||
func apiResponse(value *biz.API) *dto.APIResponse {
|
||||
@@ -87,14 +81,14 @@ func (s *APIService) Groups(ctx context.Context) ([]string, map[string]string, e
|
||||
}
|
||||
|
||||
func (s *APIService) CreateAPIRequest(ctx context.Context, req *dto.APIRequest) (*dto.APIResponse, error) {
|
||||
value := apiDomain(req)
|
||||
value := s.apiDomain(req)
|
||||
if err := s.uc.CreateAPI(ctx, value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return apiResponse(value), nil
|
||||
}
|
||||
func (s *APIService) UpdateAPIRequest(ctx context.Context, req *dto.APIRequest) error {
|
||||
return s.uc.UpdateAPI(ctx, apiDomain(req))
|
||||
return s.uc.UpdateAPI(ctx, s.apiDomain(req))
|
||||
}
|
||||
func (s *APIService) FindAPIResponse(ctx context.Context, id uint) (*dto.APIResponse, error) {
|
||||
value, err := s.uc.FindAPI(ctx, id)
|
||||
@@ -107,10 +101,10 @@ func (s *APIService) ApplyAPISyncRequest(ctx context.Context, req *dto.ApplyAPIS
|
||||
added := make([]*biz.API, 0, len(req.NewAPIs))
|
||||
deleted := make([]*biz.API, 0, len(req.DeleteAPIs))
|
||||
for i := range req.NewAPIs {
|
||||
added = append(added, apiDomain(&req.NewAPIs[i]))
|
||||
added = append(added, s.apiDomain(&req.NewAPIs[i]))
|
||||
}
|
||||
for i := range req.DeleteAPIs {
|
||||
deleted = append(deleted, apiDomain(&req.DeleteAPIs[i]))
|
||||
deleted = append(deleted, s.apiDomain(&req.DeleteAPIs[i]))
|
||||
}
|
||||
return s.uc.ApplyAPISync(ctx, added, deleted)
|
||||
}
|
||||
@@ -142,7 +136,7 @@ func (s *APIService) SyncAPIResponses(ctx context.Context, routes []dto.APIReque
|
||||
// The compatible sync endpoint compares Gin's route table directly, so newly
|
||||
// discovered routes carry only path and method. Group/description are
|
||||
// intentionally left empty for the operator to fill in the sync dialog.
|
||||
items = append(items, apiDomain(&routes[i]))
|
||||
items = append(items, s.apiDomain(&routes[i]))
|
||||
}
|
||||
return s.SyncAPIs(ctx, items)
|
||||
}
|
||||
@@ -154,10 +148,13 @@ func (s *APIService) DeleteAPI(ctx context.Context, id uint) error {
|
||||
return s.uc.DeleteAPI(ctx, id)
|
||||
}
|
||||
func (s *APIService) APIRoleIDs(ctx context.Context, path, method string) ([]uint, error) {
|
||||
return s.uc.APIRoleIDs(ctx, path, method)
|
||||
return s.uc.APIRoleIDs(ctx, s.NormalizeRoutePath(path), method)
|
||||
}
|
||||
func (s *APIService) SetAPIRoles(ctx context.Context, path, method string, ids []uint) error {
|
||||
return s.uc.SetAPIRoles(ctx, path, method, ids)
|
||||
return s.uc.SetAPIRoles(ctx, s.NormalizeRoutePath(path), method, ids)
|
||||
}
|
||||
func (s *APIService) FreshCasbin(ctx context.Context) error {
|
||||
return s.uc.FreshCasbin(ctx)
|
||||
}
|
||||
func (s *APIService) SyncAPIs(ctx context.Context, routes []*biz.API) (*dto.APISyncResponse, error) {
|
||||
diff, err := s.uc.SyncAPIs(ctx, routes)
|
||||
@@ -167,5 +164,5 @@ func (s *APIService) SyncAPIs(ctx context.Context, routes []*biz.API) (*dto.APIS
|
||||
return &dto.APISyncResponse{NewAPIs: apiResponses(diff.Added), DeleteAPIs: apiResponses(diff.Deleted), IgnoreAPIs: apiResponses(diff.Ignored)}, nil
|
||||
}
|
||||
func (s *APIService) SetAPIIgnored(ctx context.Context, path, method string, ignored bool) error {
|
||||
return s.uc.SetAPIIgnored(ctx, path, method, ignored)
|
||||
return s.uc.SetAPIIgnored(ctx, s.NormalizeRoutePath(path), method, ignored)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user