49 lines
1.8 KiB
JavaScript
49 lines
1.8 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const path = require('path');
|
|
const cron = require('node-cron');
|
|
|
|
const db = require('./db');
|
|
const { fetchAllFeeds } = require('./feedFetcher');
|
|
const foldersRouter = require('./routes/folders');
|
|
const feedsRouter = require('./routes/feeds');
|
|
const articlesRouter = require('./routes/articles');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 8080;
|
|
const FETCH_INTERVAL_MIN = Number(process.env.FETCH_INTERVAL_MINUTES || 15);
|
|
|
|
app.use(cors());
|
|
app.use(express.json({ limit: '2mb' }));
|
|
|
|
app.use('/api/folders', foldersRouter);
|
|
app.use('/api/feeds', feedsRouter);
|
|
app.use('/api/articles', articlesRouter);
|
|
|
|
app.get('/api/stats', (req, res) => {
|
|
const feedCount = db.prepare('SELECT COUNT(*) c FROM feeds').get().c;
|
|
const unread = db.prepare('SELECT COUNT(*) c FROM articles WHERE is_read = 0').get().c;
|
|
const starred = db.prepare('SELECT COUNT(*) c FROM articles WHERE is_starred = 1').get().c;
|
|
res.json({ feedCount, unread, starred });
|
|
});
|
|
|
|
app.get('/api/health', (req, res) => res.json({ ok: true }));
|
|
|
|
// Serve built frontend
|
|
const FRONTEND_DIST = path.join(__dirname, 'public');
|
|
app.use(express.static(FRONTEND_DIST));
|
|
app.get(/^(?!\/api).*/, (req, res) => {
|
|
res.sendFile(path.join(FRONTEND_DIST, 'index.html'));
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Dispatch RSS reader listening on port ${PORT}`);
|
|
// Fetch on boot, then on a schedule
|
|
fetchAllFeeds().then((r) => console.log(`Initial fetch complete: ${r.length} feeds`)).catch((e) => console.error(e));
|
|
});
|
|
|
|
cron.schedule(`*/${FETCH_INTERVAL_MIN} * * * *`, () => {
|
|
console.log('Running scheduled feed fetch...');
|
|
fetchAllFeeds().then((r) => console.log(`Scheduled fetch complete: ${r.length} feeds`)).catch((e) => console.error(e));
|
|
});
|