From c5e1daba9ddfcc902b0095124ec4bd4e0fd51d66 Mon Sep 17 00:00:00 2001 From: Andy Ford Date: Tue, 17 Oct 2023 16:56:49 +0100 Subject: [PATCH 1/2] build: dev and prod envs --- .env.example => .env.dev.example | 0 .env.prod.example | 10 ++++++++++ .github/workflows/test.yml | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) rename .env.example => .env.dev.example (100%) create mode 100644 .env.prod.example diff --git a/.env.example b/.env.dev.example similarity index 100% rename from .env.example rename to .env.dev.example diff --git a/.env.prod.example b/.env.prod.example new file mode 100644 index 0000000..c2a5185 --- /dev/null +++ b/.env.prod.example @@ -0,0 +1,10 @@ +MONGO_USERNAME=root +MONGO_PASSWORD=example_password +MONGO_DB=ecfmp_test +MONGO_HOST=mongodb +MONGO_PORT=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' +LOG_LEVEL="FATAL" +DISCORD_BOT_TOKEN="abc" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 48f2669..1897e0f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -59,7 +59,7 @@ jobs: submodules: true - name: Copy env file - run: cp .env.example .env + run: cp .env.dev.example .env - name: Build Protobuf run: make protobuf From 356c4fe22a64a901a1c02f4daa8bbbbb70400c25 Mon Sep 17 00:00:00 2001 From: Andy Ford Date: Tue, 17 Oct 2023 17:28:36 +0100 Subject: [PATCH 2/2] 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