youtu_grpc/app/auth_service/internal/svc/service_context.go
xiabin b0d719312b
Some checks are pending
/ build-services (app/admin_service/Dockerfile, admin, admin) (push) Waiting to run
/ build-services (app/auth_service/Dockerfile, auth, auth) (push) Waiting to run
/ build-services (app/ecpm_service/Dockerfile, ecpm, ecpm) (push) Waiting to run
/ build-services (app/ranking_service/Dockerfile, ranking, ranking) (push) Waiting to run
/ build-services (app/user_service/Dockerfile, user, user) (push) Waiting to run
/ start-services (push) Blocked by required conditions
通过配置合并简化配置文件
2025-03-05 09:18:45 +08:00

125 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package svc
import (
"context"
"encoding/json"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/auth_service/internal/gen/dao/query"
cli2 "gitea.youtukeji.com.cn/youtu/youtu_grpc/app/auth_service/pkg"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/user_service/userservice"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/pkg/config"
"gitea.youtukeji.com.cn/youtu/youtu_grpc/pkg/my_gorm"
"github.com/redis/go-redis/v9"
"github.com/silenceper/wechat/v2/cache"
redisCache "github.com/silenceper/wechat/v2/cache"
"github.com/spf13/viper"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/discov"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/zrpc"
clientv3 "go.etcd.io/etcd/client/v3"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"strings"
)
type ServiceContext struct {
Config config.Config
Cli cli2.Helper
UserManagerClient userservice.UserService
Query *query.Query
dwCache *cache.Redis
}
func NewServiceContext(c config.Config) *ServiceContext {
svc := &ServiceContext{
Config: c,
}
clientConf := zrpc.RpcClientConf{}
err := conf.FillDefault(&clientConf) // 填充默认值,比如 trace 透传等,参考服务配置说明
if err != nil {
panic(err)
}
clientConf.Token = "user_service.rpc.key"
clientConf.App = "user_service.rpc"
clientConf.Etcd = discov.EtcdConf{ // 通过 etcd 服务发现
Hosts: []string{viper.GetString(config.EtcdAddrKey)},
Key: "user_service.rpc",
}
//初始化redis client
redisClient := redis.NewClient(&redis.Options{
Addr: c.Redis[0].Host,
})
//初始化数据库
db, err := my_gorm.NewDBWithCache(mysql.Open(c.Mysql), &gorm.Config{}, redisClient)
if err != nil {
panic(err)
}
svc.Query = query.Use(db)
svc.UserManagerClient = userservice.NewUserService(zrpc.MustNewClient(clientConf))
svc.InitClient()
return svc
}
type AppData struct {
AppId string `json:"appId"`
AppSecret string `json:"appSecret"`
Type int32 `json:"type"`
}
type AppDataList []AppData
const AppDataWatchKey = "/youtu/app/account/"
func (svc *ServiceContext) InitClient() {
svc.dwCache = redisCache.NewRedis(context.Background(), &redisCache.RedisOpts{Host: svc.Config.Redis[0].Host})
cli, err := clientv3.NewFromURL(viper.GetString(config.EtcdAddrKey))
if err != nil {
panic(err)
}
go func() {
ch := make(chan config.WatchKV)
go config.EtcdGetOneAndWatch(context.TODO(), cli, AppDataWatchKey, ch)
//从通道中尝试取值(监视的信息)
for res := range ch {
appData := AppData{}
err = json.Unmarshal(res.Value, &appData)
if err != nil {
logx.Errorf("etcd watch %s json.Unmarshal: %v", AppDataWatchKey, err)
continue
}
appData.AppId = strings.TrimPrefix(res.Key, AppDataWatchKey)
svc.SaveDW(appData, svc.dwCache)
}
}()
}
func (svc *ServiceContext) SaveDW(v AppData, dwCache cache.Cache) {
//配置小程序cli抖音&微信)
var c cli2.DWClient
switch v.Type {
case cli2.DouyinClientType:
c = cli2.NewDouYinApi(v.AppId, v.AppSecret, dwCache)
case cli2.WechatClientType:
c = cli2.NewWechatApi(v.AppId, v.AppSecret, dwCache)
default:
return
}
svc.Cli.Set(v.AppId, c)
}
func (svc *ServiceContext) DeleteDWCache(appId string) (err error) {
err = svc.dwCache.Delete(appId)
return err
}