Update server.js
Browse files
server.js
CHANGED
|
@@ -12,6 +12,7 @@ const PORT = 7860;
|
|
| 12 |
|
| 13 |
// Browser instance management
|
| 14 |
let browser = null;
|
|
|
|
| 15 |
let isLoggedIn = false;
|
| 16 |
|
| 17 |
async function getBrowser() {
|
|
@@ -34,10 +35,22 @@ async function getBrowser() {
|
|
| 34 |
return browser;
|
| 35 |
}
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
// Login function
|
| 38 |
-
async function performLogin(
|
| 39 |
if (isLoggedIn) return true;
|
| 40 |
|
|
|
|
|
|
|
| 41 |
try {
|
| 42 |
console.log('Performing login...');
|
| 43 |
|
|
@@ -234,6 +247,9 @@ app.get("/msg/:number", async (req, res) => {
|
|
| 234 |
// Graceful shutdown
|
| 235 |
process.on('SIGINT', async () => {
|
| 236 |
console.log('Shutting down gracefully...');
|
|
|
|
|
|
|
|
|
|
| 237 |
if (browser) {
|
| 238 |
await browser.close();
|
| 239 |
}
|
|
@@ -242,6 +258,9 @@ process.on('SIGINT', async () => {
|
|
| 242 |
|
| 243 |
process.on('SIGTERM', async () => {
|
| 244 |
console.log('Shutting down gracefully...');
|
|
|
|
|
|
|
|
|
|
| 245 |
if (browser) {
|
| 246 |
await browser.close();
|
| 247 |
}
|
|
|
|
| 12 |
|
| 13 |
// Browser instance management
|
| 14 |
let browser = null;
|
| 15 |
+
let authenticatedPage = null;
|
| 16 |
let isLoggedIn = false;
|
| 17 |
|
| 18 |
async function getBrowser() {
|
|
|
|
| 35 |
return browser;
|
| 36 |
}
|
| 37 |
|
| 38 |
+
// Get or create authenticated page
|
| 39 |
+
async function getAuthenticatedPage() {
|
| 40 |
+
if (!authenticatedPage || authenticatedPage.isClosed()) {
|
| 41 |
+
const browser = await getBrowser();
|
| 42 |
+
authenticatedPage = await setupPage(browser);
|
| 43 |
+
isLoggedIn = false; // Reset login status for new page
|
| 44 |
+
}
|
| 45 |
+
return authenticatedPage;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
// Login function
|
| 49 |
+
async function performLogin() {
|
| 50 |
if (isLoggedIn) return true;
|
| 51 |
|
| 52 |
+
const page = await getAuthenticatedPage();
|
| 53 |
+
|
| 54 |
try {
|
| 55 |
console.log('Performing login...');
|
| 56 |
|
|
|
|
| 247 |
// Graceful shutdown
|
| 248 |
process.on('SIGINT', async () => {
|
| 249 |
console.log('Shutting down gracefully...');
|
| 250 |
+
if (authenticatedPage && !authenticatedPage.isClosed()) {
|
| 251 |
+
await authenticatedPage.close();
|
| 252 |
+
}
|
| 253 |
if (browser) {
|
| 254 |
await browser.close();
|
| 255 |
}
|
|
|
|
| 258 |
|
| 259 |
process.on('SIGTERM', async () => {
|
| 260 |
console.log('Shutting down gracefully...');
|
| 261 |
+
if (authenticatedPage && !authenticatedPage.isClosed()) {
|
| 262 |
+
await authenticatedPage.close();
|
| 263 |
+
}
|
| 264 |
if (browser) {
|
| 265 |
await browser.close();
|
| 266 |
}
|