From ba4781039826a837c6965a6f4168d6193f5d5722 Mon Sep 17 00:00:00 2001 From: ACoolName Date: Wed, 19 Mar 2025 13:27:43 +0200 Subject: [PATCH] centrelized collection creation logic --- dbhandler/mongo/connection.go | 33 +++++++++++++++++++++ dbhandler/mongo/servers.go | 16 ++-------- dbhandler/mongo/servers_authorization.go | 16 ++-------- dbhandler/mongo/user_pass_authentication.go | 13 ++------ 4 files changed, 39 insertions(+), 39 deletions(-) create mode 100644 dbhandler/mongo/connection.go diff --git a/dbhandler/mongo/connection.go b/dbhandler/mongo/connection.go new file mode 100644 index 0000000..163d39b --- /dev/null +++ b/dbhandler/mongo/connection.go @@ -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 +} diff --git a/dbhandler/mongo/servers.go b/dbhandler/mongo/servers.go index 0b40a4c..0ea7ca1 100644 --- a/dbhandler/mongo/servers.go +++ b/dbhandler/mongo/servers.go @@ -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 } diff --git a/dbhandler/mongo/servers_authorization.go b/dbhandler/mongo/servers_authorization.go index 7d78b68..5bea22d 100644 --- a/dbhandler/mongo/servers_authorization.go +++ b/dbhandler/mongo/servers_authorization.go @@ -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 } diff --git a/dbhandler/mongo/user_pass_authentication.go b/dbhandler/mongo/user_pass_authentication.go index 30fce0c..104b184 100644 --- a/dbhandler/mongo/user_pass_authentication.go +++ b/dbhandler/mongo/user_pass_authentication.go @@ -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 }