dev #160

Merged
sora merged 97 commits from dev into master 2025-03-02 01:09:30 +01:00
Showing only changes of commit 5a45dcb1b9 - Show all commits

View File

@ -1,44 +1,28 @@
module.exports = (fastify, opts, done) => { import { FastifyInstance } from 'fastify/types/instance';
fastify.get("/flight/:id", async (req, reply) => { import { ProxyAgent, setGlobalDispatcher } from 'undici';
if (req.params.id) {
const url = new URL("https://www.flightradar24.com/v1/search/web/find");
url.searchParams.append('format', '-')
url.searchParams.append('query', req.params.id)
url.searchParams.append('limit', 16)
url.searchParams.append('type', 'schedule')
fetch(url).then((res) => reply.send(res.json()));
} else {
return reply.send([]);
}
return reply;
});
fastify.get("/place/:id", async (req, reply) => {
if (req.params.id) {
const url = new URL("https://nominatim.openstreetmap.org/search");
url.searchParams.append('format', 'jsonv2')
url.searchParams.append('q', req.params.id)
let bb = JSON.parse(req.query.bb) import { flight_get_data } from './api_flight'
if(bb){ import { nominatim_get_data } from './api_nominatim';
url.searchParams.append('viewbox', `${bb[0][0]},${bb[0][1]},${bb[1][0]},${bb[1][1]}`)
url.searchParams.append('bounded', 1)
}
fetch(url).then((res) => {
if( !res.ok) throw new Error("Nominatim Error")
return res.json()
}).then(res=>{
reply.send(res)});
} else {
return reply.send([]);
}
return reply;
});
fastify.get("/gpx/:id", async (req, reply) => { setGlobalDispatcher(new ProxyAgent(process.env.HTTPS_PROXY as string));
export default function (server, opts, done) {
server.get("/flight/:id", async (req, reply) =>
flight_get_data(req.params.id)
.then(res => reply.send(res))
);
server.get("/place/:id", async (req, reply) =>
nominatim_get_data(req.params.id, JSON.parse(req.query.bb))
.then(res => reply.send(res))
);
server.get("/gpx/:id", async (req, reply) => {
if (req.params.id == undefined) if (req.params.id == undefined)
return reply.code(400).send({ error: "No ID query parameter" }); return reply.code(400).send({ error: "No ID query parameter" });
fastify.level.db.get(req.params.id, (err, val) => { server.level.db.get(req.params.id, (err, val) => {
if (err) { if (err) {
console.warn(err); console.warn(err);
reply.code(500).send(); reply.code(500).send();
@ -48,12 +32,12 @@ module.exports = (fastify, opts, done) => {
const gen_wpt = (name, desc, latlon, icon = "Flag") => `<wpt lat="${latlon[0]}" lon="${latlon[1]}"><ele>0</ele><name>${name}</name><cmt>-</cmt><desc>${desc}</desc><sym>${icon}</sym></wpt>` const gen_wpt = (name, desc, latlon, icon = "Flag") => `<wpt lat="${latlon[0]}" lon="${latlon[1]}"><ele>0</ele><name>${name}</name><cmt>-</cmt><desc>${desc}</desc><sym>${icon}</sym></wpt>`
const esc_str = (str) => (str || "Undefined").replace('"', "&quot;").replace("'", "&apos;").replace("<", "&lt;").replace(">", "&gt;").replace("&", "&amp;").replace("\n", "...") const esc_str = (str) => (str || "Undefined").replace('"', "&quot;").replace("'", "&apos;").replace("<", "&lt;").replace(">", "&gt;").replace("&", "&amp;").replace("\n", "...")
data.main.forEach(a => { data.main.forEach(a => {
file += gen_wpt(esc_str(a.hotel.name), esc_str(a.hotel.notes), a.hotel.latlon, icon = "Hotel"); file += gen_wpt(esc_str(a.hotel.name), esc_str(a.hotel.notes), a.hotel.latlon, "Hotel");
a.places.restaurants.forEach(b => { a.places.restaurants.forEach(b => {
file += gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, icon = "Restaurant"); file += gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, "Restaurant");
}); });
a.places.activities.forEach(b => { a.places.activities.forEach(b => {
file += gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, icon = "Tree"); file += gen_wpt(esc_str(b.name), esc_str(b.notes), b.latlon, "Tree");
}); });
}); });
file += "</gpx>"; file += "</gpx>";
@ -65,11 +49,11 @@ module.exports = (fastify, opts, done) => {
return reply; return reply;
}); });
fastify.get("/:id", async (req, reply) => { server.get("/:id", async (req, reply) => {
if (req.params.id == undefined) if (req.params.id == undefined)
return reply.code(400).send({ error: "No ID query parameter" }); return reply.code(400).send({ error: "No ID query parameter" });
fastify.level.db.get(req.params.id, (err, val) => { server.level.db.get(req.params.id, (err, val) => {
if (err) { if (err) {
console.warn(err); console.warn(err);
reply.code(500).send(); reply.code(500).send();
@ -80,11 +64,11 @@ module.exports = (fastify, opts, done) => {
return reply; return reply;
}); });
fastify.post("/:id", async (req, reply) => { server.post("/:id", async (req, reply) => {
if (req.params.id == undefined) if (req.params.id == undefined)
return reply.code(400).send({ error: "No ID query parameter" }); return reply.code(400).send({ error: "No ID query parameter" });
fastify.level.db.put(req.params.id, req.body, (err) => { server.level.db.put(req.params.id, req.body, (err) => {
if (err) { if (err) {
console.warn(err); console.warn(err);
reply.code(500).send({ error: "Error with DB" }); reply.code(500).send({ error: "Error with DB" });