Files
projects/Dockers/playwright-node-api/index.js
T
Bram c8f41768f7
Build and Push Docker Images / build-and-push (push) Successful in 3m49s
basic playwright api
2025-11-20 14:46:07 +01:00

60 lines
1.8 KiB
JavaScript

const express = require('express');
const dbOps = require('./database');
const { closeBrowser } = require('./browser');
const routes = require('./routes');
const config = require('./config');
const app = express();
// Use routes
app.use('/', routes);
// Cleanup expired cache entries
async function cleanupExpiredCache() {
const now = Date.now();
const deleted = await dbOps.deleteExpired(now);
if (deleted > 0) {
console.log(`Cleaned up ${deleted} expired cache entries`);
}
}
// Initialize database and start server
(async () => {
try {
await dbOps.init();
console.log(`Database initialized: ${dbOps.getDbType()}`);
// Periodic cache cleanup (every hour)
setInterval(cleanupExpiredCache, 60 * 60 * 1000);
// Initial cleanup on startup
await cleanupExpiredCache();
// Start server
app.listen(config.PORT, '0.0.0.0', () => {
console.log(`Playwright API server listening on port ${config.PORT}`);
console.log(`Cache expiry: ${config.CACHE_EXPIRY_HOURS} hours`);
console.log(`Database: ${dbOps.getDbType()}${dbOps.getDbType() === 'postgres' ? ` (${config.POSTGRES_HOST}:${config.POSTGRES_PORT}/${config.POSTGRES_DB})` : ` (${config.DB_PATH})`}`);
console.log(`API Key authentication: ${config.API_KEY ? 'enabled' : 'disabled'}`);
});
} catch (error) {
console.error('Failed to initialize:', error);
process.exit(1);
}
})();
// Graceful shutdown
process.on('SIGTERM', async () => {
console.log('SIGTERM received, shutting down gracefully...');
await closeBrowser();
await dbOps.close();
process.exit(0);
});
process.on('SIGINT', async () => {
console.log('SIGINT received, shutting down gracefully...');
await closeBrowser();
await dbOps.close();
process.exit(0);
});