全局变量
小于 1 分钟
我们将全局变量定义到
internal/svc/servicecontext.go
中,并初始化
例如
package svc
import (
"github.com/suyuan32/simple-admin-core/api/internal/config"
"github.com/suyuan32/simple-admin-core/api/internal/middleware"
"github.com/suyuan32/simple-admin-core/api/internal/i18n"
"github.com/suyuan32/simple-admin-core/rpc/coreclient"
"github.com/casbin/casbin/v2"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/zrpc"
)
type ServiceContext struct {
Config config.Config
Authority rest.Middleware
CoreRpc coreclient.Core
Redis *redis.Redis
Casbin *casbin.Enforcer
Trans *i18n.Translator
}
func NewServiceContext(c config.Config) *ServiceContext {
rds := redis.MustNewRedis(c.RedisConf)
cbn := c.CasbinConf.MustNewCasbinWithRedisWatcher(c.DatabaseConf.Type, c.DatabaseConf.GetDSN(), c.RedisConf)
trans := i18n.NewTranslator(i18n.LocaleFS)
return &ServiceContext{
Config: c,
Authority: middleware.NewAuthorityMiddleware(cbn, rds, trans).Handle,
CoreRpc: coreclient.NewCore(zrpc.NewClientIfEnable(c.CoreRpc)),
Redis: rds,
Casbin: cbn,
Trans: trans,
}
}
使用方法
package api
import (
"context"
"github.com/suyuan32/simple-admin-core/rpc/utils/errorhandler"
"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/types/core"
"github.com/zeromicro/go-zero/core/logx"
"github.com/suyuan32/simple-admin-common/i18n"
)
type CreateApiLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewCreateApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateApiLogic {
return &CreateApiLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *CreateApiLogic) CreateApi(in *core.ApiInfo) (*core.BaseIDResp, error) {
result, err := l.svcCtx.DB.API.Create().
SetPath(in.Path).
SetDescription(in.Description).
SetAPIGroup(in.ApiGroup).
SetMethod(in.Method).
Save(l.ctx)
if err != nil {
return nil, errorhandler.DefaultEntError(l.Logger, err, in)
}
return &core.BaseIDResp{Id: result.ID, Msg: i18n.CreateSuccess}, nil
}
我们可以通过 l.svcCtx 访问全局变量
全局获取变量方法
版本 >= v1.4.10
在 v1.4.10 版本之后我们提供了方法可以在 API 和 RPC 层任意地方获取关键变量
- 获取用户 ID
userId, err := userctx.GetUserIDFromCtx(ctx)
- 获取部门 ID
deptId, err := deptctx.GetDepartmentIDFromCtx(ctx)
- 获取角色代码
roleCodes, err := rolectx.GetRoleIDFromCtx(ctx)