mirror of
https://github.com/HexCardGames/HexDeck.git
synced 2025-09-03 18:48:38 +02:00
fix(backend): make sure database connection was successful before continuing
This commit is contained in:
@ -70,13 +70,22 @@ func (conn *DatabaseConnection) QueryGlobalStats() GlobalStatsCollection {
|
||||
return stats
|
||||
}
|
||||
|
||||
func CreateDBConnection(uri string) DatabaseConnection {
|
||||
client, _ := mongo.Connect(options.Client().ApplyURI(uri))
|
||||
return DatabaseConnection{client}
|
||||
func CreateDBConnection(uri string) *DatabaseConnection {
|
||||
client, err := mongo.Connect(options.Client().ApplyURI(uri))
|
||||
if err != nil {
|
||||
slog.Error("MongoDB connection failed", "error", err)
|
||||
return nil
|
||||
}
|
||||
return &DatabaseConnection{client}
|
||||
}
|
||||
|
||||
var Conn DatabaseConnection
|
||||
|
||||
func InitDB(uri string) {
|
||||
Conn = CreateDBConnection(uri)
|
||||
func InitDB(uri string) bool {
|
||||
dbConn := CreateDBConnection(uri)
|
||||
if dbConn == nil {
|
||||
return false
|
||||
}
|
||||
Conn = *dbConn
|
||||
return true
|
||||
}
|
||||
|
@ -22,7 +22,11 @@ func main() {
|
||||
slog.Error("MONGO_URI environment variable not set!")
|
||||
return
|
||||
}
|
||||
db.InitDB(mongoUri)
|
||||
ok := db.InitDB(mongoUri)
|
||||
if !ok {
|
||||
slog.Error("Initializing MongoDB database failed")
|
||||
return
|
||||
}
|
||||
game.LoadRooms()
|
||||
|
||||
roomTicker := time.NewTicker(1 * time.Second)
|
||||
|
Reference in New Issue
Block a user