210 lines
5.8 KiB
Go
210 lines
5.8 KiB
Go
package server
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func registerAPIRoutes(group *gin.RouterGroup, engine *gin.Engine, svc *service.AccessService) {
|
|
router := group.Group("/api")
|
|
router.POST("/getApiList", func(c *gin.Context) {
|
|
var req struct {
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
Path string `json:"path"`
|
|
Description string `json:"description"`
|
|
APIGroup string `json:"apiGroup"`
|
|
Method string `json:"method"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
items, total, err := svc.APIs(c.Request.Context(), req.Page, req.PageSize, &biz.API{Path: req.Path, Description: req.Description, APIGroup: req.APIGroup, Method: req.Method})
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
|
|
})
|
|
router.POST("/getAllApis", func(c *gin.Context) {
|
|
items, _, err := svc.APIs(c.Request.Context(), 1, 10000, nil)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"apis": items}, "获取成功")
|
|
})
|
|
type apiRequest struct {
|
|
ID uint `json:"ID"`
|
|
Path string `json:"path"`
|
|
Description string `json:"description"`
|
|
APIGroup string `json:"apiGroup"`
|
|
Method string `json:"method"`
|
|
}
|
|
router.POST("/createApi", func(c *gin.Context) {
|
|
var req apiRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.CreateAPI(c.Request.Context(), &biz.API{Path: req.Path, Description: req.Description, APIGroup: req.APIGroup, Method: req.Method}); err != nil {
|
|
fail(c, "创建失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.POST("/updateApi", func(c *gin.Context) {
|
|
var req apiRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.UpdateAPI(c.Request.Context(), &biz.API{ID: req.ID, Path: req.Path, Description: req.Description, APIGroup: req.APIGroup, Method: req.Method}); err != nil {
|
|
fail(c, "更新失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.POST("/deleteApi", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeleteAPIs(c.Request.Context(), []uint{req.ID}); err != nil {
|
|
fail(c, "删除失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.DELETE("/deleteApisByIds", func(c *gin.Context) {
|
|
var req struct {
|
|
IDs []uint `json:"ids"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeleteAPIs(c.Request.Context(), req.IDs); err != nil {
|
|
fail(c, "删除失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.POST("/getApiById", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"id"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
item, err := svc.API(c.Request.Context(), req.ID)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"api": item}, "获取成功")
|
|
})
|
|
router.GET("/getApiGroups", func(c *gin.Context) {
|
|
items, _, err := svc.APIs(c.Request.Context(), 1, 10000, nil)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
seen := map[string]bool{}
|
|
groups := []string{}
|
|
for _, item := range items {
|
|
value, _ := item["apiGroup"].(string)
|
|
if value != "" && !seen[value] {
|
|
seen[value] = true
|
|
groups = append(groups, value)
|
|
}
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"groups": groups}, "获取成功")
|
|
})
|
|
router.GET("/getApiRoles", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("apiId"), 10, 64)
|
|
ids, err := svc.APIRoleIDs(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, ids, "获取成功")
|
|
})
|
|
router.POST("/setApiRoles", func(c *gin.Context) {
|
|
var req struct {
|
|
APIID uint `json:"apiId"`
|
|
AuthorityIDs []uint `json:"authorityIds"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SetAPIRoles(c.Request.Context(), req.APIID, req.AuthorityIDs); err != nil {
|
|
fail(c, "设置失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.GET("/syncApi", func(c *gin.Context) {
|
|
routes := engine.Routes()
|
|
items := make([]*biz.API, 0, len(routes))
|
|
for _, route := range routes {
|
|
items = append(items, &biz.API{Path: route.Path, Method: route.Method})
|
|
}
|
|
result, err := svc.SyncAPIs(c.Request.Context(), items)
|
|
if err != nil {
|
|
fail(c, "同步检查失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, result, "获取成功")
|
|
})
|
|
router.POST("/ignoreApi", func(c *gin.Context) {
|
|
var req struct {
|
|
Path string `json:"path"`
|
|
Method string `json:"method"`
|
|
Flag bool `json:"flag"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil || req.Path == "" || req.Method == "" {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SetAPIIgnored(c.Request.Context(), req.Path, req.Method, req.Flag); err != nil {
|
|
fail(c, "忽略设置失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.POST("/enterSyncApi", func(c *gin.Context) {
|
|
var req struct {
|
|
NewAPIs []apiRequest `json:"newApis"`
|
|
DeleteAPIs []apiRequest `json:"deleteApis"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
toDomain := func(values []apiRequest) []*biz.API {
|
|
items := make([]*biz.API, 0, len(values))
|
|
for _, value := range values {
|
|
items = append(items, &biz.API{ID: value.ID, Path: value.Path, Description: value.Description, APIGroup: value.APIGroup, Method: value.Method})
|
|
}
|
|
return items
|
|
}
|
|
if err := svc.ApplyAPISync(c.Request.Context(), toDomain(req.NewAPIs), toDomain(req.DeleteAPIs)); err != nil {
|
|
fail(c, "同步失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.GET("/freshCasbin", func(c *gin.Context) { ok(c) })
|
|
}
|