feat: autogen service — content_queue, autogen_settings, TOPIC_BANK, cron API
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { query } = require('../config/db');
|
||||
const { runAutogen, runAutogenForCategory, getAutogenStatus, TOPIC_BANK } = require('../services/autogen');
|
||||
|
||||
// GET /api/autogen/status — текущий статус и настройки
|
||||
router.get('/status', async (_, res) => {
|
||||
try {
|
||||
const status = await getAutogenStatus();
|
||||
res.json(status);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// POST /api/autogen/run — запустить генерацию (cron или ручной запуск)
|
||||
router.post('/run', async (req, res) => {
|
||||
try {
|
||||
const { category } = req.body;
|
||||
// Запускаем фоново, сразу отвечаем
|
||||
res.json({ ok: true, message: 'Generation started' });
|
||||
runAutogen({ forceCategory: category || null }).catch(err =>
|
||||
console.error('[Autogen] run error:', err.message)
|
||||
);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// PATCH /api/autogen/settings/:category — обновить настройки
|
||||
router.patch('/settings/:category', async (req, res) => {
|
||||
try {
|
||||
const { enabled, per_day } = req.body;
|
||||
const fields = []; const vals = []; let i = 1;
|
||||
if (enabled !== undefined) { fields.push(`enabled=$${i++}`); vals.push(enabled); }
|
||||
if (per_day !== undefined) { fields.push(`per_day=$${i++}`); vals.push(per_day); }
|
||||
if (!fields.length) return res.status(400).json({ error: 'Nothing to update' });
|
||||
vals.push(req.params.category);
|
||||
await query(`UPDATE autogen_settings SET ${fields.join(',')} WHERE category=$${i}`, vals);
|
||||
res.json({ ok: true });
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// GET /api/autogen/queue — очередь тем
|
||||
router.get('/queue', async (_, res) => {
|
||||
try {
|
||||
const { rows } = await query(
|
||||
`SELECT * FROM content_queue ORDER BY priority DESC, created_at ASC LIMIT 100`
|
||||
);
|
||||
res.json(rows);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// POST /api/autogen/queue — добавить тему в очередь
|
||||
router.post('/queue', async (req, res) => {
|
||||
try {
|
||||
const { category, topic, tags = [], keywords = [], priority = 5 } = req.body;
|
||||
if (!category || !topic) return res.status(400).json({ error: 'category and topic required' });
|
||||
const { rows } = await query(
|
||||
`INSERT INTO content_queue (category, topic, tags, keywords, priority)
|
||||
VALUES ($1,$2,$3,$4,$5) RETURNING *`,
|
||||
[category, topic, JSON.stringify(tags), JSON.stringify(keywords), priority]
|
||||
);
|
||||
res.json(rows[0]);
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// DELETE /api/autogen/queue/:id
|
||||
router.delete('/queue/:id', async (req, res) => {
|
||||
try {
|
||||
await query(`DELETE FROM content_queue WHERE id=$1`, [req.params.id]);
|
||||
res.json({ ok: true });
|
||||
} catch (err) { res.status(500).json({ error: err.message }); }
|
||||
});
|
||||
|
||||
// GET /api/autogen/topics — банк тем
|
||||
router.get('/topics', async (_, res) => {
|
||||
res.json(TOPIC_BANK);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user