2 Star 0 Fork 0

hero/momo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
momo.go 6.63 KB
一键复制 编辑 原始数据 按行查看 历史
hero 提交于 2024-11-21 11:30 . add:firewall
package momo
import (
"fmt"
"runtime"
rbacModel "gitee.com/linqwen/momo/app/rbac/model"
rbacService "gitee.com/linqwen/momo/app/rbac/service"
"gitee.com/linqwen/momo/logger"
"gitee.com/linqwen/momo/middle"
"gitee.com/linqwen/momo/rbac"
confCtl "gitee.com/linqwen/momo/app/conf/controller"
genCtl "gitee.com/linqwen/momo/app/gen/controller"
jobCtl "gitee.com/linqwen/momo/app/job/controller"
logCtl "gitee.com/linqwen/momo/app/log/controller"
captchaCtl "gitee.com/linqwen/momo/app/rbac/captcha/controller"
rbacCtl "gitee.com/linqwen/momo/app/rbac/controller"
cacheCtl "gitee.com/linqwen/momo/app/srv/cache/controllers"
fileCtl "gitee.com/linqwen/momo/app/srv/filemgr/controller"
firewallCtl "gitee.com/linqwen/momo/app/srv/firewall/controller"
queueCtl "gitee.com/linqwen/momo/app/srv/queue/controller"
"gitee.com/linqwen/momo/igin"
)
func GenRouter(router igin.IRouterGroup) {
genGroup := router.Group("api/gen")
genCtl.TableRouters(genGroup)
genCtl.GenRoutes(genGroup)
genCtl.TmplRouters(genGroup)
genCtl.DataSourceRouter(genGroup)
}
func LogRouter(router igin.IRouterGroup) {
logGroup := router.Group("api/log")
logCtl.LogCtxRouter(logGroup)
logCtl.LogErrRouter(logGroup)
logCtl.LogInfoRouter(logGroup)
logCtl.AccessRouter(logGroup)
}
func ConfRouter(router igin.IRouterGroup) {
confGroup := router.Group("api/conf")
confCtl.DataSourceRouter(confGroup)
confCtl.AccessctlRouter(confGroup)
confCtl.ClientRouter(confGroup)
confCtl.DictRouter(confGroup)
confCtl.ThrottleRouter(confGroup)
}
func SrvRouter(router igin.IRouterGroup) {
srvGroup := router.Group("api/srv")
fileCtl.FileRouter(srvGroup)
cacheCtl.CacheRoutes(srvGroup)
queueCtl.QueueRoutes(srvGroup)
firewallCtl.FirewallCtlRouters(srvGroup)
}
func RbacRouter(router igin.IRouterGroup) {
rbacGroup := router.Group("api/rbac")
rbacCtl.MenuRouters(rbacGroup)
rbacCtl.UserRouter(rbacGroup)
rbacCtl.RoleRouter(rbacGroup)
captchaCtl.CaptchaRouter(router)
}
func JobRouter(router igin.IRouterGroup) {
jobGroup := router.Group("api/job")
jobCtl.ScheduleRouter(jobGroup)
jobCtl.ScheduleLogRouter(jobGroup)
jobCtl.TaskLogRouter(jobGroup)
}
func CORSMiddle() igin.HandlerFunc {
return func(c igin.IContext) {
c.Writer().Header().Set("Access-Control-Allow-Origin", "*")
c.Writer().Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Writer().Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if c.Request().Method == "OPTIONS" {
c.Writer().WriteHeader(200)
c.Writer().WriteHeaderNow()
c.Abort()
return
}
c.Next()
}
}
func SysMiddle(router igin.Engine, maxCores int) {
// 设置程序使用的最大 CPU 核心数为 1
runtime.GOMAXPROCS(maxCores)
router.Use(igin.LoggerWithFormatter(func(param igin.LogFormatterParams) string {
return fmt.Sprintf("%s-[%s] %d %s \"%s %s %s \"%s\"\n",
param.ClientIP,
param.TimeStamp.Format("2006-01-02T15:04:05Z07"),
param.StatusCode,
param.Latency,
param.Method,
param.Path,
param.Request.Proto,
// param.Request.UserAgent(),
param.ErrorMessage,
)
}))
// router.Use(CORSMiddle())
router.Use(logger.PanicMiddle())
}
type RouterBuilder interface {
WithPromePort(port int) RouterBuilder
WithMode(mode string) RouterBuilder
WithSecretKey(secretKey string) RouterBuilder
WithService(service rbac.IRBACService) RouterBuilder
WithAcl(acl bool) RouterBuilder
WithAcs(acs bool) RouterBuilder
WithLog(log bool) RouterBuilder
Build() igin.Engine
}
type routerBuilder struct {
config *RouterConfig
}
type RouterConfig struct {
Mode string
SecretKey string
Acl bool
Acs bool
Log bool
Service rbac.IRBACService
PromePort int
cpu int
}
func NewRouterBuilder() RouterBuilder {
return &routerBuilder{
config: &RouterConfig{
Mode: "release",
SecretKey: "default_secret_key",
Acl: true,
Acs: true,
Log: true,
PromePort: 6015,
cpu: 2,
},
}
}
func (rb *routerBuilder) WithLog(log bool) RouterBuilder {
rb.config.Log = log
return rb
}
func (rb *routerBuilder) WithPromePort(port int) RouterBuilder {
rb.config.PromePort = port
return rb
}
func (rb *routerBuilder) WithMode(mode string) RouterBuilder {
rb.config.Mode = mode
return rb
}
func (rb *routerBuilder) WithAcl(acl bool) RouterBuilder {
rb.config.Acl = acl
return rb
}
func (rb *routerBuilder) WithAcs(acs bool) RouterBuilder {
rb.config.Acs = acs
return rb
}
func (rb *routerBuilder) WithSecretKey(secretKey string) RouterBuilder {
rb.config.SecretKey = secretKey
return rb
}
func (rb *routerBuilder) WithService(service rbac.IRBACService) RouterBuilder {
rb.config.Service = service
return rb
}
func (rb *routerBuilder) Build() igin.Engine {
// if err := igin.SetLogFile("data/files/log.log"); err != nil {
// logger.Error("logfile", "logfile create error")
// }
igin.SetMode(rb.config.Mode)
router := igin.NewGinEngine()
SysMiddle(router, rb.config.cpu)
if rb.config.Acl {
router.Use(middle.ACLMiddle())
}
if rb.config.Service == nil {
db := rbacService.RbacDb
userRepo := rbac.NewGormUserRepo(db, &rbacModel.UserEntity{})
roleRepo := rbac.NewGormRoleRepo(db, &rbacModel.RoleEntity{})
permitRepo := rbac.NewGormPermitRepo(db, &rbacModel.MenuEntity{})
rb.config.Service = rbac.NewRBACService(userRepo, roleRepo, permitRepo, rb.config.SecretKey)
rbac.InitAuth(rb.config.Service)
} else {
rbac.InitAuth(rb.config.Service)
}
router.Use(rbac.Auth.SetAuth())
if rb.config.Acs {
router.Use(middle.AcsMiddle())
}
middle.RegisterPrometheus(rb.config.PromePort)
router.Use(middle.PrometheusMiddle())
router.Use(middle.ThrottleMiddle(""))
if rb.config.Log {
router.Use(logger.LogInfoMiddle())
}
appGroup := router.Group("")
appGroupAdapter := &igin.GinRouterGroupAdapter{RouterGroup: appGroup}
RbacRouter(appGroupAdapter)
SrvRouter(appGroupAdapter)
ConfRouter(appGroupAdapter)
LogRouter(appGroupAdapter)
JobRouter(appGroupAdapter)
GenRouter(appGroupAdapter)
return router
}
func MinRouter(port int, group, secretKey string, service rbac.IRBACService) igin.Engine {
// if err := igin.SetLogFile("data/files/log.log"); err != nil {
// logger.Error("logfile", "logfile create error")
// }
// igin.DisableConsoleColor()
router := igin.NewGinEngine()
SysMiddle(router, 2)
if service == nil {
db := rbacService.RbacDb
userRepo := rbac.NewGormUserRepo(db, &rbacModel.UserEntity{})
roleRepo := rbac.NewGormRoleRepo(db, &rbacModel.RoleEntity{})
permitRepo := rbac.NewGormPermitRepo(db, &rbacModel.MenuEntity{})
rbacService := rbac.NewRBACService(userRepo, roleRepo, permitRepo, secretKey)
rbac.InitAuth(rbacService)
} else {
rbac.InitAuth(service)
}
router.Use(rbac.Auth.SetAuth())
return router
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/linqwen/momo.git
git@gitee.com:linqwen/momo.git
linqwen
momo
momo
v1.1.16

搜索帮助