centrelized collection creation logic
This commit is contained in:
@@ -2,14 +2,11 @@ package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.acooldomain.co/server-manager/backend/dbhandler"
|
||||
"git.acooldomain.co/server-manager/backend/models"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
@@ -172,21 +169,12 @@ func (self *ServersDbHandler) UpdateServer(ctx context.Context, serverId string,
|
||||
}
|
||||
|
||||
func NewServersDbHandler(config models.MongoDBConfig) (*ServersDbHandler, error) {
|
||||
clientOptions := options.Client().ApplyURI(config.Url).SetAuth(options.Credential{
|
||||
Username: config.Username,
|
||||
Password: config.Password,
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 30*time.Second, fmt.Errorf("Timeout"))
|
||||
defer cancel()
|
||||
|
||||
client, err := mongo.Connect(ctx, clientOptions)
|
||||
|
||||
collection, err := getMongoCollection(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ServersDbHandler{
|
||||
collection: client.Database(config.Database).Collection(config.Collection),
|
||||
collection: collection,
|
||||
}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user