Skip to content

Commit

Permalink
Homepage changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bgv2 committed May 10, 2024
1 parent 32a5d87 commit 2c708b2
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,43 @@ const db = sqlite3.cached.Database(dbPath, sqlite3.OPEN_READONLY)
const dbGet = promisify(db.get).bind(db)

const app = express()
const limiter = rateLimit({
const rateLimitConfig = {
windowMs: 1000,
limit: 2, // 2 requests in 1 sec
standardHeaders: 'draft-7',
legacyHeaders: false,
})
app.use(limiter)
}
app.use(rateLimit(rateLimitConfig))

app.get('/randquote', async function (_req, res) {
const { quote, source: author } = await dbGet('SELECT quote, source FROM Quotes WHERE id IN (SELECT id FROM Quotes ORDER BY RANDOM() LIMIT 1);')
res.json({ quote, author })
})
app.get('/', function (_req, res) {
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Quotoserve</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style>
body { font-family: sans-serif; }
</style>
<body>
<h1>Quotoserve</h1>
<p>Available routes:
<ul>
<li>${app._router.stack.map(r => r.route?.path).filter(r => r !== undefined).join("</li><li>")}</li>
${app._router.stack
.filter(r => r.route?.path !== undefined)
.map(r => `<li><a href="${r.route.path}">${r.route.path}</a></li>`)
.join('')}
</ul>
</p>
<p>Rate Limit: ${rateLimitConfig.limit} requests in ${rateLimitConfig.windowMs / 1000} second(s)</p>
</body>
</html>
`)
})

Expand Down

0 comments on commit 2c708b2

Please sign in to comment.