Skip to content

Commit

Permalink
adding error handling on db connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Freeman committed Oct 19, 2024
1 parent 4dadd94 commit 8f0ea75
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"os"
"time"

"github.com/carverauto/eventrunner/pkg/api/handlers"
"github.com/carverauto/eventrunner/pkg/api/middleware"
Expand All @@ -11,15 +12,25 @@ import (
"gofr.dev/pkg/gofr/datasource/mongo"
)

const (
dbConnectTimeout = 10 * time.Second
)

func main() {
app := gofr.New()

ctx := context.Background()

// Set up MongoDB
db := mongo.New(&mongo.Config{URI: "mongodb://localhost:27017", Database: "eventrunner"})

// setup a context with a timeout
ctx, cancel := context.WithTimeout(ctx, dbConnectTimeout)
defer cancel()

err := app.AddMongo(ctx, db)
if err != nil {
app.Logger().Errorf("Failed to connect to MongoDB: %v", err)
return
}

Expand Down

0 comments on commit 8f0ea75

Please sign in to comment.