47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package svc
|
|
|
|
import (
|
|
"github.com/ip2location/ip2location-go/v9"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"github.com/zeromicro/go-zero/zrpc"
|
|
"mingyang-admin-app-rpc/ent"
|
|
"mingyang-admin-app-rpc/internal/config"
|
|
"mingyang-admin-simple-admin-message/mcmsclient"
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config config.Config
|
|
DB *ent.Client
|
|
Redis redis.UniversalClient
|
|
IPDB *ip2location.DB
|
|
McmsRpc mcmsclient.Mcms
|
|
}
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
|
|
|
entOpts := []ent.Option{
|
|
ent.Log(logx.Info),
|
|
ent.Driver(c.DatabaseConf.NewNoCacheDriver()),
|
|
}
|
|
|
|
if c.DatabaseConf.Debug {
|
|
entOpts = append(entOpts, ent.Debug())
|
|
}
|
|
|
|
db := ent.NewClient(entOpts...)
|
|
|
|
ipDB, err := ip2location.OpenDB(c.IP2Location.DBPath)
|
|
if err != nil {
|
|
logx.Errorw("failed to open ip2location db", logx.Field("detail", err))
|
|
}
|
|
|
|
return &ServiceContext{
|
|
Config: c,
|
|
DB: db,
|
|
IPDB: ipDB,
|
|
Redis: c.RedisConf.MustNewUniversalRedis(),
|
|
McmsRpc: mcmsclient.NewMcms(zrpc.MustNewClient(c.McmsRpc)),
|
|
}
|
|
}
|