centrelized collection creation logic
This commit is contained in:
parent
946bd6018f
commit
ba47810398
33
dbhandler/mongo/connection.go
Normal file
33
dbhandler/mongo/connection.go
Normal file
@ -0,0 +1,33 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.acooldomain.co/server-manager/backend/models"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func getMongoCollection(config models.MongoDBConfig) (*mongo.Collection, error) {
|
||||
clientOptions := options.Client().ApplyURI(config.Url)
|
||||
|
||||
if config.Username != "" || config.Password != "" {
|
||||
clientOptions = clientOptions.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)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return client.Database(config.Database).Collection(config.Collection), nil
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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 ServerPermissions struct {
|
||||
@ -141,21 +138,12 @@ func (self *ServersAuthorizationDbHandler) GetPermissions(ctx context.Context, u
|
||||
}
|
||||
|
||||
func NewServersAuthorizationHandler(config models.MongoDBConfig) (*ServersAuthorizationDbHandler, 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 &ServersAuthorizationDbHandler{
|
||||
collection: client.Database(config.Database).Collection(config.Collection),
|
||||
collection: collection,
|
||||
}, nil
|
||||
}
|
||||
|
@ -196,21 +196,12 @@ func (self *ServersDbHandler) GetInviteToken(ctx context.Context, token string)
|
||||
}
|
||||
|
||||
func NewInviteTokenDbHandler(config models.MongoDBConfig) (*InviteTokenDbHandler, 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 &InviteTokenDbHandler{
|
||||
collection: client.Database(config.Database).Collection(config.Collection),
|
||||
collection: collection,
|
||||
}, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user