Skip to content

Commit

Permalink
Merge branch 'master' into GOT-quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
akanshSirohi authored Oct 11, 2024
2 parents c5c43d9 + 093d1ee commit aaba4bf
Show file tree
Hide file tree
Showing 7 changed files with 629 additions and 226 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ Here is a list of cards that can currently be used:
| `programming-facts-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/programming-facts-card?theme=dark)` | ![Card](https://afraid-ninnetta-github-cards.koyeb.app/programming-facts-card?theme=dark) |
| `top-tweets-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/top-tweets-card)` | ![Card](https://afraid-ninnetta-github-cards.koyeb.app/top-tweets-card) |
| `spanish-quote-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/spanish-quote-card?theme=dark)` | ![Card](https://afraid-ninnetta-github-cards.koyeb.app/spanish-quote-card?theme=dark) |

| `fun-fact-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/fun-fact-card?theme=dark)` | ![Card](https://afraid-ninnetta-github-cards.koyeb.app/fun-fact-card?theme=dark) |

| `github-facts-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/github-facts-card?theme=dark)` | ![Card](https://afraid-ninnetta-github-cards.koyeb.app/github-facts-card?theme=dark) |
| `random-facts-card` | `![Card](https://afraid-ninnetta-github-cards.koyeb.app/random-facts-card?theme=dark)` | ![Card](https://afraid-ninnetta-github-cards.koyeb.app/random-facts-card?theme=dark) |


## Themes

### Options
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ const available_cards = {
"/spanish-quote-card": require("./src/cards/spanish-quote"),
"/top-tweets-card": require("./src/cards/top-tweets"),
"/github-facts-card": require("./src/cards/github-facts"),
"/got-quotes-card": require("./src/cards/got-quotes"),
"/random-facts-card": require("./src/cards/random-facts")
"/random-facts-card": require("./src/cards/random-facts"),
"/fun-fact-card": require("./src/cards/fun-fact-card"),
"/got-quotes-card": require("./src/cards/got-quotes"),
};

app.use(express.json());
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
"node" : ">=16.0.0"
},
"dependencies": {
"axios": "^1.7.7",
"canvas": "2.11.2",
"express": "^4.21.1",
"satori": "^0.11.2",
"satori-html": "^0.3.2"
}
}
}
56 changes: 56 additions & 0 deletions src/cards/fun-fact-card.js
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;
Loading

0 comments on commit aaba4bf

Please sign in to comment.