chore: Services loading has been redone, now in config file you can write which services will be loaded. You can also customize API token and base url
This commit is contained in:
parent
6125f99ec6
commit
f6b21ed84d
3 changed files with 28 additions and 15 deletions
|
@ -10,6 +10,15 @@
|
||||||
fiat: true
|
fiat: true
|
||||||
crypto: false
|
crypto: false
|
||||||
}
|
}
|
||||||
|
services: {
|
||||||
|
enabled: [
|
||||||
|
'coinmarketcap'
|
||||||
|
]
|
||||||
|
coinmarketcap: {
|
||||||
|
"api_key": TOKEN_COINMARKETCAP"
|
||||||
|
"base_url": https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest
|
||||||
|
}
|
||||||
|
}
|
||||||
fiat:
|
fiat:
|
||||||
[
|
[
|
||||||
USD,
|
USD,
|
||||||
|
|
24
main.js
24
main.js
|
@ -7,26 +7,28 @@ const { validateCurrency } = require('./models/Currency.js');
|
||||||
const { create_table, pool } = require('./database/data.js');
|
const { create_table, pool } = require('./database/data.js');
|
||||||
const config = require('./utils/load_config.js')();
|
const config = require('./utils/load_config.js')();
|
||||||
|
|
||||||
|
const services = [];
|
||||||
|
const servicesDir = path.join(__dirname, 'services');
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
if (!config['schedule'])
|
if (!config['schedule'])
|
||||||
throw new Error('The crontab schedule is not set.');
|
throw new Error('The crontab schedule is not set.');
|
||||||
else if (!cron.isValidCron(config['schedule'], { alias: true }))
|
else if (!cron.isValidCron(config['schedule'], { alias: true }))
|
||||||
throw new Error('The crontab is invalid.');
|
throw new Error('The crontab is invalid.');
|
||||||
|
|
||||||
const servicesDir = path.join(__dirname, 'services');
|
console.log('Loading services...');
|
||||||
const serviceFiles = fs.readdirSync(servicesDir)
|
config['currency']['services']['enabled'].forEach(serviceName => {
|
||||||
.filter(filename => filename.endsWith('.js'));
|
const servicePath = path.join(servicesDir, `${serviceName}.js`);
|
||||||
|
if (fs.existsSync(servicePath)) {
|
||||||
|
const serviceModule = require(servicePath);
|
||||||
|
|
||||||
const services = [];
|
services.push(serviceModule);
|
||||||
for (const file of serviceFiles) {
|
console.log(`Service ${serviceName} loaded successfully`);
|
||||||
const filePath = path.join(servicesDir, file);
|
} else {
|
||||||
const moduleLoaded = require(filePath);
|
console.error(`Service file for ${serviceName} not found at ${servicePath}`);
|
||||||
|
|
||||||
if (typeof moduleLoaded.parseCurrencies === 'function')
|
|
||||||
services.push(moduleLoaded);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
console.log('Loaded parser services:', serviceFiles);
|
|
||||||
await create_table();
|
await create_table();
|
||||||
|
|
||||||
schedule.scheduleJob(config['schedule'], async () => {
|
schedule.scheduleJob(config['schedule'], async () => {
|
||||||
|
|
|
@ -8,15 +8,17 @@ module.exports = {
|
||||||
return config['currency']['crypto'].map(convCurrency => {
|
return config['currency']['crypto'].map(convCurrency => {
|
||||||
if (fromCurrency === convCurrency) return Promise.resolve(null);
|
if (fromCurrency === convCurrency) return Promise.resolve(null);
|
||||||
|
|
||||||
|
const coinmarketcap = config['currency']['services']['coinmarketcap'];
|
||||||
|
|
||||||
return axios.get(
|
return axios.get(
|
||||||
'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest',
|
coinmarketcap['base_url'],
|
||||||
{
|
{
|
||||||
params: {
|
params: {
|
||||||
symbol: fromCurrency,
|
symbol: fromCurrency,
|
||||||
convert: convCurrency,
|
convert: convCurrency,
|
||||||
},
|
},
|
||||||
headers: {
|
headers: {
|
||||||
'X-CMC_PRO_API_KEY': config['currency']['api_keys']['coinmarketcap'],
|
'X-CMC_PRO_API_KEY': coinmarketcap['api_key'],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -33,7 +35,7 @@ module.exports = {
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err.respone.data);
|
console.error(err);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Reference in a new issue