Refactor image rendering to use a browser pool and update dependencies
This commit is contained in:
34
api.js
34
api.js
@@ -1,8 +1,8 @@
|
||||
const express = require('express');
|
||||
const nodeHtmlToImage = require('node-html-to-image');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const { initPool, renderHtml, POOL_SIZE } = require('./browserPool');
|
||||
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
@@ -262,20 +262,7 @@ async function generateQuoteBuffer(config) {
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
const image = await nodeHtmlToImage({
|
||||
html: html,
|
||||
puppeteerArgs: {
|
||||
args: ['--no-sandbox'],
|
||||
defaultViewport: {
|
||||
width: 3240,
|
||||
height: 3240
|
||||
}
|
||||
},
|
||||
beforeScreenshot: async (page) => {
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
}
|
||||
});
|
||||
|
||||
const image = await renderHtml(html, 1000);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -400,9 +387,16 @@ setInterval(() => {
|
||||
cleanupOldCache();
|
||||
}, 60 * 60 * 1000);
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Quote generator API running on http://localhost:${PORT}`);
|
||||
console.log(`GET: http://localhost:${PORT}/generate?text=Hello&displayName=Test&username=@test×tamp=1735574400`);
|
||||
console.log(`POST: http://localhost:${PORT}/generate`);
|
||||
console.log(`Cache cleared on startup, cleanup runs every hour`);
|
||||
// Initialize browser pool then start server
|
||||
initPool().then(() => {
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Quote generator API running on http://localhost:${PORT}`);
|
||||
console.log(`Browser pool size: ${POOL_SIZE} (set BROWSER_POOL_SIZE env var to change)`);
|
||||
console.log(`GET: http://localhost:${PORT}/generate?text=Hello&displayName=Test&username=@test×tamp=1735574400`);
|
||||
console.log(`POST: http://localhost:${PORT}/generate`);
|
||||
console.log(`Cache cleared on startup, cleanup runs every hour`);
|
||||
});
|
||||
}).catch(err => {
|
||||
console.error('Failed to initialize browser pool:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user