youtu_grpc/app/admin_service/internal/svc/service_context.go

34 lines
600 B
Go
Raw Normal View History

2025-02-21 18:24:39 +08:00
package svc
import (
2025-02-23 21:50:55 +08:00
"log"
"time"
2025-02-21 18:24:39 +08:00
"gitea.youtukeji.com.cn/youtu/youtu_grpc/pkg/config"
clientv3 "go.etcd.io/etcd/client/v3"
)
type ServiceContext struct {
2025-02-23 21:50:55 +08:00
Config config.Config
EtcdClient *clientv3.Client
2025-02-24 18:47:27 +08:00
Cached *AppDataCache
2025-02-21 18:24:39 +08:00
}
func NewServiceContext(c config.Config) *ServiceContext {
2025-02-23 21:50:55 +08:00
etcdClient, err := clientv3.New(clientv3.Config{
Endpoints: c.RpcServerConf.Etcd.Hosts,
DialTimeout: 3 * time.Second,
})
2025-02-21 18:24:39 +08:00
if err != nil {
2025-02-23 21:50:55 +08:00
log.Fatal(err)
2025-02-21 18:24:39 +08:00
}
2025-02-24 18:47:27 +08:00
svc := &ServiceContext{
2025-02-23 21:50:55 +08:00
Config: c,
EtcdClient: etcdClient,
2025-02-24 18:47:27 +08:00
Cached: NewAppDataCache(etcdClient),
2025-02-21 18:24:39 +08:00
}
2025-02-24 18:47:27 +08:00
return svc
2025-02-21 18:24:39 +08:00
}