package svc

import (
	"log"
	"time"

	"gitea.youtukeji.com.cn/youtu/youtu_grpc/pkg/config"
	clientv3 "go.etcd.io/etcd/client/v3"
)

type ServiceContext struct {
	Config     config.Config
	EtcdClient *clientv3.Client
	Cached     *AppDataCache
}

func NewServiceContext(c config.Config) *ServiceContext {
	etcdClient, err := clientv3.New(clientv3.Config{
		Endpoints:   c.RpcServerConf.Etcd.Hosts,
		DialTimeout: 3 * time.Second,
	})
	if err != nil {
		log.Fatal(err)
	}

	svc := &ServiceContext{
		Config:     c,
		EtcdClient: etcdClient,
		Cached:     NewAppDataCache(etcdClient),
	}

	return svc
}