forked from plibither8/made-with-love-in
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (44 loc) · 1.28 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const path = require('path');
const fastify = require('fastify')();
const {BadgeFactory} = require('gh-badges');
const DATA = require('./data.json');
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/',
})
fastify.get('/', (req, reply) => {
return reply.sendFile('index.html');
});
fastify.get('/index.js', (req, reply) => {
return reply.sendFile('index.js');
});
fastify.get('/list', (req, reply) => {
return reply.json(DATA);
});
fastify.get('/:countryCode', (req, reply) => {
let {countryCode} = req.params;
countryCode = countryCode.toLowerCase();
if (!(countryCode in DATA)) {
reply.status(400);
return reply.send(`<pre>Error 400: country code "${countryCode}" not found.\nGET /list for list of valid country codes.</pre>`);
}
const config = {
heart: false,
...req.query
};
const countryName = DATA[countryCode.toLowerCase()];
const countryText = 'text' in config ? config.text : countryName;
const badgeFormat = {
template: 'flat',
colorB: '#dc3545',
...config,
text: [
`Made with ${config.heart ? '❤' : 'love'} in`,
countryText
]
};
const badgeSvg = new BadgeFactory().create(badgeFormat);
reply.type('image/svg+xml');
return reply.send(badgeSvg);
});
fastify.listen(process.env.PORT || 5000);