add ferretdb option (#11 and #17)

This commit is contained in:
pluja
2023-09-02 20:09:30 +02:00
parent 7de10e2388
commit d2e2982e67
2 changed files with 124 additions and 1 deletions

View File

@@ -23,7 +23,20 @@ type MongoDb struct {
func NewMongoDb() *MongoDb {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(fmt.Sprintf("mongodb://root:example@%v", os.Getenv("DB_ENDPOINT"))))
var client *mongo.Client
var err error
mongoUri := fmt.Sprintf("mongodb://%v", os.Getenv("DB_ENDPOINT"))
credentials := options.Credential{
Username: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASS"),
}
// Set auth method to PLAIN if using FerretDB
if os.Getenv("FERRETDB_MONGO_URL") != "" {
credentials.AuthMechanism = "PLAIN"
}
client, err = mongo.Connect(ctx, options.Client().ApplyURI(mongoUri).SetAuth(credentials))
if err != nil {
log.Error().Err(err).Msg("Error connecting to mongodb")
}