From 32db91122af5dac7b92c6e62f1812c2b9e9ef801 Mon Sep 17 00:00:00 2001 From: xangelo Date: Fri, 26 Jan 2024 13:58:54 -0500 Subject: [PATCH] add ability to add feeds on the fly --- .env.sample | 1 + src/server.ts | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) 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`); -- 2.25.1