2024-09-24 06:17:11 -04:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-11-20 06:09:04 -05:00
|
|
|
"pcgamedb/config"
|
|
|
|
"pcgamedb/log"
|
2024-11-15 02:02:45 -05:00
|
|
|
|
2024-09-24 06:17:11 -04:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-11-18 07:21:08 -05:00
|
|
|
gameItemCollectionName = "games"
|
|
|
|
gameInfoCollectionName = "game_infos"
|
2024-09-24 06:17:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-11-16 00:48:48 -05:00
|
|
|
mongoDB *mongo.Client
|
|
|
|
mutx = &sync.RWMutex{}
|
|
|
|
GameItemCollection = &CustomCollection{
|
2024-11-18 07:21:08 -05:00
|
|
|
collName: gameItemCollectionName,
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
GameInfoCollection = &CustomCollection{
|
|
|
|
collName: gameInfoCollectionName,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func connect() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
clientOptions := options.Client().ApplyURI(fmt.Sprintf(
|
|
|
|
"mongodb://%s:%s@%s:%v",
|
|
|
|
config.Config.Database.User,
|
|
|
|
config.Config.Database.Password,
|
|
|
|
config.Config.Database.Host,
|
|
|
|
config.Config.Database.Port,
|
|
|
|
))
|
|
|
|
client, err := mongo.Connect(ctx, clientOptions)
|
|
|
|
if err != nil {
|
|
|
|
log.Logger.Panic("Failed to connect to MongoDB", zap.Error(err))
|
|
|
|
}
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err = client.Ping(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Logger.Panic("Failed to ping MongoDB", zap.Error(err))
|
|
|
|
}
|
|
|
|
log.Logger.Info("Connected to MongoDB")
|
|
|
|
mongoDB = client
|
|
|
|
|
2024-11-18 07:21:08 -05:00
|
|
|
gameDownloadCollection := mongoDB.Database(config.Config.Database.Database).Collection(gameItemCollectionName)
|
2024-09-24 06:17:11 -04:00
|
|
|
gameInfoCollection := mongoDB.Database(config.Config.Database.Database).Collection(gameInfoCollectionName)
|
|
|
|
|
|
|
|
nameIndex := mongo.IndexModel{
|
|
|
|
Keys: bson.D{
|
|
|
|
{Key: "name", Value: 1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
authorIndex := mongo.IndexModel{
|
|
|
|
Keys: bson.D{
|
|
|
|
{Key: "author", Value: 1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
gamesIndex := mongo.IndexModel{
|
|
|
|
Keys: bson.D{
|
|
|
|
{Key: "games", Value: 1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
searchIndex := mongo.IndexModel{
|
|
|
|
Keys: bson.D{{Key: "name", Value: "text"}, {Key: "aliases", Value: "text"}},
|
|
|
|
}
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, err = gameDownloadCollection.Indexes().CreateOne(ctx, nameIndex)
|
|
|
|
if err != nil {
|
|
|
|
log.Logger.Error("Failed to create index", zap.Error(err))
|
|
|
|
}
|
|
|
|
_, err = gameDownloadCollection.Indexes().CreateOne(ctx, authorIndex)
|
|
|
|
if err != nil {
|
|
|
|
log.Logger.Error("Failed to create index", zap.Error(err))
|
|
|
|
}
|
|
|
|
_, err = gameInfoCollection.Indexes().CreateOne(ctx, gamesIndex)
|
|
|
|
if err != nil {
|
|
|
|
log.Logger.Error("Failed to create index", zap.Error(err))
|
|
|
|
}
|
|
|
|
_, err = gameInfoCollection.Indexes().CreateOne(ctx, searchIndex)
|
|
|
|
if err != nil {
|
|
|
|
log.Logger.Error("Failed to create index", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckConnect() {
|
|
|
|
mutx.RLock()
|
|
|
|
if mongoDB != nil {
|
|
|
|
mutx.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mutx.RUnlock()
|
|
|
|
|
|
|
|
mutx.Lock()
|
|
|
|
if mongoDB == nil {
|
|
|
|
connect()
|
|
|
|
}
|
|
|
|
mutx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func HealthCheck() error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
return mongoDB.Ping(ctx, nil)
|
|
|
|
}
|