Docker compose içerisine Mongodb eklendi ve bağlantıları yapıldı.

This commit is contained in:
2025-12-07 22:54:50 +03:00
parent 0f181833e5
commit 15611b1dc7
7 changed files with 111 additions and 1 deletions

39
server/modules/db.js Normal file
View File

@@ -0,0 +1,39 @@
import { MongoClient } from "mongodb";
const MONGO_HOST = process.env.MONGO_HOST || "mongo";
const MONGO_PORT = process.env.MONGO_PORT || "27017";
const MONGO_DB = process.env.MONGO_DB || "dupe";
const MONGO_USER = process.env.MONGO_USER || "dupe";
const MONGO_PASS = process.env.MONGO_PASS || "dupe";
const MONGO_AUTH_SOURCE = process.env.MONGO_AUTH_SOURCE || "admin";
const DEFAULT_URI = `mongodb://${encodeURIComponent(MONGO_USER)}:${encodeURIComponent(
MONGO_PASS
)}@${MONGO_HOST}:${MONGO_PORT}/${encodeURIComponent(MONGO_DB)}?authSource=${encodeURIComponent(
MONGO_AUTH_SOURCE
)}`;
const MONGO_URI = process.env.MONGO_URI || DEFAULT_URI;
let client = null;
let db = null;
export async function connectMongo() {
if (client && db) return { client, db };
const mongoClient = new MongoClient(MONGO_URI, {
maxPoolSize: 10,
serverSelectionTimeoutMS: 5000
});
await mongoClient.connect();
client = mongoClient;
db = mongoClient.db(MONGO_DB);
console.log(`📦 MongoDB bağlantısı hazır (db: ${db.databaseName})`);
return { client, db };
}
export function getDb() {
return db;
}

View File

@@ -8,6 +8,7 @@
"dependencies": {
"cors": "^2.8.5",
"express": "^4.19.2",
"mongodb": "^6.9.0",
"jsonwebtoken": "^9.0.2",
"mime-types": "^2.1.35",
"multer": "^1.4.5-lts.1",

View File

@@ -13,6 +13,7 @@ import { createAuth } from "./modules/auth.js";
import { buildHealthReport, healthRouter } from "./modules/health.js";
import { restoreTorrentsFromDisk } from "./modules/state.js";
import { createWebsocketServer, broadcastJson } from "./modules/websocket.js";
import { connectMongo, getDb } from "./modules/db.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -97,6 +98,16 @@ app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/downloads", express.static(DOWNLOAD_DIR));
// MongoDB bağlantısını başlat
connectMongo()
.then(({ db }) => {
app.locals.db = db;
app.locals.getDb = getDb;
})
.catch((err) => {
console.error("❌ MongoDB bağlantısı kurulamadı:", err.message);
});
// --- En uygun video dosyasını seç ---
function pickBestVideoFile(torrent) {
const videos = torrent.files