-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
233 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const DB = require("../db/db"); | ||
const pool = require("../db/connect"); | ||
const NodeCache = require("node-cache"); | ||
const rateLimit = require("express-rate-limit"); | ||
const slowDown = require("express-slow-down"); | ||
|
||
router.use(express.json()); | ||
|
||
const cache = new NodeCache({ stdTTL: 3600 }); | ||
|
||
const rateLimiter = rateLimit({ | ||
windowMs: 15 * 1000, | ||
max: 7, | ||
}); | ||
|
||
const speedLimiter = slowDown({ | ||
windowMs: 15 * 1000, | ||
delayAfter: 3, | ||
delayMs: (hits) => hits * 100, | ||
}); | ||
|
||
const CACHE_KEYS = { | ||
missingEpisodes: "missing-episodes", | ||
shortenedEpisodes: "shortened-episodes", | ||
lastChecked: "last-checked", | ||
}; | ||
|
||
router.use(rateLimiter); | ||
router.use(speedLimiter); | ||
|
||
router.get("/api/episodes", async (req, res, next) => { | ||
console.info("request fired"); | ||
|
||
const missingCacheExists = cache.has(CACHE_KEYS.missingEpisodes); | ||
const shortenedCacheExists = cache.has(CACHE_KEYS.shortenedEpisodes); | ||
const lastCheckedExists = cache.has(CACHE_KEYS.lastChecked); | ||
|
||
if ([missingCacheExists, shortenedCacheExists, lastCheckedExists].some((c) => !c)) { | ||
const client = await pool.connect(); | ||
const db = DB(client); | ||
|
||
try { | ||
if (!missingCacheExists) { | ||
const missingEpisodes = await db.getMissingEpisodes(); | ||
cache.set(CACHE_KEYS.missingEpisodes, missingEpisodes); | ||
} | ||
|
||
if (!shortenedCacheExists) { | ||
const shortenedEpisodes = await db.getShortenedEpisodes(); | ||
cache.set(CACHE_KEYS.shortenedEpisodes, shortenedEpisodes); | ||
} | ||
|
||
if (!lastCheckedExists) { | ||
const lastChecked = await db.getLastChecked(); | ||
cache.set(CACHE_KEYS.lastChecked, lastChecked); | ||
} | ||
|
||
client.release(); | ||
console.info("db queried and cache updated"); | ||
} catch (error) { | ||
console.error(error.message); | ||
client.release(); | ||
next(error); | ||
} | ||
} | ||
|
||
res.json({ | ||
missingEpisodes: cache.get(CACHE_KEYS.missingEpisodes), | ||
shortenedEpisodes: cache.get(CACHE_KEYS.shortenedEpisodes), | ||
lastCheckedInMs: cache.get(CACHE_KEYS.lastChecked), | ||
}); | ||
}); | ||
|
||
module.exports = { api: router }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,4 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const DB = require("../db/db"); | ||
const pool = require("../db/connect"); | ||
const NodeCache = require("node-cache"); | ||
const rateLimit = require("express-rate-limit"); | ||
const slowDown = require("express-slow-down"); | ||
const { api } = require("./api"); | ||
const { devApi } = require("./dev-api"); | ||
|
||
router.use(express.json()); | ||
|
||
const cache = new NodeCache({ stdTTL: 3600 }); | ||
|
||
const rateLimiter = rateLimit({ | ||
windowMs: 15 * 1000, | ||
max: 7, | ||
}); | ||
|
||
const speedLimiter = slowDown({ | ||
windowMs: 15 * 1000, | ||
delayAfter: 3, | ||
delayMs: (hits) => hits * 100, | ||
}); | ||
|
||
const CACHE_KEYS = { | ||
missingEpisodes: "missing-episodes", | ||
shortenedEpisodes: "shortened-episodes", | ||
lastChecked: "last-checked", | ||
}; | ||
|
||
router.use(rateLimiter); | ||
router.use(speedLimiter); | ||
|
||
router.get("/api/episodes", async (req, res, next) => { | ||
console.info("request fired"); | ||
|
||
const missingCacheExists = cache.has(CACHE_KEYS.missingEpisodes); | ||
const shortenedCacheExists = cache.has(CACHE_KEYS.shortenedEpisodes); | ||
const lastCheckedExists = cache.has(CACHE_KEYS.lastChecked); | ||
|
||
if ([missingCacheExists, shortenedCacheExists, lastCheckedExists].some((c) => !c)) { | ||
const client = await pool.connect(); | ||
const db = DB(client); | ||
|
||
try { | ||
if (!missingCacheExists) { | ||
const missingEpisodes = await db.getMissingEpisodes(); | ||
cache.set(CACHE_KEYS.missingEpisodes, missingEpisodes); | ||
} | ||
|
||
if (!shortenedCacheExists) { | ||
const shortenedEpisodes = await db.getShortenedEpisodes(); | ||
cache.set(CACHE_KEYS.shortenedEpisodes, shortenedEpisodes); | ||
} | ||
|
||
if (!lastCheckedExists) { | ||
const lastChecked = await db.getLastChecked(); | ||
cache.set(CACHE_KEYS.lastChecked, lastChecked); | ||
} | ||
|
||
client.release(); | ||
console.info("db queried and cache updated"); | ||
} catch (error) { | ||
console.error(error.message); | ||
client.release(); | ||
next(error); | ||
} | ||
} | ||
|
||
res.json({ | ||
missingEpisodes: cache.get(CACHE_KEYS.missingEpisodes), | ||
shortenedEpisodes: cache.get(CACHE_KEYS.shortenedEpisodes), | ||
lastCheckedInMs: cache.get(CACHE_KEYS.lastChecked), | ||
}); | ||
}); | ||
|
||
module.exports = router; | ||
module.exports = { api, devApi }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.