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.
goctls-template/api/context.tpl

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}}
}
}