This commit is contained in:
2026-08-15 01:58:41 +08:00
parent e55fb57432
commit 1a5969897a
32 changed files with 687 additions and 475 deletions
+38 -14
View File
@@ -2,11 +2,35 @@ package service
import (
"context"
"strings"
"kra/internal/biz"
"kra/internal/conf"
"kra/internal/service/dto"
)
type APIService struct {
uc *biz.AccessUsecase
runtime *conf.Runtime
}
func NewAPIService(uc *biz.AccessUsecase, runtime *conf.Runtime) *APIService {
return &APIService{uc: uc, runtime: runtime}
}
func (s *APIService) NormalizeRoutePath(path string) string {
config := s.runtime.Admin()
if config == nil || config.RouterPrefix == "" {
return path
}
prefix := strings.TrimSuffix(config.RouterPrefix, "/")
normalized := strings.TrimPrefix(path, prefix)
if normalized == "" {
return "/"
}
return normalized
}
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}
}
@@ -23,7 +47,7 @@ func apiResponses(items []*biz.API) []*dto.APIResponse {
return out
}
func (s *AccessService) ListAPI(ctx context.Context, req *dto.APIListRequest) ([]*dto.APIResponse, int64, error) {
func (s *APIService) ListAPI(ctx context.Context, req *dto.APIListRequest) ([]*dto.APIResponse, int64, error) {
items, total, err := s.uc.ListAPIs(ctx, req.Page, req.PageSize, &biz.API{Path: req.Path, Description: req.Description, APIGroup: req.APIGroup, Method: req.Method, OrderKey: req.OrderKey, Desc: req.Desc})
if err != nil {
return nil, 0, err
@@ -31,7 +55,7 @@ func (s *AccessService) ListAPI(ctx context.Context, req *dto.APIListRequest) ([
return apiResponses(items), total, nil
}
func (s *AccessService) AllAPI(ctx context.Context, strict bool) ([]*dto.APIResponse, error) {
func (s *APIService) AllAPI(ctx context.Context, strict bool) ([]*dto.APIResponse, error) {
items, _, err := s.uc.ListAPIs(ctx, 0, 0, &biz.API{StrictAll: strict})
if err != nil {
return nil, err
@@ -39,24 +63,24 @@ func (s *AccessService) AllAPI(ctx context.Context, strict bool) ([]*dto.APIResp
return apiResponses(items), nil
}
func (s *AccessService) CreateAPIRequest(ctx context.Context, req *dto.APIRequest) (*dto.APIResponse, error) {
func (s *APIService) CreateAPIRequest(ctx context.Context, req *dto.APIRequest) (*dto.APIResponse, error) {
value := apiDomain(req)
if err := s.uc.CreateAPI(ctx, value); err != nil {
return nil, err
}
return apiResponse(value), nil
}
func (s *AccessService) UpdateAPIRequest(ctx context.Context, req *dto.APIRequest) error {
func (s *APIService) UpdateAPIRequest(ctx context.Context, req *dto.APIRequest) error {
return s.uc.UpdateAPI(ctx, apiDomain(req))
}
func (s *AccessService) FindAPIResponse(ctx context.Context, id uint) (*dto.APIResponse, error) {
func (s *APIService) FindAPIResponse(ctx context.Context, id uint) (*dto.APIResponse, error) {
value, err := s.uc.FindAPI(ctx, id)
if err != nil {
return nil, err
}
return apiResponse(value), nil
}
func (s *AccessService) ApplyAPISyncRequest(ctx context.Context, req *dto.ApplyAPISyncRequest) error {
func (s *APIService) ApplyAPISyncRequest(ctx context.Context, req *dto.ApplyAPISyncRequest) error {
added := make([]*biz.API, 0, len(req.NewAPIs))
deleted := make([]*biz.API, 0, len(req.DeleteAPIs))
for i := range req.NewAPIs {
@@ -67,14 +91,14 @@ func (s *AccessService) ApplyAPISyncRequest(ctx context.Context, req *dto.ApplyA
}
return s.uc.ApplyAPISync(ctx, added, deleted)
}
func (s *AccessService) SetPolicyPathsRequest(ctx context.Context, req *dto.SetPolicyPathsRequest) error {
func (s *APIService) SetPolicyPathsRequest(ctx context.Context, req *dto.SetPolicyPathsRequest) error {
paths := make([]*biz.API, 0, len(req.Infos))
for _, value := range req.Infos {
paths = append(paths, &biz.API{Path: value.Path, Method: value.Method})
}
return s.uc.SetPolicyPaths(ctx, req.AuthorityID, paths)
}
func (s *AccessService) PolicyPathResponses(ctx context.Context, authorityID uint) ([]dto.PolicyPathRequest, error) {
func (s *APIService) PolicyPathResponses(ctx context.Context, authorityID uint) ([]dto.PolicyPathRequest, error) {
items, err := s.uc.PolicyPaths(ctx, authorityID)
if err != nil {
return nil, err
@@ -85,7 +109,7 @@ func (s *AccessService) PolicyPathResponses(ctx context.Context, authorityID uin
}
return out, nil
}
func (s *AccessService) SyncAPIResponses(ctx context.Context, routes []dto.APIRequest) (*dto.APISyncResponse, error) {
func (s *APIService) SyncAPIResponses(ctx context.Context, routes []dto.APIRequest) (*dto.APISyncResponse, error) {
items := make([]*biz.API, 0, len(routes))
for i := range routes {
routes[i].Path = s.NormalizeRoutePath(routes[i].Path)
@@ -106,22 +130,22 @@ func (s *AccessService) SyncAPIResponses(ctx context.Context, routes []dto.APIRe
func apiDTO(v *biz.API) map[string]any {
return map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "UpdatedAt": v.UpdatedAt, "DeletedAt": nil, "path": v.Path, "description": v.Description, "apiGroup": v.APIGroup, "method": v.Method}
}
func (s *AccessService) DeleteAPIs(ctx context.Context, ids []uint) error {
func (s *APIService) DeleteAPIs(ctx context.Context, ids []uint) error {
return s.uc.DeleteAPIs(ctx, ids)
}
func (s *AccessService) APIRoleIDs(ctx context.Context, path, method string) ([]uint, error) {
func (s *APIService) APIRoleIDs(ctx context.Context, path, method string) ([]uint, error) {
return s.uc.APIRoleIDs(ctx, path, method)
}
func (s *AccessService) SetAPIRoles(ctx context.Context, path, method string, ids []uint) error {
func (s *APIService) SetAPIRoles(ctx context.Context, path, method string, ids []uint) error {
return s.uc.SetAPIRoles(ctx, path, method, ids)
}
func (s *AccessService) SyncAPIs(ctx context.Context, routes []*biz.API) (*dto.APISyncResponse, error) {
func (s *APIService) SyncAPIs(ctx context.Context, routes []*biz.API) (*dto.APISyncResponse, error) {
diff, err := s.uc.SyncAPIs(ctx, routes)
if err != nil {
return nil, err
}
return &dto.APISyncResponse{NewAPIs: apiResponses(diff.Added), DeleteAPIs: apiResponses(diff.Deleted), IgnoreAPIs: apiResponses(diff.Ignored)}, nil
}
func (s *AccessService) SetAPIIgnored(ctx context.Context, path, method string, ignored bool) error {
func (s *APIService) SetAPIIgnored(ctx context.Context, path, method string, ignored bool) error {
return s.uc.SetAPIIgnored(ctx, path, method, ignored)
}