Skip to content

Commit

Permalink
fix: add mongo auth connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Jul 10, 2024
1 parent b138621 commit 550c314
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 1 deletion.
10 changes: 10 additions & 0 deletions bin/init-mongo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ mongosh --eval "rs.initiate({_id: 'RS', members: [{ _id: 0, host: 'localhost:270
# Wait for replica set to initiate
sleep 5

# Create the root user
mongosh --eval "
db = db.getSiblingDB('admin');
db.createUser({
user: 'root',
pwd: 'example',
roles: [{ role: 'root', db: 'admin' }]
});
"

# Create the necessary indexes
mongosh --eval "
db = db.getSiblingDB('staking-api-service');
Expand Down
2 changes: 2 additions & 0 deletions config/config-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ server:
btc-net: "mainnet"
max-content-length: 4096
db:
username: root
password: example
address: "mongodb://mongodb:27017"
db-name: staking-api-service
max-pagination-limit: 10
Expand Down
2 changes: 2 additions & 0 deletions config/config-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ server:
btc-net: "signet"
max-content-length: 4096
db:
username: root
password: example
address: "mongodb://localhost:27017/?directConnection=true"
db-name: staking-api-service
max-pagination-limit: 10
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ services:
hostname: mongodb
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- ./bin/init-mongo.sh:/init-mongo.sh
entrypoint: [ "/init-mongo.sh" ]
Expand Down
10 changes: 10 additions & 0 deletions internal/config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const (
)

type DbConfig struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
DbName string `mapstructure:"db-name"`
Address string `mapstructure:"address"`
MaxPaginationLimit int64 `mapstructure:"max-pagination-limit"`
Expand All @@ -19,6 +21,14 @@ type DbConfig struct {
}

func (cfg *DbConfig) Validate() error {
if cfg.Username == "" {
return fmt.Errorf("missing db username")
}

if cfg.Password == "" {
return fmt.Errorf("missing db password")
}

if cfg.Address == "" {
return fmt.Errorf("missing db address")
}
Expand Down
6 changes: 5 additions & 1 deletion internal/db/dbclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ type DbResultMap[T any] struct {
}

func New(ctx context.Context, cfg config.DbConfig) (*Database, error) {
clientOps := options.Client().ApplyURI(cfg.Address)
credential := options.Credential{
Username: cfg.Username,
Password: cfg.Password,
}
clientOps := options.Client().ApplyURI(cfg.Address).SetAuth(credential)
client, err := mongo.Connect(ctx, clientOps)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions tests/config/config-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ server:
btc-net: "signet"
max-content-length: 4096
db:
username: root
password: example
address: "mongodb://localhost:27017"
db-name: staking-api-service
max-pagination-limit: 10
Expand Down

0 comments on commit 550c314

Please sign in to comment.