You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.5 KiB
Smarty
62 lines
1.5 KiB
Smarty
package svc
|
|
|
|
import (
|
|
{{.configImport}}
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"git.noahlan.cn/noahlan/ntool/nlog"
|
|
"git.noahlan.cn/noahlan/ntool-biz/zero/logz"
|
|
{{if .useI18n}}
|
|
"git.noahlan.cn/noahlan/ntool-biz/core/i18n"{{end}}
|
|
{{if .useEnt}}
|
|
"{{.projectPackage}}/ent"{{end}}
|
|
{{if .useCasbin}}
|
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
|
"github.com/zeromicro/go-zero/rest"
|
|
"github.com/casbin/casbin/v2"{{end}}
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config {{.config}}
|
|
{{.middleware}}
|
|
{{if .useCasbin}}Casbin *casbin.Enforcer
|
|
Authority rest.Middleware{{end}}
|
|
{{if .useEnt}}
|
|
DB *ent.Client{{end}}
|
|
}
|
|
|
|
func NewServiceContext(c {{.config}}) *ServiceContext {
|
|
// 配置nlog-logx适配器
|
|
nlog.MustSetup(logz.WrapConf(c.Log))
|
|
logx.SetWriter(logz.NewWriter())
|
|
{{if .useI18n}}
|
|
// 配置多语言
|
|
i18n.InitWithConfig(c.I18nConf)
|
|
{{end}}
|
|
|
|
{{if .useCasbin}}
|
|
rds := redis.MustNewRedis(redis.RedisConf{
|
|
Host: c.RedisConf.Addr,
|
|
Type: c.RedisConf.Type,
|
|
Pass: c.RedisConf.Password,
|
|
Tls: c.RedisConf.Tls,
|
|
})
|
|
cbn := c.CasbinConf.MustNewCasbinWithRedisWatcher(c.DatabaseConf.Type, c.DatabaseConf.GetDSN(), c.RedisConf)
|
|
{{end}}
|
|
|
|
{{if .useEnt}}
|
|
db := ent.NewClient(
|
|
ent.Log(nlog.Info), // logger
|
|
ent.Driver(c.DatabaseConf.NewNoCacheDriver()),
|
|
ent.Debug(), // debug mode
|
|
)
|
|
{{end}}
|
|
return &ServiceContext{
|
|
Config: c,
|
|
{{if .useCasbin}}
|
|
Authority: middleware.NewAuthorityMiddleware(cbn, rds).Handle,
|
|
Casbin: cbn,
|
|
{{end}}
|
|
{{if .useEnt}}DB: db,{{end}}
|
|
}
|
|
}
|