forked from romcal/romcal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (78 loc) · 3.12 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import Romcal from 'romcal';
import fs from 'fs';
import path from 'path';
import Fastify from 'fastify';
import { France_Fr } from '@romcal/calendar.france';
const fastify = new Fastify();
/**
* Wire an index HTML page to the URL root of this server.
*/
fastify.get('/', async (request, reply) => {
const stream = fs.createReadStream(path.resolve('./public/index.html'));
return reply.type('text/html').send(stream);
});
/**
* Example that output data of the General Roman calendar, and all its defined locales.
* The year is optional (taking the current year by default).
*/
const manageGeneralRomanRoute = async (request, reply) => {
const locale = request.params['locale'].toLowerCase();
const localeIndex = Romcal.LOCALE_KEYS.indexOf(locale);
// Check if the provided locale exists in the `general_roman` calendar. If no, return a 404 error.
if (localeIndex === -1) {
const code = 404;
return reply.status(code).send({ code, message: "The provided locale doesn't exists" });
}
// Load dynamically the localized General Roman Calendar
const module = await import(`@romcal/calendar.general-roman/esm/${locale}.mjs`);
const localeVarName = Romcal.LOCALE_VAR_NAMES[localeIndex];
const localizedCalendar = module[`GeneralRoman_${localeVarName}`];
// Initialize a romcal object with the General Roman Calendar data.
const romcalGeneralRoman = new Romcal({ localizedCalendar });
try {
// Generate a liturgical calendar with the provided locale and year.
const year = request.params['year'];
const data = await romcalGeneralRoman.generateCalendar(year);
// Finally, send the computed data.
return reply.code(200).header('Content-Type', 'application/json; charset=utf-8').send(JSON.stringify(data));
} catch ({ message }) {
// If romcal return an error, we must manage and display it through Fastify.
const code = 500;
reply.status(code).send({ code, message });
}
};
fastify.get('/romcal/general-roman/:locale', manageGeneralRomanRoute);
fastify.get('/romcal/general-roman/:locale/:year', manageGeneralRomanRoute);
/**
* Simple example with only 1 supported calendar and locale by the REST API:
* the calendar of France and the `fr` locale.
*/
const romcalFranceFr = new Romcal({ localizedCalendar: France_Fr });
const manageFranceFrRoute = async (request, reply) => {
try {
const year = request.params['year'];
const data = await romcalFranceFr.generateCalendar(year);
reply.code(200).header('Content-Type', 'application/json; charset=utf-8').send(JSON.stringify(data));
} catch ({ message }) {
// If romcal return an error, we must manage and display it through Fastify.
const code = 500;
reply.status(code).send({ code, message });
}
};
fastify.get('/romcal/france/fr', manageFranceFrRoute);
fastify.get('/romcal/france/fr/:year', manageFranceFrRoute);
/**
* Run the server.
* @returns {Promise<void>}
*/
const start = async () => {
try {
await fastify.listen({ port: 3000 });
console.log('Romcal server listening on port 3000');
console.log('url: http://127.0.0.1:3000');
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();