You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.3 KiB
38 lines
1.3 KiB
const express = require('express');
|
|
const {createChartOnData} = require("./chartGenerator");
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// Middleware для парсинга JSON тела запроса
|
|
app.use(express.json({ limit: '10mb' }));
|
|
//ai sloooop moment
|
|
app.post('/api/chart/:id.png', async (req, res) => {
|
|
const { id } = req.params;
|
|
const payload = req.body;
|
|
|
|
// Проверяем, что тело запроса не пустое
|
|
if (!payload || (typeof payload === 'object' && Object.keys(payload).length === 0)) {
|
|
return res.status(400).send('Bad Request: request body is required');
|
|
}
|
|
|
|
try {
|
|
// Получаем PNG поток из пользовательской функции
|
|
const pngStream = createChartOnData(payload);
|
|
|
|
// Устанавливаем заголовок ответа
|
|
res.setHeader('Content-Type', 'image/png');
|
|
|
|
// Отправляем поток в ответ
|
|
pngStream.pipe(res);
|
|
} catch (err) {
|
|
console.error('Error generating PNG:', err);
|
|
res.status(500).send('Internal Server Error: could not generate image');
|
|
}
|
|
});
|
|
|
|
// Запуск сервера
|
|
app.listen(PORT, () => {
|
|
console.log(`Server is running on http://localhost:${PORT}`);
|
|
console.log(`Endpoint: POST /api/chart/:id.png`);
|
|
});
|