sqlconf.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package servercfg
  2. import (
  3. "github.com/gravitl/netmaker/config"
  4. "os"
  5. "strconv"
  6. )
  7. func GetSQLConf() config.SQLConfig {
  8. var cfg config.SQLConfig
  9. cfg.Host = GetSQLHost()
  10. cfg.Port = GetSQLPort()
  11. cfg.Username = GetSQLUser()
  12. cfg.Password = GetSQLPass()
  13. cfg.DB = GetSQLDB()
  14. cfg.SSLMode = GetSQLSSLMode()
  15. return cfg
  16. }
  17. func GetSQLHost() string {
  18. host := "localhost"
  19. if os.Getenv("SQL_HOST") != "" {
  20. host = os.Getenv("SQL_HOST")
  21. } else if config.Config.SQL.Host != "" {
  22. host = config.Config.SQL.Host
  23. }
  24. return host
  25. }
  26. func GetSQLPort() int32 {
  27. port := int32(5432)
  28. envport, err := strconv.Atoi(os.Getenv("SQL_PORT"))
  29. if err == nil && envport != 0 {
  30. port = int32(envport)
  31. } else if config.Config.SQL.Port != 0 {
  32. port = config.Config.SQL.Port
  33. }
  34. return port
  35. }
  36. func GetSQLUser() string {
  37. user := "posgres"
  38. if os.Getenv("SQL_USER") != "" {
  39. user = os.Getenv("SQL_USER")
  40. } else if config.Config.SQL.Username != "" {
  41. user = config.Config.SQL.Username
  42. }
  43. return user
  44. }
  45. func GetSQLPass() string {
  46. pass := "nopass"
  47. if os.Getenv("SQL_PASS") != "" {
  48. pass = os.Getenv("SQL_PASS")
  49. } else if config.Config.SQL.Password != "" {
  50. pass = config.Config.SQL.Password
  51. }
  52. return pass
  53. }
  54. func GetSQLDB() string {
  55. db := "netmaker"
  56. if os.Getenv("SQL_DB") != "" {
  57. db = os.Getenv("SQL_DB")
  58. } else if config.Config.SQL.DB != "" {
  59. db = config.Config.SQL.DB
  60. }
  61. return db
  62. }
  63. func GetSQLSSLMode() string {
  64. sslmode := "disable"
  65. if os.Getenv("SQL_SSL_MODE") != "" {
  66. sslmode = os.Getenv("SQL_SSL_MODE")
  67. } else if config.Config.SQL.SSLMode != "" {
  68. sslmode = config.Config.SQL.SSLMode
  69. }
  70. return sslmode
  71. }