Spaces:
Paused
Paused
Update app.js
Browse files
app.js
CHANGED
|
@@ -5,11 +5,9 @@ import os from 'os';
|
|
| 5 |
|
| 6 |
const totalCpus = os.cpus().length;
|
| 7 |
|
| 8 |
-
// --- মাল্টি-কোর ক্লাস্টার সেটআপ ---
|
| 9 |
if (cluster.isPrimary) {
|
| 10 |
console.log(`Primary Master Process ${process.pid} is running`);
|
| 11 |
|
| 12 |
-
// প্রতিটি CPU কোরের জন্য একটি করে ওয়ার্কার প্রসেস তৈরি হবে
|
| 13 |
for (let i = 0; i < totalCpus; i++) {
|
| 14 |
cluster.fork();
|
| 15 |
}
|
|
@@ -20,14 +18,13 @@ if (cluster.isPrimary) {
|
|
| 20 |
});
|
| 21 |
|
| 22 |
} else {
|
| 23 |
-
// ওয়ার্কার প্রসেস - যা মূল API হ্যান্ডেল করবে
|
| 24 |
const app = express();
|
| 25 |
app.use(express.json());
|
| 26 |
|
| 27 |
const PORT = process.env.PORT || 7860;
|
| 28 |
const BOT_TOKEN = process.env.BOT_TOKEN;
|
| 29 |
|
| 30 |
-
// --- ফায়ারবেস
|
| 31 |
let app1, app2;
|
| 32 |
try {
|
| 33 |
const firebaseAdmin1 = JSON.parse(process.env.FIREBASE_ADMIN_1 || '{}');
|
|
@@ -48,19 +45,35 @@ if (cluster.isPrimary) {
|
|
| 48 |
console.error('Firebase initialization error:', error.message);
|
| 49 |
}
|
| 50 |
|
| 51 |
-
// ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
function verifyIntegrity(intdata, uid, fullName) {
|
| 53 |
if (!intdata || !uid || !fullName) return false;
|
| 54 |
-
|
| 55 |
-
// আপনার আইডিয়া অনুযায়ী BOT_TOKEN এবং intdata ম্যাচিং বা ডিক্রিপশন লজিক এখানে হবে
|
| 56 |
-
// সাময়িকভাবে ভ্যালিডেশন পাস করানোর জন্য true দেওয়া হলো
|
| 57 |
return true;
|
| 58 |
}
|
| 59 |
|
| 60 |
// --- এপিআই এন্ডপয়েন্ট সমূহ ---
|
| 61 |
|
| 62 |
-
// ১. /health এন্ডপয়েন্ট
|
| 63 |
app.all('/health', (req, res) => {
|
|
|
|
| 64 |
res.status(200).json({ status: "OK", message: "Server is healthy" });
|
| 65 |
});
|
| 66 |
|
|
@@ -68,27 +81,36 @@ if (cluster.isPrimary) {
|
|
| 68 |
app.post('/profile', async (req, res) => {
|
| 69 |
const { intdata, uid, full_name } = req.body;
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
if (!verifyIntegrity(intdata, uid, full_name)) {
|
|
|
|
| 73 |
return res.status(400).json({ error: "Bad Request: Integrity check failed" });
|
| 74 |
}
|
| 75 |
|
| 76 |
let responseData = {};
|
| 77 |
|
| 78 |
try {
|
| 79 |
-
//
|
|
|
|
| 80 |
const db1 = admin.database(app1);
|
| 81 |
const snapshot1 = await db1.ref(`database/withdraw/${uid}`).once('value');
|
| 82 |
|
| 83 |
if (snapshot1.exists()) {
|
| 84 |
responseData.withdraw = snapshot1.val();
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
|
| 87 |
-
//
|
|
|
|
| 88 |
const db2 = admin.database(app2);
|
| 89 |
const snapshot2 = await db2.ref(`user/${uid}`).once('value');
|
| 90 |
|
| 91 |
responseData.user_info = snapshot2.exists() ? snapshot2.val() : {};
|
|
|
|
| 92 |
|
| 93 |
// ফাইনাল রেসপন্স পাঠানো
|
| 94 |
return res.status(200).json({
|
|
@@ -97,6 +119,7 @@ if (cluster.isPrimary) {
|
|
| 97 |
});
|
| 98 |
|
| 99 |
} catch (error) {
|
|
|
|
| 100 |
return res.status(500).json({
|
| 101 |
error: "Internal Server Error",
|
| 102 |
details: error.message
|
|
|
|
| 5 |
|
| 6 |
const totalCpus = os.cpus().length;
|
| 7 |
|
|
|
|
| 8 |
if (cluster.isPrimary) {
|
| 9 |
console.log(`Primary Master Process ${process.pid} is running`);
|
| 10 |
|
|
|
|
| 11 |
for (let i = 0; i < totalCpus; i++) {
|
| 12 |
cluster.fork();
|
| 13 |
}
|
|
|
|
| 18 |
});
|
| 19 |
|
| 20 |
} else {
|
|
|
|
| 21 |
const app = express();
|
| 22 |
app.use(express.json());
|
| 23 |
|
| 24 |
const PORT = process.env.PORT || 7860;
|
| 25 |
const BOT_TOKEN = process.env.BOT_TOKEN;
|
| 26 |
|
| 27 |
+
// --- ফায়ারবেস ইনিশিয়ালাইজেশন ---
|
| 28 |
let app1, app2;
|
| 29 |
try {
|
| 30 |
const firebaseAdmin1 = JSON.parse(process.env.FIREBASE_ADMIN_1 || '{}');
|
|
|
|
| 45 |
console.error('Firebase initialization error:', error.message);
|
| 46 |
}
|
| 47 |
|
| 48 |
+
// --- কাস্টম রিকোয়েস্ট-রেসপন্স লগার মিডলওয়্যার ---
|
| 49 |
+
app.use((req, res, next) => {
|
| 50 |
+
const startTime = Date.now();
|
| 51 |
+
|
| 52 |
+
// রেসপন্স শেষ হলে এই ফাংশনটি রান করবে
|
| 53 |
+
res.on('finish', () => {
|
| 54 |
+
const duration = Date.now() - startTime;
|
| 55 |
+
console.log(`\n=================== REQUEST LOG [Worker: ${process.pid}] ===================`);
|
| 56 |
+
console.log(`[${new Date().toISOString()}] ${req.method} ${req.originalUrl}`);
|
| 57 |
+
console.log(`➜ Incoming Body:`, JSON.stringify(req.body || {}));
|
| 58 |
+
console.log(`➜ HTTP Status: ${res.statusCode}`);
|
| 59 |
+
console.log(`➜ Execution Time: ${duration}ms`);
|
| 60 |
+
console.log(`=================================================================\n`);
|
| 61 |
+
});
|
| 62 |
+
|
| 63 |
+
next();
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
// --- ডাটা ভ্যালিডেশন ফাংশন ---
|
| 67 |
function verifyIntegrity(intdata, uid, fullName) {
|
| 68 |
if (!intdata || !uid || !fullName) return false;
|
|
|
|
|
|
|
|
|
|
| 69 |
return true;
|
| 70 |
}
|
| 71 |
|
| 72 |
// --- এপিআই এন্ডপয়েন্ট সমূহ ---
|
| 73 |
|
| 74 |
+
// ১. /health এন্ডপয়েন্ট
|
| 75 |
app.all('/health', (req, res) => {
|
| 76 |
+
console.log(`[Worker ${process.pid}] Processing /health request...`);
|
| 77 |
res.status(200).json({ status: "OK", message: "Server is healthy" });
|
| 78 |
});
|
| 79 |
|
|
|
|
| 81 |
app.post('/profile', async (req, res) => {
|
| 82 |
const { intdata, uid, full_name } = req.body;
|
| 83 |
|
| 84 |
+
console.log(`[Worker ${process.pid}] Processing /profile for UID: ${uid}`);
|
| 85 |
+
|
| 86 |
+
// ভ্যালিডেশন চেক
|
| 87 |
if (!verifyIntegrity(intdata, uid, full_name)) {
|
| 88 |
+
console.log(`[Worker ${process.pid}] Validation FAILED for UID: ${uid}`);
|
| 89 |
return res.status(400).json({ error: "Bad Request: Integrity check failed" });
|
| 90 |
}
|
| 91 |
|
| 92 |
let responseData = {};
|
| 93 |
|
| 94 |
try {
|
| 95 |
+
// কাজের ধাপ ১: DB 1 থেকে উইথড্র ডাটা চেক
|
| 96 |
+
console.log(`[Worker ${process.pid}] Fetching withdraw data from DB_1 for UID: ${uid}`);
|
| 97 |
const db1 = admin.database(app1);
|
| 98 |
const snapshot1 = await db1.ref(`database/withdraw/${uid}`).once('value');
|
| 99 |
|
| 100 |
if (snapshot1.exists()) {
|
| 101 |
responseData.withdraw = snapshot1.val();
|
| 102 |
+
console.log(`[Worker ${process.pid}] Withdraw data FOUND for UID: ${uid}`);
|
| 103 |
+
} else {
|
| 104 |
+
console.log(`[Worker ${process.pid}] No withdraw data found for UID: ${uid}`);
|
| 105 |
}
|
| 106 |
|
| 107 |
+
// কাজের ধাপ ২: DB 2 থেকে ইউজার ডাটা চেক
|
| 108 |
+
console.log(`[Worker ${process.pid}] Fetching user info from DB_2 for UID: ${uid}`);
|
| 109 |
const db2 = admin.database(app2);
|
| 110 |
const snapshot2 = await db2.ref(`user/${uid}`).once('value');
|
| 111 |
|
| 112 |
responseData.user_info = snapshot2.exists() ? snapshot2.val() : {};
|
| 113 |
+
console.log(`[Worker ${process.pid}] User info processed successfully for UID: ${uid}`);
|
| 114 |
|
| 115 |
// ফাইনাল রেসপন্স পাঠানো
|
| 116 |
return res.status(200).json({
|
|
|
|
| 119 |
});
|
| 120 |
|
| 121 |
} catch (error) {
|
| 122 |
+
console.error(`[Worker ${process.pid}] Error processing profile:`, error.message);
|
| 123 |
return res.status(500).json({
|
| 124 |
error: "Internal Server Error",
|
| 125 |
details: error.message
|