import express from 'express'; import { createProxyMiddleware } from 'http-proxy-middleware'; import config from "../eventmapper_config.json" with { type: "json" }; const app = express(); app.use("/assets", express.static('assets')); app.use("/", express.static('pages')); app.use("/dataServer", express.static('data')); // Example data file-server app.use("/fileServer", express.static('files')); // Example file file-server app.use("/files", createProxyMiddleware({ target: config.files_url })); app.get('/data/:lang/events/:floor', async (req, res) => { // Get events let eventsReq = await fetch(new URL(`./events/${req.params.floor}.json`, config.data_url)); let events; if (eventsReq.ok) events = await eventsReq.json(); else return res.status(400).send("Bad Request"); // Merge events and localization const lang = req.params.lang; const merged = events.map(event => { const curLang = event.lang[lang] ?? event.lang[config.default_language]; event.name = curLang.name ?? ""; event.description = curLang.description ?? ""; if (curLang.host != null) event.host = curLang.host; delete event.lang; return event; }); return res.send(merged); }) app.get('/data/:lang/events/', async (req, res) => { // Get floors let floorsReq = await fetch(new URL("./floors.json", config.data_url)); let floors; if (floorsReq.ok) floors = await floorsReq.json(); else return res.status(400).send("Bad Request"); let allEvents = []; await Promise.allSettled(floors.map(async (curFloor) => { let eventsReq = await fetch(new URL(`./events/${curFloor.id}.json`, config.data_url)); let events; if (eventsReq.ok) events = await eventsReq.json(); else return null; allEvents = allEvents.concat(events); })); // Merge events and localization const lang = req.params.lang; const merged = allEvents.map(event => { const curLang = event.lang[lang] ?? event.lang[config.default_language]; event.name = curLang.name ?? ""; event.description = curLang.description ?? ""; if (curLang.host != null) event.host = curLang.host; delete event.lang; return event; }); return res.send(merged); }) app.get('/data/:lang/floors', async (req, res) => { // Get floors let floorsReq = await fetch(new URL("./floors.json", config.data_url)); let floors; if (floorsReq.ok) floors = await floorsReq.json(); else return res.status(400).send("Bad Request"); // Merge floors and localization const lang = req.params.lang; const merged = floors.map(floor => { const curLang = floor.lang[lang] ?? floor.lang[config.default_language]; floor.name = curLang.name ?? ""; floor.shortName = curLang.shortName ?? ""; floor.description = curLang.description ?? ""; delete floor.lang; return floor; }); return res.send(merged); }) app.get('/data/:lang/tags', async (req, res) => { // Get tags let tagsReq = await fetch(new URL("tags.json", config.data_url)); let tags; if (tagsReq.ok) tags = await tagsReq.json(); else return res.status(400).send("Bad Request"); // Merge floors and localization const lang = req.params.lang; const merged = tags.map(tag => { const curLang = tag.lang[lang] ?? tag.lang[config.default_language]; tag.name = curLang.name ?? ""; tag.shortName = curLang.shortName ?? ""; tag.description = curLang.description ?? ""; delete tag.lang; return tag; }); return res.send(merged); }) app.get('/data/:lang/rooms/:floor', async (req, res) => { // Get rooms let roomsReq = await fetch(new URL(`rooms/${req.params.floor}.json`, config.data_url)); let rooms; if (roomsReq.ok) rooms = await roomsReq.json(); else return res.status(400).send("Bad Request"); // Merge rooms and localization const lang = req.params.lang; const merged = rooms.map(room => { const curLang = room.lang[lang] ?? room.lang[config.default_language]; room.name = curLang.name ?? ""; room.shortName = curLang.shortName ?? ""; room.description = curLang.description ?? ""; delete room.lang; return room; }); return res.send(merged); }) app.listen(config.port, () => { const listeningURL = new URL("http://localhost/"); listeningURL.port = config.port; console.log(`Example app listening on ${listeningURL.toString()}`) }) export default app;