Skip to content

Commit

Permalink
feat: mongodb url customisations
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
AndyTWF committed Oct 17, 2023
1 parent c5e1dab commit 356c4fe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
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 356c4fe

Please sign in to comment.