25 lines
476 B
Go
25 lines
476 B
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
|
|
|
"git.urec56.ru/urec/chat_back_go/config"
|
|
)
|
|
|
|
func NewMongo(cfg config.Mongo) (*mongo.Client, error) {
|
|
opt := options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%d", cfg.Host, cfg.Port))
|
|
|
|
client, err := mongo.Connect(opt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = client.Ping(nil, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return client, nil
|
|
}
|