Global Variables
December 19, 2022Less than 1 minute
We define the global variables in
internal/svc/servicecontext.go
, and initialize them.
For example:
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,
}
}
How to use it?
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
}
We can get the global variables via l.svcCtx.
Global Variable Retrieval Methods
Version >= v1.4.10
Starting from version v1.4.10, we offer methods to retrieve critical variables anywhere in the API and RPC layers:
- Get User ID:
userId, err := userctx.GetUserIDFromCtx(ctx)
- Get Department ID:
deptId, err := deptctx.GetDepartmentIDFromCtx(ctx)
- Get Role Codes:
roleCodes, err := rolectx.GetRoleIDFromCtx(ctx)