|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "go.mongodb.org/mongo-driver/mongo" |
| 11 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 12 | + "go.mongodb.org/mongo-driver/mongo/readpref" |
| 13 | +) |
| 14 | + |
| 15 | +var dbInstance *Database |
| 16 | + |
| 17 | +type Database struct { |
| 18 | + MongoClient *mongo.Client |
| 19 | + Config *DatabaseConfig |
| 20 | +} |
| 21 | + |
| 22 | +type DatabaseConfig struct { |
| 23 | + MongoDatabaseName string |
| 24 | +} |
| 25 | + |
| 26 | +func defaultDatabaseConfig() *DatabaseConfig { |
| 27 | + return &DatabaseConfig{ |
| 28 | + MongoDatabaseName: "core", |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func Init() error { |
| 33 | + dbInstance = &Database{ |
| 34 | + Config: defaultDatabaseConfig(), |
| 35 | + } |
| 36 | + |
| 37 | + mongoClient, err := initMongoClient() |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + dbInstance.MongoClient = mongoClient |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +/* |
| 47 | + Returns default mongo database. |
| 48 | +*/ |
| 49 | +func Get() (*mongo.Database, error) { |
| 50 | + |
| 51 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 52 | + defer cancel() |
| 53 | + err := dbInstance.MongoClient.Ping(ctx, readpref.Primary()) |
| 54 | + |
| 55 | + if err == nil { |
| 56 | + client := dbInstance.MongoClient |
| 57 | + config := dbInstance.Config |
| 58 | + return client.Database(config.MongoDatabaseName), nil |
| 59 | + } |
| 60 | + |
| 61 | + mongoClient, err := initMongoClient() |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + dbInstance.MongoClient = mongoClient |
| 67 | + |
| 68 | + client := dbInstance.MongoClient |
| 69 | + config := dbInstance.Config |
| 70 | + return client.Database(config.MongoDatabaseName), nil |
| 71 | +} |
| 72 | + |
| 73 | +/* |
| 74 | + Initializes the mongoDB Client to access databases and collections. |
| 75 | +*/ |
| 76 | +func initMongoClient() (*mongo.Client, error) { |
| 77 | + |
| 78 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 79 | + defer cancel() |
| 80 | + client, err := mongo.Connect(ctx, options.Client().ApplyURI(getMongoDBConfig())) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + err = client.Ping(ctx, readpref.Primary()) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + log.Println("Initialized mongo client!") |
| 91 | + return client, err |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +/* |
| 96 | + Returns configurated URI string for MongoDB. |
| 97 | +*/ |
| 98 | +func getMongoDBConfig() (uri string) { |
| 99 | + dbHost := os.Getenv("MONGO_HOST") |
| 100 | + dbName := os.Getenv("MONGO_NAME") |
| 101 | + dbUser := os.Getenv("MONGO_USER") |
| 102 | + dbPass := os.Getenv("MONGO_PASS") |
| 103 | + |
| 104 | + return fmt.Sprintf(`mongodb://%s:%s@%s/%s`, dbUser, dbPass, dbHost, dbName) |
| 105 | +} |
0 commit comments