-
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 branch 'master' into GOT-quotes
- Loading branch information
Showing
7 changed files
with
629 additions
and
226 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
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
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,56 @@ | ||
// src/cards/fun-fact-card.js | ||
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/fun_facts.json"; | ||
const DEFAULT_THEME = "light"; | ||
|
||
// Middleware to handle theme | ||
const handleTheme = (req, res, next) => { | ||
req.theme = req.query.theme || DEFAULT_THEME; | ||
next(); | ||
}; | ||
|
||
// Middleware to handle options like custom themes | ||
const handleOptions = (req, res, next) => { | ||
if (req.theme === "my_theme") { | ||
req.theme = "pattern_3"; | ||
req.options = { | ||
card_color: "#ffffffc2", | ||
font_color: "#000", | ||
shadow: false, | ||
}; | ||
} else if (req.theme === "custom") { | ||
req.options = parseOptions(req.query); | ||
} | ||
next(); | ||
}; | ||
|
||
// Route to display fun fact card | ||
router.get("/", handleTheme, handleOptions, async (req, res) => { | ||
try { | ||
const facts = JSON.parse(await fs.readFile(DATA_FILE_PATH, "utf8")); | ||
const random_fact = facts[Math.floor(Math.random() * facts.length)]; | ||
|
||
const fact_card = await generateCard( | ||
random_fact.fact, | ||
req.theme, | ||
req.options, | ||
Languages.ENGLISH | ||
); | ||
|
||
res.writeHead(200, { | ||
"Content-Type": "image/svg+xml", | ||
"Cache-Control": `public, max-age=${CARD_AGE}`, | ||
}); | ||
res.end(fact_card); | ||
} catch (error) { | ||
console.error("Error:", error); | ||
res.status(500).send("Internal Server Error"); | ||
} | ||
}); | ||
|
||
module.exports = router; |
Oops, something went wrong.