2024-07-31 14:45:22 +03:00
|
|
|
const logger = require('../logger/main.js');
|
2024-08-02 21:51:54 +03:00
|
|
|
const config = require('../config/main.js')();
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
const fastify = require('fastify')({
|
2024-08-11 19:46:07 +03:00
|
|
|
logger: config['server']['log']['print'] ? logger : false,
|
2024-08-02 21:51:54 +03:00
|
|
|
...config['server']['ssl'] ? {
|
|
|
|
https: {
|
|
|
|
key: fs.readFileSync(config['server']['private_key'], 'utf8'),
|
|
|
|
cert: fs.readFileSync(config['server']['cert'], 'utf8'),
|
|
|
|
}
|
|
|
|
} : false
|
|
|
|
});
|
|
|
|
|
2024-07-30 18:06:21 +03:00
|
|
|
const rate = require('../database/main.js');
|
2024-08-02 15:42:35 +03:00
|
|
|
const chart = require('../chart/main.js');
|
2024-07-30 18:06:21 +03:00
|
|
|
|
2024-08-02 21:51:54 +03:00
|
|
|
require('../collect-currency/main.js').main();
|
|
|
|
|
2024-07-30 18:06:21 +03:00
|
|
|
fastify.get('/api/getRate/', async function (req, res){
|
|
|
|
const query = req.query;
|
2024-07-31 14:45:22 +03:00
|
|
|
if (!query['from_currency'] || !query['conv_currency']) {
|
|
|
|
return res.status(400).send({
|
|
|
|
status: 400,
|
|
|
|
message: 'The from_currency and conv_currency fields are required'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-30 18:06:21 +03:00
|
|
|
|
2024-07-31 14:45:22 +03:00
|
|
|
if (query['date']) return rate.getDay(query['from_currency'], query['conv_currency'], query['date']);
|
|
|
|
else if (query['start_date'] && query['end_date']) return rate.getPeriod(
|
|
|
|
query['from_currency'],
|
|
|
|
query['conv_currency'],
|
|
|
|
query['start_date'],
|
|
|
|
query['end_date']
|
2024-07-30 18:06:21 +03:00
|
|
|
);
|
2024-07-31 14:45:22 +03:00
|
|
|
else return res.status(400).send({
|
|
|
|
status: 400,
|
|
|
|
message: 'The date or period field is incorrect. ' +
|
|
|
|
'There must be fields \'date\' or \'start_date\' and \'end_date\'. ' +
|
|
|
|
'Read more in the documentation'
|
|
|
|
});
|
2024-07-30 18:06:21 +03:00
|
|
|
});
|
|
|
|
|
2024-08-02 15:42:35 +03:00
|
|
|
fastify.get('/api/getChart/', async function (req, res){
|
|
|
|
const query = req.query;
|
|
|
|
if (!query['from_currency'] || !query['conv_currency']) {
|
|
|
|
return res.status(400).send({
|
|
|
|
status: 400,
|
|
|
|
message: 'The from_currency and conv_currency fields are required',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!query['start_date'] || !query['end_date']) return res.status(400).send({
|
|
|
|
status: 400,
|
|
|
|
message: 'start_date and end_date is required',
|
|
|
|
});
|
|
|
|
|
|
|
|
const charts = await chart.gen_chart(
|
|
|
|
query['from_currency'],
|
|
|
|
query['conv_currency'],
|
|
|
|
query['start_date'],
|
|
|
|
query['end_date'],
|
|
|
|
)
|
|
|
|
|
2024-08-03 00:32:44 +03:00
|
|
|
if (config['currency']['chart']['save_chart']) chart.save_chart(
|
|
|
|
charts,
|
|
|
|
`${query['from_currency']} ${query['conv_currency']} ` +
|
|
|
|
`(${query['start_date']} - ${query['end_date']}).png`
|
|
|
|
)
|
|
|
|
|
2024-08-02 15:42:35 +03:00
|
|
|
return res.status(200).send({
|
|
|
|
status: 200,
|
|
|
|
message: charts,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-08-02 21:51:54 +03:00
|
|
|
fastify.listen({
|
|
|
|
port: 3000,
|
|
|
|
host: config['server']['host'] ? config['server']['host'] : 'localhost',
|
|
|
|
}, function (err) {
|
2024-07-30 18:06:21 +03:00
|
|
|
if (err) {
|
|
|
|
fastify.log.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
});
|