1 Star 0 Fork 0

CaptialSTeam/ubdframe

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api_server.go 7.50 KB
一键复制 编辑 原始数据 按行查看 历史
sage 提交于 2025-01-13 15:20 +08:00 . modify rbac
package adminapp
import (
"gitee.com/captials-team/ubdframe/src/apps"
httpController "gitee.com/captials-team/ubdframe/src/apps/adminapp/controllers/http"
"gitee.com/captials-team/ubdframe/src/apps/adminapp/docs"
"gitee.com/captials-team/ubdframe/src/common"
"gitee.com/captials-team/ubdframe/src/common/consts"
"gitee.com/captials-team/ubdframe/src/domain/configstc"
"gitee.com/captials-team/ubdframe/src/domain/interfaces"
"gitee.com/captials-team/ubdframe/src/pkg/gin_http"
"gitee.com/captials-team/ubdframe/src/pkg/jwtauth"
v1log "gitee.com/captials-team/ubdframe/src/pkg/logs"
"gitee.com/captials-team/ubdframe/src/pkg/uber_help"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"go.uber.org/dig"
"net/http"
)
type ApiServer struct {
*apps.ApiServer
di *dig.Container
conf *configstc.AdminAppConfig
gin_http.AuthOption //auth相关选项配置
gin_http.SwaggerOption //swagger相关选项配置
gin_http.PProfOption //pprof选项配置
gin_http.OperateLogOption //操作日志
gin_http.AuthExtendInfoOption //认证扩展信息选项配置
}
func (s *ApiServer) Name() string {
return "admin_api"
}
func (s *ApiServer) InitRouter() {
s.Engine().GET("ping", gin_http.PingHandler)
s.InitRouterForGin(s.Engine())
}
func (s *ApiServer) router(g gin.IRouter) {
corsConfig := cors.DefaultConfig()
corsConfig.AllowOrigins = []string{"*"}
corsConfig.AllowCredentials = true
corsConfig.AllowHeaders = append(corsConfig.AllowHeaders, consts.AcceptAuthorizationKey, consts.AcceptLanguageKey, "Access-Control-Allow-Origin", "Referer")
corsConfig.OptionsResponseStatusCode = http.StatusOK
g.Use(
cors.New(corsConfig),
gin_http.PanicHandler,
gin_http.QPSLimiterHandler(10, 10),
)
authGroup := g.Group("", s.OptAuthHandler())
allowMethods := []string{http.MethodPost, http.MethodOptions}
common.ErrPanic(s.di.Invoke(func(ctr interfaces.ItfAuthController, captcha interfaces.ItfCaptchaController) {
if d, ok := ctr.(*httpController.AuthController); ok {
d.AuthExtendInfoOption = s.AuthExtendInfoOption
}
}))
common.ErrPanic(s.di.Invoke(func(ctr interfaces.ItfAuthController, captcha interfaces.ItfCaptchaController) {
//验证码
g.Match(allowMethods, "/mag/captcha/get", captcha.GetCaptcha) //图片验证码
//登陆相关
g.Match(allowMethods, "/mag/auth/login", captcha.VerifyHandler(), ctr.AuthLogin, s.RecordLog("登录")) //不需要授权
authGroup.Match(allowMethods, "/mag/auth/info", ctr.AuthInfo)
authGroup.Match(allowMethods, "/mag/auth/logout", ctr.AuthLogout, s.RecordLog("登出"))
authGroup.Match(allowMethods, "/mag/auth/fresh_token", ctr.AuthFreshToken)
}))
//个人中心
common.ErrPanic(s.di.Invoke(func(ctr interfaces.ItfUserCenterController) {
authGroup.Match(allowMethods, "/mag/u_center/modify_pwd", ctr.ModifyPassword, s.RecordLog("修改密码")) //修改密码(登录情况下)
authGroup.Match(allowMethods, "/mag/u_center/modify_info", ctr.ModifyInfo, s.RecordLog("修改个人信息"))
}))
common.ErrPanic(s.di.Invoke(func(ctr interfaces.ItfAccreditController) {
authGroup.Match(allowMethods, "/mag/u_center/accredit_info", ctr.AccreditInfo)
}))
//管理员管理
common.ErrPanic(s.di.Invoke(func(ctr interfaces.ItfAdminManageController) {
//管理员管理
authGroup.Match(allowMethods, "/mag/admin/search", ctr.SearchAdmin, s.RecordLog("进入管理员管理"))
authGroup.Match(allowMethods, "/mag/admin/detail", ctr.QueryAdmin, s.RecordLog("查看管理员"))
authGroup.Match(allowMethods, "/mag/admin/save", ctr.SaveAdmin, s.RecordLog("创建/更新管理员"))
authGroup.Match(allowMethods, "/mag/admin/disable", ctr.DisableAdmin, s.RecordLog("禁用管理员"))
authGroup.Match(allowMethods, "/mag/admin/delete", ctr.DeleteAdmin, s.RecordLog("删除管理员"))
}))
//管理员日志
common.ErrPanic(s.di.Invoke(func(ctr interfaces.ItfAdminLogController) {
//管理员管理
authGroup.Match(allowMethods, "/mag/admin_log/search", ctr.SearchAdminLog, s.RecordLog("进入日志管理"))
}))
//权限管理(RBAC)
common.ErrPanic(s.di.Invoke(func(ctr interfaces.ItfRbacManageController) {
//rbac-用户角色信息
authGroup.Match(allowMethods, "/mag/rbac/user/role/search", ctr.SearchUserRoles)
authGroup.Match(allowMethods, "/mag/rbac/user/role/bind", ctr.BindUserRoles, s.RecordLog("绑定用户角色"))
//rbac-角色
authGroup.Match(allowMethods, "/mag/rbac/role/search", ctr.SearchRoles, s.RecordLog("进入角色管理"))
authGroup.Match(allowMethods, "/mag/rbac/role/simples", ctr.SimpleRoles) //简易返回所有角色
authGroup.Match(allowMethods, "/mag/rbac/role/detail", ctr.QueryRole, s.RecordLog("查看角色详情"))
authGroup.Match(allowMethods, "/mag/rbac/role/save", ctr.SaveRole, s.RecordLog("新增/更新角色信息"))
authGroup.Match(allowMethods, "/mag/rbac/role/disable", ctr.DisableRole, s.RecordLog("启用/禁用角色"))
authGroup.Match(allowMethods, "/mag/rbac/role/delete", ctr.DeleteRole, s.RecordLog("删除角色"))
authGroup.Match(allowMethods, "/mag/rbac/role/permissions", ctr.SearchRolePermissions)
authGroup.Match(allowMethods, "/mag/rbac/role/permission/save", ctr.SaveRolePermissions, s.RecordLog("更新角色权限"))
//rbac-权限
authGroup.Match(allowMethods, "/mag/rbac/permission/search", ctr.SearchPermissions, s.RecordLog("进入权限树管理"))
authGroup.Match(allowMethods, "/mag/rbac/permission/save", ctr.SavePermission, s.RecordLog("新增/更新权限树节点"))
authGroup.Match(allowMethods, "/mag/rbac/permission/delete", ctr.DeletePermission, s.RecordLog("删除权限树节点"))
}))
}
func (s *ApiServer) InitRouterForGin(engine *gin.Engine) {
var g = engine.Group("")
if len(s.conf.RoutePrefix) > 0 {
g = engine.Group(s.conf.RoutePrefix)
}
//注册swagger
s.SwaggerRouter(g)
//注册pprof
s.PProfRouter(engine)
s.router(g)
return
}
func (s *ApiServer) Start() error {
s.InitRouter()
return s.ApiServer.Start()
}
func (s *ApiServer) Stop() error {
return s.ApiServer.Stop()
}
func NewApiServer(di *dig.Container, conf *configstc.AdminAppConfig, logger v1log.ILog) *ApiServer {
common.ErrPanic(di.Provide(gin_http.NewCaptchaController, dig.As(new(interfaces.ItfCaptchaController))), uber_help.ErrAlreadyProvided)
common.ErrPanic(di.Provide(httpController.NewAuthController, dig.As(new(interfaces.ItfAuthController))), uber_help.ErrAlreadyProvided)
common.ErrPanic(di.Provide(httpController.NewAdminController, dig.As(new(interfaces.ItfAdminManageController)), dig.As(new(interfaces.ItfUserCenterController))), uber_help.ErrAlreadyProvided)
common.ErrPanic(di.Provide(httpController.NewRbacController, dig.As(new(interfaces.ItfRbacManageController)), dig.As(new(interfaces.ItfAccreditController))), uber_help.ErrAlreadyProvided)
common.ErrPanic(di.Provide(httpController.NewAdminLogController, dig.As(new(interfaces.ItfAdminLogController))), uber_help.ErrAlreadyProvided)
docs.SwaggerInfoadminservice.Host = conf.ApiServer.HostAddr() + conf.RoutePrefix
s := &ApiServer{
di: di,
conf: conf,
SwaggerOption: gin_http.SwaggerOption{
Enable: conf.DocsEnable,
Name: docs.SwaggerInfoadminservice.InstanceName(),
Swagger: docs.SwaggerInfoadminservice,
},
PProfOption: gin_http.PProfOption{
Enable: conf.PProfEnable,
},
ApiServer: apps.NewApiServer(gin.Default(), conf.ApiServer),
}
s.AuthOption.AuthHandler = gin_http.AuthHandler(jwtauth.NewJwtTokenHandler(conf.AuthConfig.SecretKey))
common.ErrPanic(s.di.Invoke(func(ctr interfaces.ItfAdminLogController) {
s.OperateLogOption.OperateLogHandler = ctr.LogHandler()
}))
return s
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/captials-team/ubdframe.git
git@gitee.com:captials-team/ubdframe.git
captials-team
ubdframe
ubdframe
v1.0.1

搜索帮助