package dao import ( "context" "github.com/go-kratos/kratos/contrib/registry/consul/v2" "github.com/go-kratos/kratos/v2/log" "github.com/go-kratos/kratos/v2/middleware/recovery" "github.com/go-kratos/kratos/v2/transport/grpc" "github.com/google/wire" "gorm.io/gorm" "hilo/api/service/country" "hilo/api/service/mgr" "hilo/app/service/user/internal/conf" "hilo/common/httpclient" "hilo/common/mysql" "hilo/common/redis" "net/http" "time" ) var ProviderSet = wire.NewSet(NewDao, NewCountryClient, NewMgrClient) // Dao . type Dao struct { DB *gorm.DB Redis *redis.WrapClient Http *http.Client MgrClient mgr.MgrClient CountryClient country.CountryClient } func NewDao(c *conf.Data, countryClient country.CountryClient, mgrClient mgr.MgrClient) (*Dao, func(), error) { db := mysql.NewDB(c.Mysql) rs := redis.NewRedis(c.Redis) httpClient := httpclient.NewClient() cleanup := func() { d, _ := db.DB() d.Close() rs.Client.Close() httpClient.CloseIdleConnections() log.Debug("close Dao resources") } return &Dao{ DB: db, Redis: rs, Http: httpClient, CountryClient: countryClient, MgrClient: mgrClient, }, cleanup, nil } func NewMgrClient(conf *conf.Client, rr *consul.Registry) mgr.MgrClient { conn, err := grpc.DialInsecure( context.Background(), grpc.WithEndpoint(conf.Mgr.Endpoint), grpc.WithDiscovery(rr), grpc.WithMiddleware( recovery.Recovery(), ), grpc.WithTimeout(time.Second*2), ) if err != nil { panic(err) } return mgr.NewMgrClient(conn) } func NewCountryClient(conf *conf.Client, rr *consul.Registry) country.CountryClient { conn, err := grpc.DialInsecure( context.Background(), grpc.WithEndpoint(conf.Country.Endpoint), grpc.WithDiscovery(rr), grpc.WithMiddleware( recovery.Recovery(), ), grpc.WithTimeout(time.Second*2), ) if err != nil { panic(err) } return country.NewCountryClient(conn) }