Add Discord notifications for image generation and snapshot creation
This commit is contained in:
18
api.js
18
api.js
@@ -6,6 +6,7 @@ const crypto = require('crypto');
|
||||
const { initPool, renderHtml, POOL_SIZE } = require('./browserPool');
|
||||
const v2Routes = require('./v2Routes');
|
||||
const { cleanupExpiredSessions, cleanupExpiredSnapshots } = require('./db');
|
||||
const { notifyImageGenerated } = require('./discordWebhook');
|
||||
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
@@ -16,6 +17,15 @@ if (!fs.existsSync(CACHE_DIR)) {
|
||||
fs.mkdirSync(CACHE_DIR);
|
||||
}
|
||||
|
||||
// get client ip address
|
||||
function getClientIp(req) {
|
||||
const forwarded = req.headers['x-forwarded-for'];
|
||||
if (forwarded) {
|
||||
return forwarded.split(',')[0].trim();
|
||||
}
|
||||
return req.ip || req.socket.remoteAddress || 'unknown';
|
||||
}
|
||||
|
||||
app.use(express.json({ limit: '1gb' }));
|
||||
app.use(express.urlencoded({ limit: '1gb', extended: true }));
|
||||
|
||||
@@ -288,6 +298,10 @@ app.get('/generate', async (req, res) => {
|
||||
image = await generateQuoteBuffer(config);
|
||||
cacheImage(config, image);
|
||||
fromCache = false;
|
||||
|
||||
// notify discord about new image
|
||||
const clientIp = getClientIp(req);
|
||||
notifyImageGenerated(config, clientIp).catch(err => console.error('Discord notification failed:', err));
|
||||
}
|
||||
|
||||
res.setHeader('Content-Type', 'image/png');
|
||||
@@ -387,6 +401,10 @@ app.post('/generate', async (req, res) => {
|
||||
image = await generateQuoteBuffer(config);
|
||||
cacheImage(config, image);
|
||||
fromCache = false;
|
||||
|
||||
// notify discord about new image
|
||||
const clientIp = getClientIp(req);
|
||||
notifyImageGenerated(config, clientIp).catch(err => console.error('Discord notification failed:', err));
|
||||
}
|
||||
|
||||
res.setHeader('Content-Type', 'image/png');
|
||||
|
||||
Reference in New Issue
Block a user