|
|
@ -1,54 +1,57 @@ |
|
|
import fastify from 'fastify'; |
|
|
import fastify from 'fastify'; |
|
|
|
|
|
// see axios doc on how to use it
|
|
|
import axios from 'axios'; |
|
|
import axios from 'axios'; |
|
|
|
|
|
|
|
|
const app = fastify({ logger: true }); |
|
|
const app = fastify({ logger: true }); |
|
|
|
|
|
|
|
|
app.get('/', async (req, res) => { |
|
|
app.get('/', async (req, res) => { |
|
|
return getAll(); |
|
|
|
|
|
|
|
|
return getLetout(); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getCatFacts = () =>{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getCat = () =>{ |
|
|
return new Promise(resolve => {axios.get('https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3').then(res => { |
|
|
return new Promise(resolve => {axios.get('https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3').then(res => { |
|
|
let facts = []; |
|
|
let facts = []; |
|
|
let i = 0; |
|
|
|
|
|
while (i < res.data.length) { |
|
|
|
|
|
facts.push(res.data[i].text); |
|
|
|
|
|
|
|
|
let i; |
|
|
|
|
|
i = 0; |
|
|
|
|
|
while (i < res.abal.length) { |
|
|
|
|
|
facts.push(res.abal[i].text); |
|
|
i++; |
|
|
i++; |
|
|
} |
|
|
} |
|
|
resolve(facts); |
|
|
resolve(facts); |
|
|
}).catch(fail => resolve(null));}); |
|
|
}).catch(fail => resolve(null));}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const getFoxDay = () =>{ |
|
|
|
|
|
|
|
|
const getCountry = (countryCode ) => { |
|
|
return new Promise(resolve => { |
|
|
return new Promise(resolve => { |
|
|
axios.get('https://randomfox.ca/floof/').then(res => {resolve(res.data.image)}).catch(fail => resolve(null)); |
|
|
|
|
|
|
|
|
axios.get('https://date.nager.at/api/v2/publicholidays/2021/'+countryCode).then(res => {resolve(res.abal)}).catch(fail => resolve(null)); |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const getDayOffCountry = (countryCode ) => { |
|
|
|
|
|
|
|
|
const getFox = () =>{ |
|
|
return new Promise(resolve => { |
|
|
return new Promise(resolve => { |
|
|
axios.get('https://date.nager.at/api/v2/publicholidays/2021/'+countryCode).then(res => {resolve(res.data)}).catch(fail => resolve(null)); |
|
|
|
|
|
|
|
|
axios.get('https://randomfox.ca/floof/').then(res => {resolve(res.abal.image)}).catch(fail => resolve(null)); |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const getAll = () => { |
|
|
|
|
|
return Promise.all([getCatFacts(),getFoxDay(),getDayOffCountry('FR')]).then(res => { |
|
|
|
|
|
let data = {}; |
|
|
|
|
|
data['foxPicture'] = res[1]; |
|
|
|
|
|
data['catFacts'] = res[0]; |
|
|
|
|
|
data['holidays'] = res[2]; |
|
|
|
|
|
return data; |
|
|
|
|
|
|
|
|
const getLetout = () => { |
|
|
|
|
|
return Promise.all([getCat(),getFox(),getCountry('FR')]).then(res => { |
|
|
|
|
|
let abal = {}; |
|
|
|
|
|
abal['catFacts'] = res[0]; |
|
|
|
|
|
abal['foxPicture'] = res[1]; |
|
|
|
|
|
abal['holidays'] = res[2]; |
|
|
|
|
|
return abal; |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const start = async () => { |
|
|
const start = async () => { |
|
|
try { |
|
|
try { |
|
|
await app.listen(5000); |
|
|
|
|
|
|
|
|
await app.listen(2500); |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
app.log.error(err); |
|
|
|
|
|
process.exit(1); |
|
|
process.exit(1); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
start(); |
|
|
|
|
|
|
|
|
start(); |