-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from HritvikBhatia/breaking-bad-quotes
Breaking bad quotes
- Loading branch information
Showing
5 changed files
with
562 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
.prettierignore | ||
.idea | ||
.idea | ||
package-lock.json |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const fs = require("fs").promises; | ||
const { generateCard, CARD_AGE, Languages } = require("../card-generator"); | ||
const { parseOptions } = require("../options-parser"); | ||
|
||
const DATA_FILE_PATH = "./src/data/breaking-bad-quotes.json"; | ||
const DEFAULT_THEME = "dark_2"; | ||
|
||
const handleTheme = (req, res, next) => { | ||
req.theme = req.query.theme || DEFAULT_THEME; | ||
next(); | ||
}; | ||
|
||
const handleOptions = (req, res, next) => { | ||
if (req.theme === "custom") { | ||
req.options = parseOptions(req.query); | ||
} | ||
next(); | ||
}; | ||
|
||
router.get("/", handleTheme, handleOptions, async (req, res) => { | ||
try { | ||
const breakingbadQuotesData = JSON.parse( | ||
await fs.readFile(DATA_FILE_PATH, "utf8") | ||
); | ||
const randomQuote = breakingbadQuotesData[Math.floor(Math.random() * breakingbadQuotesData.length)]; | ||
const quoteContent = `"${randomQuote.quote}"\n\nAuthor- ${randomQuote.author}`; | ||
|
||
const quoteCard = await generateCard( | ||
quoteContent, | ||
req.theme, | ||
req.options, | ||
Languages.ENGLISH | ||
); | ||
|
||
res.writeHead(200, { | ||
"Content-Type": "image/svg+xml", | ||
"Cache-Control": `public, max-age=${CARD_AGE}`, | ||
}); | ||
res.end(quoteCard); | ||
} catch (error) { | ||
console.error("Error:", error); | ||
res.status(500).send("Internal Server Error"); | ||
} | ||
}); | ||
|
||
module.exports = router; |
Oops, something went wrong.