mirror of
https://github.com/Redume/Kekkai.git
synced 2025-02-23 20:51:25 +03:00
Удалено из-за перехода на микросервисы
This commit is contained in:
parent
ae00a1a297
commit
8f0207ad06
7 changed files with 0 additions and 177 deletions
7
.gitignore
vendored
7
.gitignore
vendored
|
@ -1,7 +0,0 @@
|
||||||
.idea
|
|
||||||
node_modules
|
|
||||||
|
|
||||||
config.yaml
|
|
||||||
package-lock.json
|
|
||||||
|
|
||||||
CertSSL/
|
|
|
@ -1,6 +0,0 @@
|
||||||
CREATE TABLE currency(
|
|
||||||
from_currency TEXT NOT NULL,
|
|
||||||
conv_currency TEXT NOT NULL,
|
|
||||||
rate FLOAT NOT NULL,
|
|
||||||
date DATE NOT NULL
|
|
||||||
);
|
|
62
main.js
62
main.js
|
@ -1,62 +0,0 @@
|
||||||
const fastify = require('fastify')({ logger: true });
|
|
||||||
const schedule = require('node-schedule');
|
|
||||||
const pool = require("./postgresql.js");
|
|
||||||
const yaml = require("yaml")
|
|
||||||
const fs = require("fs");
|
|
||||||
const config = yaml.parse(fs.readFileSync("./config.yaml", "utf-8"));
|
|
||||||
|
|
||||||
const saveRate = require('./utils/saveRate.js');
|
|
||||||
const response = require('./utils/errorResponse');
|
|
||||||
|
|
||||||
saveRate();
|
|
||||||
schedule.scheduleJob('30 8 * * *', async function () {
|
|
||||||
console.log('I save the currency data');
|
|
||||||
await saveRate();
|
|
||||||
});
|
|
||||||
|
|
||||||
fastify.get('/api/getRate/', async function (req) {
|
|
||||||
if (!req['query']?.['fromCurrency'] || !req['query']?.['convCurrency']) return response(
|
|
||||||
'error',
|
|
||||||
400,
|
|
||||||
'fromCurrency and convCurrency parameter is required'
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!req['query']?.['periodStart']) return response(
|
|
||||||
'error',
|
|
||||||
400,
|
|
||||||
'periodStart parameter is required'
|
|
||||||
);
|
|
||||||
|
|
||||||
let data = await pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3', [
|
|
||||||
req['query']['fromCurrency'],
|
|
||||||
req['query']['convCurrency'],
|
|
||||||
req['query']['periodStart'],
|
|
||||||
]).then(response('error', 500, 'Internal Server Error'));
|
|
||||||
|
|
||||||
if (!data['rows']?.[0]) return response(
|
|
||||||
'error',
|
|
||||||
204,
|
|
||||||
'There is no data for this time'
|
|
||||||
);
|
|
||||||
|
|
||||||
if (req['query']?.['periodEnd']) {
|
|
||||||
let data = await pool.query('SELECT * FROM currency WHERE (date BETWEEN $3 AND $4) AND from_currency = $1 AND conv_currency = $2', [
|
|
||||||
req['query']['fromCurrency'],
|
|
||||||
req['query']['convCurrency'],
|
|
||||||
req['query']['periodStart'],
|
|
||||||
req['query']['periodEnd'],
|
|
||||||
]);
|
|
||||||
|
|
||||||
return data['rows'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return data['rows'][0];
|
|
||||||
});
|
|
||||||
|
|
||||||
fastify.listen({
|
|
||||||
host: config['server']['host'],
|
|
||||||
port: config['server']['port'],
|
|
||||||
}, err => {
|
|
||||||
if (err) throw err
|
|
||||||
console.log(`server listening on ${fastify.server.address().port}`)
|
|
||||||
})
|
|
17
package.json
17
package.json
|
@ -1,17 +0,0 @@
|
||||||
{
|
|
||||||
"name": "kekkai",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "Chart for popular fiat currencies",
|
|
||||||
"main": "main.js",
|
|
||||||
"scripts": {},
|
|
||||||
"author": "Redume",
|
|
||||||
"license": "LGPL-3.0-only",
|
|
||||||
"dependencies": {
|
|
||||||
"axios": "^1.6.7",
|
|
||||||
"axios-retry": "^4.0.0",
|
|
||||||
"fastify": "^4.26.1",
|
|
||||||
"node-schedule": "^2.1.1",
|
|
||||||
"pg": "^8.11.3",
|
|
||||||
"yaml": "^2.3.4"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
const pg = require("pg");
|
|
||||||
const yaml = require("yaml")
|
|
||||||
const fs = require("fs");
|
|
||||||
const config = yaml.parse(fs.readFileSync("./config.yaml", "utf-8"));
|
|
||||||
|
|
||||||
const pool = new pg.Pool({
|
|
||||||
user: config['database']['user'],
|
|
||||||
password: config['database']['password'],
|
|
||||||
host: config['database']['host'],
|
|
||||||
port: config['database']['port'],
|
|
||||||
database: config['database']['name']
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = pool;
|
|
|
@ -1,19 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {string} status
|
|
||||||
* @param {number} statusCode
|
|
||||||
* @param {string} message
|
|
||||||
* @returns {{data: {message}, status, statusCode}}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function response(status, statusCode, message) {
|
|
||||||
return {
|
|
||||||
status: status,
|
|
||||||
statusCode: statusCode,
|
|
||||||
data: {
|
|
||||||
message: message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = response;
|
|
|
@ -1,52 +0,0 @@
|
||||||
const pool = require('../postgresql.js');
|
|
||||||
const yaml = require('yaml');
|
|
||||||
const fs = require('fs');
|
|
||||||
const axios = require('axios');
|
|
||||||
const config = yaml.parse(fs.readFileSync('./config.yaml', 'utf-8'));
|
|
||||||
|
|
||||||
async function save_fiat() {
|
|
||||||
if (!config['currency']['collecting']['fiat']) return;
|
|
||||||
|
|
||||||
config['currency']['fiat'].forEach(
|
|
||||||
(value) => config['currency']['fiat'].forEach(async (pair) => {
|
|
||||||
if(value !== pair) {
|
|
||||||
const res = await axios.get(
|
|
||||||
`https://duckduckgo.com/js/spice/currency/1/${value}/${pair}`,
|
|
||||||
{
|
|
||||||
timeout: 3000,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const regExp = new RegExp('\\(\\s*(.*)\\s*\\);$', 'mg');
|
|
||||||
const data = JSON.parse(Array.from(res.data.matchAll(regExp))[0][1])
|
|
||||||
|
|
||||||
delete data['terms'];
|
|
||||||
delete data['privacy'];
|
|
||||||
|
|
||||||
const point = data['to'][0]['mid'].toString().indexOf('.') + 4;
|
|
||||||
|
|
||||||
pool.query('SELECT * FROM currency WHERE from_currency = $1 AND conv_currency = $2 AND date = $3',
|
|
||||||
[
|
|
||||||
value,
|
|
||||||
pair,
|
|
||||||
new Date(data['timestamp']).toLocaleDateString()
|
|
||||||
]
|
|
||||||
).then(async (db) => {
|
|
||||||
if (!db['rows'][0]) {
|
|
||||||
await pool.query(`INSERT INTO currency (from_currency, conv_currency, rate, date)
|
|
||||||
VALUES ($1, $2, $3, $4) RETURNING *`,
|
|
||||||
[
|
|
||||||
value,
|
|
||||||
pair,
|
|
||||||
data['to'][0]['mid'].toString().slice(0, point),
|
|
||||||
new Date(data['timestamp']).toLocaleDateString()
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = save_fiat;
|
|
Loading…
Add table
Reference in a new issue