Merge pull request 'Убрал mono host port и создание урла в функции конекта. добавил тесты, нужно для удобной передачи строки для подключения к монге' (#2) from mongo into main
Reviewed-on: https://gitea.pena/PenaSide/common/pulls/2
This commit is contained in:
commit
02f71ccd7b
@ -7,8 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
Host string `env:"MONGO_HOST,default=localhost"`
|
URL string `env:"MONGO_URL,required"`
|
||||||
Port string `env:"MONGO_PORT,default=27017"`
|
|
||||||
User string `env:"MONGO_USER,required"`
|
User string `env:"MONGO_USER,required"`
|
||||||
Password string `env:"MONGO_PASSWORD,required"`
|
Password string `env:"MONGO_PASSWORD,required"`
|
||||||
Auth string `env:"MONGO_AUTH,required"`
|
Auth string `env:"MONGO_AUTH,required"`
|
||||||
|
@ -5,8 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"go.mongodb.org/mongo-driver/event"
|
"go.mongodb.org/mongo-driver/event"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/url"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
@ -23,11 +21,6 @@ func Connect(ctx context.Context, deps *ConnectDeps) (*mongo.Database, error) {
|
|||||||
return nil, ErrEmptyArgs
|
return nil, ErrEmptyArgs
|
||||||
}
|
}
|
||||||
|
|
||||||
mongoURI := &url.URL{
|
|
||||||
Scheme: "mongodb",
|
|
||||||
Host: net.JoinHostPort(deps.Configuration.Host, deps.Configuration.Port),
|
|
||||||
}
|
|
||||||
|
|
||||||
cmdMonitor := &event.CommandMonitor{
|
cmdMonitor := &event.CommandMonitor{
|
||||||
Started: func(_ context.Context, evt *event.CommandStartedEvent) {
|
Started: func(_ context.Context, evt *event.CommandStartedEvent) {
|
||||||
if evt.CommandName == "find" {
|
if evt.CommandName == "find" {
|
||||||
@ -47,7 +40,7 @@ func Connect(ctx context.Context, deps *ConnectDeps) (*mongo.Database, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
connectionOptions := options.Client().
|
connectionOptions := options.Client().
|
||||||
ApplyURI(mongoURI.String()).
|
ApplyURI(deps.Configuration.URL).
|
||||||
SetAuth(options.Credential{
|
SetAuth(options.Credential{
|
||||||
AuthMechanism: "SCRAM-SHA-1",
|
AuthMechanism: "SCRAM-SHA-1",
|
||||||
AuthSource: deps.Configuration.Auth,
|
AuthSource: deps.Configuration.Auth,
|
||||||
@ -70,12 +63,12 @@ func Connect(ctx context.Context, deps *ConnectDeps) (*mongo.Database, error) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
return connection.Database(deps.Configuration.DatabaseName), nil
|
return connection.Database(deps.Configuration.DatabaseName), nil
|
||||||
}
|
}
|
||||||
log.Printf("failed to ping the database <%s>: %s", mongoURI.String(), err.Error())
|
log.Printf("failed to ping the database <%s>: %s", deps.Configuration.URL, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("failed to connect to db <%s>: %s", mongoURI.String(), err.Error())
|
log.Printf("failed to connect to db <%s>: %s", deps.Configuration.URL, err.Error())
|
||||||
case <-timeoutExceeded:
|
case <-timeoutExceeded:
|
||||||
return nil, fmt.Errorf("db connection <%s> failed after %d timeout", mongoURI.String(), deps.Timeout)
|
return nil, fmt.Errorf("db connection <%s> failed after %d timeout", deps.Configuration.URL, deps.Timeout)
|
||||||
default:
|
default:
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
|
38
mongo/mongo_test.go
Normal file
38
mongo/mongo_test.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package mongo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConnectMongo(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.TODO(), 15*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
d, err := Connect(ctx, &ConnectDeps{
|
||||||
|
Configuration: &Configuration{
|
||||||
|
URL: "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0&readPreference=primary&ssl=false",
|
||||||
|
User: "test",
|
||||||
|
Password: "test",
|
||||||
|
Auth: "admin",
|
||||||
|
DatabaseName: "test_replicaset",
|
||||||
|
},
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
d.Drop(ctx)
|
||||||
|
|
||||||
|
d2, err := Connect(ctx, &ConnectDeps{
|
||||||
|
Configuration: &Configuration{
|
||||||
|
URL: "mongodb://localhost:27020/",
|
||||||
|
User: "test",
|
||||||
|
Password: "test",
|
||||||
|
Auth: "admin",
|
||||||
|
DatabaseName: "test_default",
|
||||||
|
},
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
d2.Drop(ctx)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user