Convertido código para classe

This commit is contained in:
Luiz F Picolo 2023-06-13 15:17:12 -04:00
parent eead1cca3d
commit 0c55cc3a27

View File

@ -1,49 +1,56 @@
const axios = require('axios');
function checkSiteStatus(site) {
return Promise.race([
axios.get(`https://${site}/api/v1/instance`),
new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('Tempo de espera excedido')), 30000);
})
])
.then(response => {
return { site, status: 'online' };
})
.catch(error => {
return { site, status: 'offline' };
});
class InstanceChecker {
constructor() {
this.axiosInstance = axios.create();
}
async function fetchSitesFromAPI() {
async checkInstanceStatus(instance) {
try {
const response = await axios.get('https://mastodon.social/api/v1/instance/peers');
const response = await Promise.race([
this.axiosInstance.get(`https://${instance}/api/v1/instance`),
new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('Tempo de espera excedido')), 6000);
})
]);
return { instance, status: true };
} catch (error) {
return { instance, status: false };
}
}
async fetchPeersFromAPI() {
try {
const response = await this.axiosInstance.get('https://mastodon.social/api/v1/instance/peers');
return response.data;
} catch (error) {
console.error('Erro ao buscar sites da API:', error.message);
console.error('Erro ao buscar peers da API:', error.message);
return [];
}
}
async function checkAllSitesStatus() {
const sites = await fetchSitesFromAPI();
async checkPeersStatus() {
const peers = await this.fetchPeersFromAPI();
if (sites.length === 0) {
console.log('Nenhum site encontrado na API.');
if (peers.length === 0) {
console.log('Nenhum peer encontrado na API.');
return;
}
const results = [];
for (const site of sites) {
for (const peer of peers) {
try {
const result = await checkSiteStatus(site);
console.log(`${result.site} está ${result.status}.`);
const result = await this.checkInstanceStatus(peer);
console.log(`${result.instance} está ${result.status}.`);
results.push(result);
} catch (error) {
console.error(`Erro ao verificar o status de ${site}:`, error.message);
console.error(`Erro ao verificar o status de ${peer}:`, error.message);
}
}
}
}
checkAllSitesStatus();
const instanceChecker = new InstanceChecker();
instanceChecker.checkPeersStatus();