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' };
});
}
async function fetchSitesFromAPI() {
try {
const response = await axios.get('https://mastodon.social/api/v1/instance/peers');
return response.data;
} catch (error) {
console.error('Erro ao buscar sites da API:', error.message);
return [];
}
}
async function checkAllSitesStatus() {
const sites = await fetchSitesFromAPI();
if (sites.length === 0) {
console.log('Nenhum site encontrado na API.');
return;
class InstanceChecker {
constructor() {
this.axiosInstance = axios.create();
}
const results = [];
for (const site of sites) {
async checkInstanceStatus(instance) {
try {
const result = await checkSiteStatus(site);
console.log(`${result.site} está ${result.status}.`);
results.push(result);
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) {
console.error(`Erro ao verificar o status de ${site}:`, error.message);
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 peers da API:', error.message);
return [];
}
}
async checkPeersStatus() {
const peers = await this.fetchPeersFromAPI();
if (peers.length === 0) {
console.log('Nenhum peer encontrado na API.');
return;
}
const results = [];
for (const peer of peers) {
try {
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 ${peer}:`, error.message);
}
}
}
}
checkAllSitesStatus();
const instanceChecker = new InstanceChecker();
instanceChecker.checkPeersStatus();