Skip to content

Commit

Permalink
Merge pull request #106 from Nayanika1402/nayanika1
Browse files Browse the repository at this point in the history
Health tip card added
  • Loading branch information
akanshSirohi authored Oct 20, 2024
2 parents c02696e + b15338a commit 9ecf944
Show file tree
Hide file tree
Showing 5 changed files with 546 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Here is a list of cards that can currently be used:
| `security-tips-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/security-tips-card?theme=dark)` |
| `french-word-of-the-day-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/french-word-of-the-day-card)` |
| `travel-destinations-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/travel-destinations-card?theme=dark)` |
| `health-tip-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/health-tip-card?theme=dark)` |

## Themes

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const available_cards = {
"/harry-potter-spell-card": require("./src/cards/harry-potter-spells"),
"/travel-destinations-card": require("./src/cards/travel_destinations"),
"/french-word-of-the-day-card": require("./src/cards/french_word_of_the_day"),
"/health-tip-card": require("./src/cards/health-tip-card"),
};

app.use(express.json());
Expand Down
53 changes: 53 additions & 0 deletions src/cards/health-tip-card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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/health-tips.json";
const DEFAULT_THEME = "light";

const handleTheme = (req, res, next) => {
req.theme = req.query.theme || DEFAULT_THEME;
next();
};

const handleOptions = (req, res, next) => {
// Custom theme moderation
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();
};

router.get("/", handleTheme, handleOptions, async (req, res) => {
try {
const healthTips = JSON.parse(await fs.readFile(DATA_FILE_PATH, "utf8"));
const random_tip = healthTips[Math.floor(Math.random() * healthTips.length)];

const health_tip_card = await generateCard(
random_tip.content,
req.theme,
req.options,
Languages.ENGLISH
);

res.writeHead(200, {
"Content-Type": "image/svg+xml",
"Cache-Control": `public, max-age=${CARD_AGE}`,
});
res.end(health_tip_card);
} catch (error) {
console.error("Error:", error);
res.status(500).send("Internal Server Error");
}
});

module.exports = router;
Loading

0 comments on commit 9ecf944

Please sign in to comment.