OTM/router/api.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-07-16 09:12:30 +02:00
const axios = require('axios');
module.exports = (fastify, opts, done) => {
fastify.get('/flight/:id', async (req,reply) => {
const ENDPOINT = 'https://www.flightradar24.com/v1/search/web/find';
const FORMAT = '-';
if(req.params.id){
axios.get(ENDPOINT,{
params: {
format: FORMAT,
query: req.params.id,
limit:16,
type: 'schedule'
}
2021-07-16 12:17:32 +02:00
}).then(res=>reply.send(res.data));
}else{
2023-06-14 15:34:42 +02:00
return reply.send([]);
2021-07-16 12:17:32 +02:00
}
2023-06-14 15:34:42 +02:00
return reply
2021-07-16 12:17:32 +02:00
});
fastify.get('/place/:id', async (req,reply) => {
const ENDPOINT = 'https://nominatim.openstreetmap.org/';
const FORMAT = 'jsonv2';
if(req.params.id){
axios.get(ENDPOINT,{
params: {
format: FORMAT,
q: req.params.id,
}
}).then(res=>reply.send(res.data));
2021-07-16 09:12:30 +02:00
}else{
2023-06-14 15:34:42 +02:00
return reply.send([]);
2021-07-16 09:12:30 +02:00
}
2023-06-14 15:34:42 +02:00
return reply;
2021-07-16 09:12:30 +02:00
});
2021-07-16 12:12:05 +02:00
fastify.get('/:id', async (req, reply) => {
2023-06-14 15:34:42 +02:00
if(req.params.id == undefined)
2021-07-16 12:12:05 +02:00
return reply.code(400).send({error:"No ID query parameter"});
2021-07-16 09:12:30 +02:00
2023-06-14 15:34:42 +02:00
fastify.level.db.get(req.params.id, (err, val) => {
2021-07-16 12:12:05 +02:00
if(err){
console.warn(err);
2023-06-25 12:26:17 +02:00
reply.code(500).send();
2021-07-16 12:12:05 +02:00
} else {
reply.send(JSON.parse(val));
}
});
2023-06-14 15:34:42 +02:00
return reply
2021-07-16 09:12:30 +02:00
});
2021-07-16 12:12:05 +02:00
fastify.post('/:id', async (req, reply) => {
2023-06-14 15:34:42 +02:00
if(req.params.id == undefined)
2021-07-16 12:12:05 +02:00
return reply.code(400).send({error:"No ID query parameter"});
2023-06-14 15:34:42 +02:00
fastify.level.db.put(req.params.id, JSON.stringify(req.body), (err) => {
2021-07-16 12:12:05 +02:00
if(err){
console.warn(err);
reply.code(500).send({error:"Error with DB"});
} else {
reply.send({content:"ok"});
}
});
2023-06-14 15:34:42 +02:00
return reply
2021-07-16 09:12:30 +02:00
});
done();
};