connect.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package database
  2. import (
  3. "fpdxfeed/config"
  4. "github.com/jinzhu/gorm"
  5. _ "github.com/jinzhu/gorm/dialects/mysql"
  6. "github.com/gomodule/redigo/redis"
  7. )
  8. type Conns struct {
  9. KdDB *gorm.DB
  10. DataDb *gorm.DB
  11. Redis redis.Conn
  12. }
  13. func NewConns(conf *config.Config) (conns *Conns, err error) {
  14. // koudai数据库
  15. user, err := conf.GetString("mysql.kd.user")
  16. if err != nil {
  17. return
  18. }
  19. password, err := conf.GetString("mysql.kd.password")
  20. if err != nil {
  21. return
  22. }
  23. host, err := conf.GetString("mysql.kd.host")
  24. if err != nil {
  25. return
  26. }
  27. port, err := conf.GetString("mysql.kd.port")
  28. if err != nil {
  29. return
  30. }
  31. database, err := conf.GetString("mysql.kd.database")
  32. if err != nil {
  33. return
  34. }
  35. kddns := user + ":" + password + "@(" + host + ":" + port + ")/" + database + "?charset=utf8&parseTime=True&loc=Asia%2FShanghai"
  36. kddb, err := gorm.Open("mysql", kddns)
  37. if err != nil {
  38. return
  39. }
  40. // datalog数据库
  41. user, err = conf.GetString("mysql.data.user")
  42. if err != nil {
  43. return
  44. }
  45. password, err = conf.GetString("mysql.data.password")
  46. if err != nil {
  47. return
  48. }
  49. host, err = conf.GetString("mysql.data.host")
  50. if err != nil {
  51. return
  52. }
  53. port, err = conf.GetString("mysql.data.port")
  54. if err != nil {
  55. return
  56. }
  57. database, err = conf.GetString("mysql.data.database")
  58. if err != nil {
  59. return
  60. }
  61. datadns := user + ":" + password + "@(" + host + ":" + port + ")/" + database + "?charset=utf8&parseTime=True&loc=Asia%2FShanghai"
  62. datadb, err := gorm.Open("mysql", datadns)
  63. if err != nil {
  64. return
  65. }
  66. // redis数据库
  67. host, err = conf.GetString("redis.host")
  68. if err != nil {
  69. return
  70. }
  71. port, err = conf.GetString("redis.port")
  72. if err != nil {
  73. return
  74. }
  75. auth, err := conf.GetString("redis.auth")
  76. if err != nil {
  77. return
  78. }
  79. rds, _ := connRedis(host, port, auth)
  80. conns = &Conns{
  81. KdDB: kddb,
  82. DataDb: datadb,
  83. Redis: rds,
  84. }
  85. return
  86. }
  87. func connRedis(host string, port string, auth string) (redis.Conn, error) {
  88. rds, err := redis.Dial("tcp", host + ":" + port)
  89. if err != nil {
  90. return nil, err
  91. }
  92. if auth != "null" {
  93. if _, err = rds.Do("AUTH", auth); err != nil {
  94. rds.Close()
  95. return nil, err
  96. }
  97. }
  98. if _, err = rds.Do("SELECT", 5); err != nil {
  99. rds.Close()
  100. return nil, err
  101. }
  102. return rds, nil
  103. }