postgres.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package db
  2. import (
  3. "fmt"
  4. "github.com/gravitl/netmaker/servercfg"
  5. "os"
  6. "strconv"
  7. "github.com/gravitl/netmaker/config"
  8. "gorm.io/driver/postgres"
  9. "gorm.io/gorm"
  10. "gorm.io/gorm/logger"
  11. )
  12. // postgresConnector for initializing and
  13. // connecting to a postgres database.
  14. type postgresConnector struct{}
  15. // postgresConnector.connect connects and
  16. // initializes a connection to postgres.
  17. func (pg *postgresConnector) connect() (*gorm.DB, error) {
  18. pgConf := servercfg.GetSQLConf()
  19. dsn := fmt.Sprintf(
  20. "host=%s port=%d user=%s password=%s dbname=%s sslmode=%s connect_timeout=5",
  21. pgConf.Host,
  22. pgConf.Port,
  23. pgConf.Username,
  24. pgConf.Password,
  25. pgConf.DB,
  26. pgConf.SSLMode,
  27. )
  28. return gorm.Open(postgres.Open(dsn), &gorm.Config{
  29. Logger: logger.Default.LogMode(logger.Silent),
  30. })
  31. }
  32. func GetSQLConf() config.SQLConfig {
  33. var cfg config.SQLConfig
  34. cfg.Host = GetSQLHost()
  35. cfg.Port = GetSQLPort()
  36. cfg.Username = GetSQLUser()
  37. cfg.Password = GetSQLPass()
  38. cfg.DB = GetSQLDB()
  39. cfg.SSLMode = GetSQLSSLMode()
  40. return cfg
  41. }
  42. func GetSQLHost() string {
  43. host := "localhost"
  44. if os.Getenv("SQL_HOST") != "" {
  45. host = os.Getenv("SQL_HOST")
  46. } else if config.Config.SQL.Host != "" {
  47. host = config.Config.SQL.Host
  48. }
  49. return host
  50. }
  51. func GetSQLPort() int32 {
  52. port := int32(5432)
  53. envport, err := strconv.Atoi(os.Getenv("SQL_PORT"))
  54. if err == nil && envport != 0 {
  55. port = int32(envport)
  56. } else if config.Config.SQL.Port != 0 {
  57. port = config.Config.SQL.Port
  58. }
  59. return port
  60. }
  61. func GetSQLUser() string {
  62. user := "postgres"
  63. if os.Getenv("SQL_USER") != "" {
  64. user = os.Getenv("SQL_USER")
  65. } else if config.Config.SQL.Username != "" {
  66. user = config.Config.SQL.Username
  67. }
  68. return user
  69. }
  70. func GetSQLPass() string {
  71. pass := "nopass"
  72. if os.Getenv("SQL_PASS") != "" {
  73. pass = os.Getenv("SQL_PASS")
  74. } else if config.Config.SQL.Password != "" {
  75. pass = config.Config.SQL.Password
  76. }
  77. return pass
  78. }
  79. func GetSQLDB() string {
  80. db := "netmaker"
  81. if os.Getenv("SQL_DB") != "" {
  82. db = os.Getenv("SQL_DB")
  83. } else if config.Config.SQL.DB != "" {
  84. db = config.Config.SQL.DB
  85. }
  86. return db
  87. }
  88. func GetSQLSSLMode() string {
  89. sslmode := "disable"
  90. if os.Getenv("SQL_SSL_MODE") != "" {
  91. sslmode = os.Getenv("SQL_SSL_MODE")
  92. } else if config.Config.SQL.SSLMode != "" {
  93. sslmode = config.Config.SQL.SSLMode
  94. }
  95. return sslmode
  96. }