package svc import ( "github.com/elastic/go-elasticsearch/v8" "github.com/zeromicro/go-zero/core/stores/redis" "github.com/zeromicro/go-zero/zrpc" "net/http" socialRpc "paopaoimrpc/apps/client/paopaosocial" userRpc "paopaoimrpc/apps/client/paopaouser" "paopaoimrpc/apps/pkg/constants" "paopaoimtask/internal/config" "paopaoimtask/pkg/es" "paopaoimws/immodels" "paopaoimws/websocket" ) type ServiceContext struct { Config config.Config WsClient websocket.Client *redis.Redis immodels.ChatLogModel immodels.ConversationModel EsClient *elasticsearch.Client userRpc.PaoPaoUser socialRpc.PaoPaoSocial } func NewServiceContext(c config.Config) *ServiceContext { esClient, err := es.GetEsClient(c.ElasticSearchConf.Host, c.ElasticSearchConf.Port, c.ElasticSearchConf.Username, c.ElasticSearchConf.Password) if err != nil { panic(err) } userrpc, _ := zrpc.NewClient(c.UserRpc) socialrpc, _ := zrpc.NewClient(c.SocialRpc) svc := &ServiceContext{ Config: c, Redis: redis.MustNewRedis(c.Redisx), ChatLogModel: immodels.MustChatLogModel(c.Mongo.Url, c.Mongo.Db), ConversationModel: immodels.MustConversationModel(c.Mongo.Url, c.Mongo.Db), PaoPaoUser: userRpc.NewPaoPaoUser(userrpc), PaoPaoSocial: socialRpc.NewPaoPaoSocial(socialrpc), EsClient: esClient, } token, err := svc.GetSystemToken() if err != nil { panic(err) } header := http.Header{} header.Set("Authorization", token) svc.WsClient = websocket.NewClient(c.Ws.Host, websocket.WithClientHeader(header)) return svc } func (s *ServiceContext) GetSystemToken() (string, error) { get, err := s.Redis.Get(constants.REDIS_SYSTEM_ROOT_TOKEN) return get, err }