Skip to content

Commit

Permalink
extracted the db-intalisation to a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 13, 2023
1 parent cdc891b commit 2b31893
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,7 @@ func main() {
log.WithError(err).Error("InfluxDB connection failed - health check failed")
}

// Connect to DB
var conn gorm.Dialector
if dbHost := os.Getenv("DB_DSN"); dbHost == "" {
log.Fatal("Failed to start! The 'DB_DSN' environment variable is not defined. Take a look at the README.md for more details.")
} else {
log.Info("Connecting to dsn")
conn = mysql.Open(dbHost)
}

db, err := gorm.Open(conn, &gorm.Config{})
if err != nil {
log.WithError(err).Panic("failed to connect database")
}

// Migrate the schema
// currently not activated as
if err := migration.New(db, false).Migrate(); err != nil {
log.WithError(err).Fatal("Failed to migrate database")
}
db := setupDB()

// Create any other background services (these shouldn't do any long-running work here)
cronService := cron.New(db)
Expand Down Expand Up @@ -142,6 +124,30 @@ func main() {
}
}

// setupDB connects to the database and migrates it if necessary
func setupDB() *gorm.DB {
// Connect to DB
var conn gorm.Dialector
if dbHost := os.Getenv("DB_DSN"); dbHost == "" {
log.Fatal("Failed to start! The 'DB_DSN' environment variable is not defined. Take a look at the README.md for more details.")
} else {
log.Info("Connecting to dsn")
conn = mysql.Open(dbHost)
}

db, err := gorm.Open(conn, &gorm.Config{})
if err != nil {
log.WithError(err).Panic("failed to connect database")
}

// Migrate the schema
// currently not activated as
if err := migration.New(db, false).Migrate(); err != nil {
log.WithError(err).Fatal("Failed to migrate database")
}
return db
}

// setupTelemetry initializes our telemetry stack
// - sentry to be connected with log
// - logrus to
Expand Down

0 comments on commit 2b31893

Please sign in to comment.