dao.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/service/live/gift/conf"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/net/rpc/liverpc"
  9. activity "go-common/app/service/live/activity/api/liverpc"
  10. fans_medal "go-common/app/service/live/fans_medal/api/liverpc"
  11. live_user "go-common/app/service/live/live_user/api/liverpc"
  12. room "go-common/app/service/live/room/api/liverpc"
  13. user "go-common/app/service/live/user/api/liverpc"
  14. xuser "go-common/app/service/live/xuser/api/grpc/v1"
  15. )
  16. // Dao dao
  17. type Dao struct {
  18. c *conf.Config
  19. mc *memcache.Pool
  20. redis *redis.Pool
  21. db *xsql.DB
  22. }
  23. // New init mysql db
  24. func New(c *conf.Config) (dao *Dao) {
  25. dao = &Dao{
  26. c: c,
  27. mc: memcache.NewPool(c.Memcache),
  28. redis: redis.NewPool(c.Redis),
  29. db: xsql.NewMySQL(c.MySQL),
  30. }
  31. return
  32. }
  33. // Close close the resource.
  34. func (d *Dao) Close() {
  35. d.mc.Close()
  36. d.redis.Close()
  37. d.db.Close()
  38. }
  39. // Ping dao ping
  40. func (d *Dao) Ping(ctx context.Context) error {
  41. // TODO: add mc,redis... if you use
  42. return d.db.Ping(ctx)
  43. }
  44. var (
  45. // RoomApi RoomApi
  46. RoomApi *room.Client
  47. // LiveUserApi LiveUserApi
  48. LiveUserApi *live_user.Client
  49. // UserApi UserApi
  50. UserApi *user.Client
  51. // FansMedalApi FansMedalApi
  52. FansMedalApi *fans_medal.Client
  53. // ActivityApi ActivityApi
  54. ActivityApi *activity.Client
  55. // XuserClient XuserClient
  56. XuserClient *xuser.Client
  57. )
  58. //InitApi InitApi
  59. func InitApi() {
  60. RoomApi = room.New(getConf("room"))
  61. LiveUserApi = live_user.New(getConf("live_user"))
  62. UserApi = user.New(getConf("user"))
  63. FansMedalApi = fans_medal.New(getConf("fans_medal"))
  64. ActivityApi = activity.New(getConf("activity"))
  65. var err error
  66. XuserClient, err = xuser.NewClient(nil)
  67. if err != nil {
  68. panic(err)
  69. }
  70. }
  71. func getConf(appName string) *liverpc.ClientConfig {
  72. c := conf.Conf.LiveRpc
  73. if c != nil {
  74. return c[appName]
  75. }
  76. return nil
  77. }