399 lines
11 KiB
Go
399 lines
11 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
|
|
"kra/internal/biz/system"
|
|
"kra/internal/server/middleware"
|
|
|
|
"github.com/go-kratos/kratos/v2/errors"
|
|
"github.com/go-kratos/kratos/v2/transport/http"
|
|
)
|
|
|
|
// ApiService API服务
|
|
type ApiService struct {
|
|
uc *system.ApiUsecase
|
|
}
|
|
|
|
// NewApiService 创建API服务
|
|
func NewApiService(uc *system.ApiUsecase) *ApiService {
|
|
return &ApiService{uc: uc}
|
|
}
|
|
|
|
// ApiInfo API信息
|
|
type ApiInfo struct {
|
|
ID uint `json:"ID"`
|
|
Path string `json:"path"`
|
|
Description string `json:"description"`
|
|
ApiGroup string `json:"apiGroup"`
|
|
Method string `json:"method"`
|
|
}
|
|
|
|
// CreateApiRequest 创建API请求
|
|
type CreateApiRequest struct {
|
|
Path string `json:"path"`
|
|
Description string `json:"description"`
|
|
ApiGroup string `json:"apiGroup"`
|
|
Method string `json:"method"`
|
|
}
|
|
|
|
// CreateApi 创建API
|
|
func (s *ApiService) CreateApi(ctx context.Context, req *CreateApiRequest) error {
|
|
api := &system.Api{
|
|
Path: req.Path,
|
|
Description: req.Description,
|
|
ApiGroup: req.ApiGroup,
|
|
Method: req.Method,
|
|
}
|
|
return s.uc.CreateApi(ctx, api)
|
|
}
|
|
|
|
// UpdateApiRequest 更新API请求
|
|
type UpdateApiRequest struct {
|
|
ID uint `json:"ID"`
|
|
Path string `json:"path"`
|
|
Description string `json:"description"`
|
|
ApiGroup string `json:"apiGroup"`
|
|
Method string `json:"method"`
|
|
}
|
|
|
|
// UpdateApi 更新API
|
|
func (s *ApiService) UpdateApi(ctx context.Context, req *UpdateApiRequest) error {
|
|
api := &system.Api{
|
|
ID: req.ID,
|
|
Path: req.Path,
|
|
Description: req.Description,
|
|
ApiGroup: req.ApiGroup,
|
|
Method: req.Method,
|
|
}
|
|
return s.uc.UpdateApi(ctx, api)
|
|
}
|
|
|
|
// DeleteApi 删除API
|
|
func (s *ApiService) DeleteApi(ctx context.Context, id uint) error {
|
|
return s.uc.DeleteApi(ctx, id)
|
|
}
|
|
|
|
// DeleteApisByIdsRequest 批量删除API请求
|
|
type DeleteApisByIdsRequest struct {
|
|
Ids []uint `json:"ids"`
|
|
}
|
|
|
|
// DeleteApisByIds 批量删除API
|
|
func (s *ApiService) DeleteApisByIds(ctx context.Context, req *DeleteApisByIdsRequest) error {
|
|
return s.uc.DeleteApisByIds(ctx, req.Ids)
|
|
}
|
|
|
|
// GetApiById 根据ID获取API
|
|
func (s *ApiService) GetApiById(ctx context.Context, id uint) (*ApiInfo, error) {
|
|
api, err := s.uc.GetApiById(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return toApiInfo(api), nil
|
|
}
|
|
|
|
// GetApiListRequest 获取API列表请求
|
|
type GetApiListRequest struct {
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
Path string `json:"path"`
|
|
Description string `json:"description"`
|
|
Method string `json:"method"`
|
|
ApiGroup string `json:"apiGroup"`
|
|
OrderKey string `json:"orderKey"`
|
|
Desc bool `json:"desc"`
|
|
}
|
|
|
|
// GetApiListResponse 获取API列表响应
|
|
type GetApiListResponse struct {
|
|
List []*ApiInfo `json:"list"`
|
|
Total int64 `json:"total"`
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
}
|
|
|
|
// GetApiList 获取API列表
|
|
func (s *ApiService) GetApiList(ctx context.Context, req *GetApiListRequest) (*GetApiListResponse, error) {
|
|
filters := make(map[string]string)
|
|
if req.Path != "" {
|
|
filters["path"] = req.Path
|
|
}
|
|
if req.Description != "" {
|
|
filters["description"] = req.Description
|
|
}
|
|
if req.Method != "" {
|
|
filters["method"] = req.Method
|
|
}
|
|
if req.ApiGroup != "" {
|
|
filters["api_group"] = req.ApiGroup
|
|
}
|
|
|
|
apis, total, err := s.uc.GetAPIInfoList(ctx, req.Page, req.PageSize, filters, req.OrderKey, req.Desc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
list := make([]*ApiInfo, len(apis))
|
|
for i, api := range apis {
|
|
list[i] = toApiInfo(api)
|
|
}
|
|
|
|
return &GetApiListResponse{
|
|
List: list,
|
|
Total: total,
|
|
Page: req.Page,
|
|
PageSize: req.PageSize,
|
|
}, nil
|
|
}
|
|
|
|
// GetAllApis 获取所有API
|
|
func (s *ApiService) GetAllApis(ctx context.Context, authorityID uint) ([]*ApiInfo, error) {
|
|
apis, err := s.uc.GetAllApis(ctx, authorityID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list := make([]*ApiInfo, len(apis))
|
|
for i, api := range apis {
|
|
list[i] = toApiInfo(api)
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
// GetApiGroups 获取API分组
|
|
func (s *ApiService) GetApiGroups(ctx context.Context) ([]string, map[string]string, error) {
|
|
return s.uc.GetApiGroups(ctx)
|
|
}
|
|
|
|
// SyncApiResponse 同步API响应
|
|
type SyncApiResponse struct {
|
|
NewApis []*ApiInfo `json:"newApis"`
|
|
DeleteApis []*ApiInfo `json:"deleteApis"`
|
|
IgnoreApis []*ApiInfo `json:"ignoreApis"`
|
|
}
|
|
|
|
// SyncApi 同步API
|
|
func (s *ApiService) SyncApi(ctx context.Context) (*SyncApiResponse, error) {
|
|
newApis, deleteApis, ignoreApis, err := s.uc.SyncApi(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp := &SyncApiResponse{
|
|
NewApis: make([]*ApiInfo, len(newApis)),
|
|
DeleteApis: make([]*ApiInfo, len(deleteApis)),
|
|
IgnoreApis: make([]*ApiInfo, len(ignoreApis)),
|
|
}
|
|
for i, api := range newApis {
|
|
resp.NewApis[i] = toApiInfo(api)
|
|
}
|
|
for i, api := range deleteApis {
|
|
resp.DeleteApis[i] = toApiInfo(api)
|
|
}
|
|
for i, api := range ignoreApis {
|
|
resp.IgnoreApis[i] = toApiInfo(api)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
// IgnoreApiRequest 忽略API请求
|
|
type IgnoreApiRequest struct {
|
|
Path string `json:"path"`
|
|
Method string `json:"method"`
|
|
Flag bool `json:"flag"`
|
|
}
|
|
|
|
// IgnoreApi 忽略API
|
|
func (s *ApiService) IgnoreApi(ctx context.Context, req *IgnoreApiRequest) error {
|
|
return s.uc.IgnoreApi(ctx, req.Path, req.Method, req.Flag)
|
|
}
|
|
|
|
// EnterSyncApiRequest 确认同步API请求
|
|
type EnterSyncApiRequest struct {
|
|
NewApis []*ApiInfo `json:"newApis"`
|
|
DeleteApis []*ApiInfo `json:"deleteApis"`
|
|
}
|
|
|
|
// EnterSyncApi 确认同步API
|
|
func (s *ApiService) EnterSyncApi(ctx context.Context, req *EnterSyncApiRequest) error {
|
|
newApis := make([]*system.Api, len(req.NewApis))
|
|
for i, api := range req.NewApis {
|
|
newApis[i] = &system.Api{
|
|
Path: api.Path,
|
|
Description: api.Description,
|
|
ApiGroup: api.ApiGroup,
|
|
Method: api.Method,
|
|
}
|
|
}
|
|
deleteApis := make([]*system.Api, len(req.DeleteApis))
|
|
for i, api := range req.DeleteApis {
|
|
deleteApis[i] = &system.Api{
|
|
ID: api.ID,
|
|
Path: api.Path,
|
|
Method: api.Method,
|
|
}
|
|
}
|
|
return s.uc.EnterSyncApi(ctx, newApis, deleteApis)
|
|
}
|
|
|
|
// FreshCasbin 刷新Casbin缓存
|
|
func (s *ApiService) FreshCasbin() error {
|
|
return s.uc.FreshCasbin()
|
|
}
|
|
|
|
// 转换函数
|
|
func toApiInfo(api *system.Api) *ApiInfo {
|
|
return &ApiInfo{
|
|
ID: api.ID,
|
|
Path: api.Path,
|
|
Description: api.Description,
|
|
ApiGroup: api.ApiGroup,
|
|
Method: api.Method,
|
|
}
|
|
}
|
|
|
|
// RegisterRoutes 注册路由
|
|
func (s *ApiService) RegisterRoutes(srv *http.Server) {
|
|
r := srv.Route("/")
|
|
|
|
r.POST("/api/createApi", s.handleCreateApi)
|
|
r.POST("/api/updateApi", s.handleUpdateApi)
|
|
r.POST("/api/deleteApi", s.handleDeleteApi)
|
|
r.DELETE("/api/deleteApisByIds", s.handleDeleteApisByIds)
|
|
r.POST("/api/getApiList", s.handleGetApiList)
|
|
r.POST("/api/getApiById", s.handleGetApiById)
|
|
r.POST("/api/getAllApis", s.handleGetAllApis)
|
|
r.GET("/api/getApiGroups", s.handleGetApiGroups)
|
|
r.GET("/api/syncApi", s.handleSyncApi)
|
|
r.POST("/api/ignoreApi", s.handleIgnoreApi)
|
|
r.POST("/api/enterSyncApi", s.handleEnterSyncApi)
|
|
r.GET("/api/freshCasbin", s.handleFreshCasbin)
|
|
}
|
|
|
|
// HTTP Handlers
|
|
func (s *ApiService) handleCreateApi(ctx http.Context) error {
|
|
var req CreateApiRequest
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
if err := s.CreateApi(ctx, &req); err != nil {
|
|
return errors.BadRequest("CREATE_FAILED", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "创建成功"})
|
|
}
|
|
|
|
func (s *ApiService) handleUpdateApi(ctx http.Context) error {
|
|
var req UpdateApiRequest
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
if err := s.UpdateApi(ctx, &req); err != nil {
|
|
return errors.BadRequest("UPDATE_FAILED", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "修改成功"})
|
|
}
|
|
|
|
func (s *ApiService) handleDeleteApi(ctx http.Context) error {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
}
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
if err := s.DeleteApi(ctx, req.ID); err != nil {
|
|
return errors.BadRequest("DELETE_FAILED", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "删除成功"})
|
|
}
|
|
|
|
func (s *ApiService) handleDeleteApisByIds(ctx http.Context) error {
|
|
var req DeleteApisByIdsRequest
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
if err := s.DeleteApisByIds(ctx, &req); err != nil {
|
|
return errors.BadRequest("DELETE_FAILED", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "删除成功"})
|
|
}
|
|
|
|
func (s *ApiService) handleGetApiList(ctx http.Context) error {
|
|
var req GetApiListRequest
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
resp, err := s.GetApiList(ctx, &req)
|
|
if err != nil {
|
|
return errors.InternalServer("LIST_ERROR", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": resp})
|
|
}
|
|
|
|
func (s *ApiService) handleGetApiById(ctx http.Context) error {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
}
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
resp, err := s.GetApiById(ctx, req.ID)
|
|
if err != nil {
|
|
return errors.NotFound("NOT_FOUND", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"api": resp}})
|
|
}
|
|
|
|
func (s *ApiService) handleGetAllApis(ctx http.Context) error {
|
|
authorityID := middleware.GetAuthorityID(ctx)
|
|
resp, err := s.GetAllApis(ctx, authorityID)
|
|
if err != nil {
|
|
return errors.InternalServer("LIST_ERROR", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"apis": resp}})
|
|
}
|
|
|
|
func (s *ApiService) handleGetApiGroups(ctx http.Context) error {
|
|
groups, apiGroupMap, err := s.GetApiGroups(ctx)
|
|
if err != nil {
|
|
return errors.InternalServer("LIST_ERROR", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "获取成功", "data": map[string]any{"groups": groups, "apiGroupMap": apiGroupMap}})
|
|
}
|
|
|
|
func (s *ApiService) handleSyncApi(ctx http.Context) error {
|
|
resp, err := s.SyncApi(ctx)
|
|
if err != nil {
|
|
return errors.InternalServer("SYNC_ERROR", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "data": resp})
|
|
}
|
|
|
|
func (s *ApiService) handleIgnoreApi(ctx http.Context) error {
|
|
var req IgnoreApiRequest
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
if err := s.IgnoreApi(ctx, &req); err != nil {
|
|
return errors.BadRequest("IGNORE_FAILED", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "操作成功"})
|
|
}
|
|
|
|
func (s *ApiService) handleEnterSyncApi(ctx http.Context) error {
|
|
var req EnterSyncApiRequest
|
|
if err := ctx.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
if err := s.EnterSyncApi(ctx, &req); err != nil {
|
|
return errors.BadRequest("SYNC_FAILED", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "同步成功"})
|
|
}
|
|
|
|
func (s *ApiService) handleFreshCasbin(ctx http.Context) error {
|
|
if err := s.FreshCasbin(); err != nil {
|
|
return errors.InternalServer("FRESH_FAILED", err.Error())
|
|
}
|
|
return ctx.Result(200, map[string]any{"code": 0, "msg": "刷新成功"})
|
|
}
|