From 5a45dcb1b99295328d18d7da075dc065c6d64a00 Mon Sep 17 00:00:00 2001 From: sora-ext Date: Tue, 25 Feb 2025 16:10:18 +0100 Subject: [PATCH] Update src/server/api.ts --- src/server/{api.js => api.ts} | 70 ++++++++++++++--------------------- 1 file changed, 27 insertions(+), 43 deletions(-) rename src/server/{api.js => api.ts} (57%) diff --git a/src/server/api.js b/src/server/api.ts similarity index 57% rename from src/server/api.js rename to src/server/api.ts index c7c421a..afe855b 100644 --- a/src/server/api.js +++ b/src/server/api.ts @@ -1,44 +1,28 @@ -module.exports = (fastify, opts, done) => { - fastify.get("/flight/:id", async (req, reply) => { - 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) +import { FastifyInstance } from 'fastify/types/instance'; +import { ProxyAgent, setGlobalDispatcher } from 'undici'; - let bb = JSON.parse(req.query.bb) - if(bb){ - 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; - }); +import { flight_get_data } from './api_flight' +import { nominatim_get_data } from './api_nominatim'; - 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) 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) { console.warn(err); reply.code(500).send(); @@ -48,12 +32,12 @@ module.exports = (fastify, opts, done) => { const gen_wpt = (name, desc, latlon, icon = "Flag") => `0${name}-${desc}${icon}` const esc_str = (str) => (str || "Undefined").replace('"', """).replace("'", "'").replace("<", "<").replace(">", ">").replace("&", "&").replace("\n", "...") 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 => { - 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 => { - 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 += ""; @@ -65,11 +49,11 @@ module.exports = (fastify, opts, done) => { return reply; }); - fastify.get("/:id", async (req, reply) => { + server.get("/:id", async (req, reply) => { if (req.params.id == undefined) 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) { console.warn(err); reply.code(500).send(); @@ -80,11 +64,11 @@ module.exports = (fastify, opts, done) => { return reply; }); - fastify.post("/:id", async (req, reply) => { + server.post("/:id", async (req, reply) => { if (req.params.id == undefined) 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) { console.warn(err); reply.code(500).send({ error: "Error with DB" });