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.

46 lines
628 B

  1. package database
  2. import (
  3. "fmt"
  4. "gorm.io/driver/postgres"
  5. "gorm.io/gorm"
  6. "git.tovijaeschke.xyz/tovi/personal_website/variables"
  7. )
  8. var (
  9. DB *gorm.DB
  10. )
  11. func InitDatabaseConn() error {
  12. var (
  13. dbConnString string
  14. err error
  15. )
  16. dbConnString = fmt.Sprintf(
  17. "host=%s port=%d dbname=%s user=%s password=%s %s",
  18. variables.DbHost,
  19. variables.DbPort,
  20. variables.DbName,
  21. variables.DbUser,
  22. variables.DbPass,
  23. variables.DbOpts,
  24. )
  25. DB, err = gorm.Open(
  26. postgres.Open(dbConnString),
  27. &gorm.Config{},
  28. )
  29. if err != nil {
  30. return err
  31. }
  32. err = MigrateDB()
  33. if err != nil {
  34. return err
  35. }
  36. return nil
  37. }