Skip to content

Commit

Permalink
build: update build mongo URI logic. (#152)
Browse files Browse the repository at this point in the history
* build: implement kafka health check.

* build: update build mongo URI logic.
  • Loading branch information
mo3et authored Nov 15, 2024
1 parent ca13221 commit 7099015
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
8 changes: 4 additions & 4 deletions db/mongoutil/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ package mongoutil
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"strings"

"go.mongodb.org/mongo-driver/mongo"
)

const (
defaultMaxPoolSize = 100
defaultMaxRetry = 3
)

func buildMongoURI(config *Config) string {
func buildMongoURI(config *Config, authSource string) string {
credentials := ""
authSource := "admin"

if config.Username != "" && config.Password != "" {
credentials = fmt.Sprintf("%s:%s", config.Username, config.Password)
}

// 构建 MongoDB URI
return fmt.Sprintf(
"mongodb://%s@%s/%s?authSource=%s&maxPoolSize=%d",
credentials,
Expand Down
1 change: 1 addition & 0 deletions db/mongoutil/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {
Database string
Username string
Password string
AuthSource string
MaxPoolSize int
MaxRetry int
}
Expand Down
7 changes: 6 additions & 1 deletion db/mongoutil/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ func (c *Config) ValidateAndSetDefaults() error {
c.MaxRetry = defaultMaxRetry
}
if c.Uri == "" {
c.Uri = buildMongoURI(c)
// if authSource is not provided, default to admin
if c.AuthSource == "" {
c.Uri = buildMongoURI(c, "admin")
} else {
c.Uri = buildMongoURI(c, c.AuthSource)
}
}
return nil
}

0 comments on commit 7099015

Please sign in to comment.