You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
703 B

  1. package Database
  2. import (
  3. "fmt"
  4. "gorm.io/driver/postgres"
  5. "gorm.io/gorm"
  6. "PersonalWebsite/Variables"
  7. )
  8. var (
  9. DB *gorm.DB
  10. )
  11. func InitDatabaseConn() error {
  12. var (
  13. dbConnString string
  14. e error
  15. )
  16. // Connect to the postgres db
  17. // you might have to change the connection string to add your database credentials
  18. dbConnString = fmt.Sprintf(
  19. "host=%s port=%d dbname=%s user=%s password=%s %s",
  20. Variables.DbHost,
  21. Variables.DbPort,
  22. Variables.DbName,
  23. Variables.DbUser,
  24. Variables.DbPass,
  25. Variables.DbOpts,
  26. )
  27. DB, e = gorm.Open(
  28. postgres.Open(dbConnString),
  29. &gorm.Config{},
  30. )
  31. if e != nil {
  32. return e
  33. }
  34. e = MigrateDB()
  35. if e != nil {
  36. return e
  37. }
  38. return nil
  39. }