Co-authored-by: sora-ext Co-authored-by: soraefir Reviewed-on: #160
This commit is contained in:
90
src/server/api.ts
Normal file
90
src/server/api.ts
Normal file
@ -0,0 +1,90 @@
|
||||
//import { ProxyAgent, setGlobalDispatcher } from 'undici';
|
||||
|
||||
import { flight_get_data } from './api_flight'
|
||||
import { nominatim_get_data } from './api_nominatim';
|
||||
|
||||
//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 => {
|
||||
let wait_for_all: Promise<any>[] = []
|
||||
res.forEach(r => {
|
||||
wait_for_all.push(nominatim_get_data(r.from).then(geo => (r as any).from_geo = geo[0]));
|
||||
wait_for_all.push(nominatim_get_data(r.to).then(geo => (r as any).to_geo = geo[0]));
|
||||
});
|
||||
return Promise.all(wait_for_all).then(_ => res)
|
||||
})
|
||||
.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" });
|
||||
|
||||
server.level.db.get(req.params.id, (err, val) => {
|
||||
if (err) {
|
||||
console.warn(err);
|
||||
reply.code(500).send();
|
||||
} else {
|
||||
let file = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="EMTAC BTGPS Trine II DataLog Dump 1.0 - http://www.ayeltd.biz" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">'
|
||||
const data = JSON.parse(val);
|
||||
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('"', """).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, "Hotel");
|
||||
a.places.restaurants.forEach(b => {
|
||||
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, "Tree");
|
||||
});
|
||||
});
|
||||
file += "</gpx>";
|
||||
reply.header('Content-Type', 'application/gpx+xml');
|
||||
reply.header('Content-Disposition', `attachment; filename=${req.params.id}.gpx`);
|
||||
reply.send(file);
|
||||
}
|
||||
});
|
||||
return reply;
|
||||
});
|
||||
|
||||
server.get("/:id", async (req, reply) => {
|
||||
if (req.params.id == undefined)
|
||||
return reply.code(400).send({ error: "No ID query parameter" });
|
||||
|
||||
server.level.db.get(req.params.id, (err, val) => {
|
||||
if (err) {
|
||||
console.warn(err);
|
||||
reply.code(500).send();
|
||||
} else {
|
||||
reply.send(JSON.parse(val));
|
||||
}
|
||||
});
|
||||
return reply;
|
||||
});
|
||||
|
||||
server.post("/:id", async (req, reply) => {
|
||||
if (req.params.id == undefined)
|
||||
return reply.code(400).send({ error: "No ID query parameter" });
|
||||
|
||||
server.level.db.put(req.params.id, req.body, (err) => {
|
||||
if (err) {
|
||||
console.warn(err);
|
||||
reply.code(500).send({ error: "Error with DB" });
|
||||
} else {
|
||||
reply.send({ content: "ok" });
|
||||
}
|
||||
});
|
||||
return reply;
|
||||
});
|
||||
|
||||
done();
|
||||
};
|
Reference in New Issue
Block a user