From: xangelo Date: Fri, 26 Jan 2024 18:58:54 +0000 (-0500) Subject: add ability to add feeds on the fly X-Git-Url: https://git.xangelo.ca/?a=commitdiff_plain;h=32db91122af5dac7b92c6e62f1812c2b9e9ef801;p=river.git add ability to add feeds on the fly --- diff --git a/.env.sample b/.env.sample index 7e6c7ea..ec01267 100644 --- a/.env.sample +++ b/.env.sample @@ -1,2 +1,3 @@ API_PORT=9191 FEED_REFRESH_RATE=1 +ADMIN_KEY=test diff --git a/src/server.ts b/src/server.ts index f250f00..9f22797 100644 --- a/src/server.ts +++ b/src/server.ts @@ -9,7 +9,7 @@ import { renderFeedItem, renderFeedItemHeader } from './views'; const db = knex({ client: 'better-sqlite3', connection: { - filename: join(__dirname, '..', 'data.db') + filename: join('./data.db') } }); const parser = new Parser(); @@ -64,6 +64,7 @@ async function queryFeeds() { } app.use(express.static(join(__dirname, 'public'))); +app.use(express.json()); app.use((req, res, next) => { console.log(`${req.method}: ${req.url}`); @@ -128,6 +129,29 @@ app.get('/river', async (req, res) => { }).join("\n")); }); +app.post('/feeds', async (req, res) => { + if(req.body.key !== process.env.ADMIN_KEY) { + res.sendStatus(400).end(); + return; + } + const now = new Date(); + const feed = { + url: req.body.url, + title: req.body.title, + favicon: req.body.favicon, + created_at: now, + updated_at: now + }; + + const rows = await db('feeds').insert(feed).onConflict().ignore().returning('*'); + + res.json(rows); +}); + +app.get('/feeds', async (req, res) => { + res.json(await db.select('*').from('feeds').orderBy('created_at')); +}) + app.listen(process.env.API_PORT, () => { console.log(`Listening on port ${process.env.API_PORT}`); console.log(`Feed refresh rate: ${(Number(process.env.FEED_REFRESH_RATE) + 1)}m`);