From 356c4fe22a64a901a1c02f4daa8bbbbb70400c25 Mon Sep 17 00:00:00 2001 From: Andy Ford Date: Tue, 17 Oct 2023 17:28:36 +0100 Subject: [PATCH] feat: mongodb url customisations Closes #6 --- docker-compose-ci.yml | 6 ++---- docker-compose.yml | 4 ++-- internal/db/mongo.go | 15 ++++++--------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/docker-compose-ci.yml b/docker-compose-ci.yml index 0391026..32d805e 100644 --- a/docker-compose-ci.yml +++ b/docker-compose-ci.yml @@ -20,8 +20,7 @@ services: MONGO_USERNAME: root MONGO_PASSWORD: example_password MONGO_DB: ecfmp_test - MONGO_HOST: mongodb - MONGO_PORT: 27017 + MONGO_HOST: mongodb://mongodb:27017 AUTH_JWT_PUBLIC_KEY_FILE: './docker/dev_public_key.pub' AUTH_JWT_PRIVATE_KEY_FILE: './docker/dev_private_key.pem' AUTH_JWT_AUDIENCE: 'ecfmp-discord-dev' @@ -44,8 +43,7 @@ services: MONGO_USERNAME: root MONGO_PASSWORD: example_password MONGO_DB: ecfmp_test - MONGO_HOST: mongodb - MONGO_PORT: 27017 + MONGO_HOST: mongodb://mongodb:27017 AUTH_JWT_PUBLIC_KEY_FILE: './app/docker/dev_public_key.pub' AUTH_JWT_PRIVATE_KEY_FILE: './app/docker/dev_private_key.pem' AUTH_JWT_AUDIENCE: 'ecfmp-discord-dev' diff --git a/docker-compose.yml b/docker-compose.yml index 3afb799..24a009b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,8 +13,7 @@ services: MONGO_USERNAME: root MONGO_PASSWORD: example_password MONGO_DB: ecfmp - MONGO_HOST: mongodb - MONGO_PORT: 27017 + MONGO_HOST: mongodb://mongodb:27017 AUTH_JWT_PUBLIC_KEY_FILE: "./docker/dev_public_key.pub" AUTH_JWT_AUDIENCE: "ecfmp-discord-dev" LOG_LEVEL: "TRACE" @@ -47,6 +46,7 @@ services: volumes: mongodb_data: + networks: default: name: ecfmp diff --git a/internal/db/mongo.go b/internal/db/mongo.go index c747cc5..4ee40d2 100644 --- a/internal/db/mongo.go +++ b/internal/db/mongo.go @@ -23,17 +23,14 @@ type Mongo struct { * Create a new mongo connection */ func NewMongo() (*Mongo, error) { - mongoUri := fmt.Sprintf( - "mongodb://%s:%s@%s:%s", - os.Getenv("MONGO_USERNAME"), - os.Getenv("MONGO_PASSWORD"), - os.Getenv("MONGO_HOST"), - os.Getenv("MONGO_PORT"), - ) - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoUri), options.Client().SetMaxPoolSize(10), options.Client().SetMaxConnIdleTime(5*time.Second)) + + auth := options.Credential{ + Username: os.Getenv("MONGO_USERNAME"), + Password: os.Getenv("MONGO_PASSWORD"), + } + client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_HOST")).SetAuth(auth).SetMaxPoolSize(10).SetMaxConnIdleTime(5*time.Second)) if err != nil { log.Errorf("Failed to connect to mongo: %v", err) return nil, err