Skip to content

Commit

Permalink
Merge pull request #9 from ECFMP/mongodb-urls
Browse files Browse the repository at this point in the history
Mongodb urls
  • Loading branch information
AndyTWF authored Oct 17, 2023
2 parents 3e71df3 + 356c4fe commit 9355bb6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
File renamed without changes.
10 changes: 10 additions & 0 deletions .env.prod.example
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions docker-compose-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -47,6 +46,7 @@ services:
volumes:
mongodb_data:


networks:
default:
name: ecfmp
15 changes: 6 additions & 9 deletions internal/db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9355bb6

Please sign in to comment.