import express from 'express'; 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.get('/data/:lang/events', async (req, res) => { // Get events let eventsReq = await fetch(new URL("events/events.json", config.data_url)); let events; if (eventsReq.ok) events = await eventsReq.json(); else return res.status(400).send("Bad Request"); // Get localization let l10nReq = await fetch(new URL(`localization/${req.params.lang}.json`, config.data_url)); let l10n; if (l10nReq.ok) l10n = await l10nReq.json(); else return res.status(404).send("Localization not found!"); // Merge events and localization const merged = events.map(event => { const localizationKey = l10n.__events[event.id]; return { ...event, ...localizationKey }; }); return res.send(merged); }) app.get('/data/:lang/events/:layer', async (req, res) => { // Get events let eventsReq = await fetch(new URL("events/events.json", config.data_url)); let events; if (eventsReq.ok) events = await eventsReq.json(); else return res.status(400).send("Bad Request"); // Get localization let l10nReq = await fetch(new URL(`localization/${req.params.lang}.json`, config.data_url)); let l10n; if (l10nReq.ok) l10n = await l10nReq.json(); else return res.status(404).send("Localization not found!"); const clippedEvents = events.filter((event) => event.where.layer == req.params.layer).map((event) => { event.where = event.where.room; return event; }); // Merge events and localization const merged = clippedEvents.map(event => { const localizationKey = l10n.__events[event.id]; return { ...event, ...localizationKey }; }); return res.send(merged); }) app.get('/data/:lang/map', async (req, res) => { // Get layers let layersReq = await fetch(new URL("map/layers.json", config.data_url)); let layers; if (layersReq.ok) layers = await layersReq.json(); else return res.status(400).send("Bad Request"); // Get localization let l10nReq = await fetch(new URL(`localization/${req.params.lang}.json`, config.data_url)); let l10n; if (l10nReq.ok) l10n = await l10nReq.json(); else return res.status(404).send("Localization not found!"); // Merge layers and localization const merged = layers.map(layer => { const localizationKey = l10n.__layers[layer.id]; return { ...layer, ...localizationKey }; }); return res.send(merged); }) app.get('/data/:lang/map/:layer', async (req, res) => { // Get rooms let roomsReq = await fetch(new URL(`map/${req.params.layer}.json`, config.data_url)); let rooms; if (roomsReq.ok) rooms = await roomsReq.json(); else return res.status(400).send("Bad Request"); // Get localization let l10nReq = await fetch(new URL(`localization/${req.params.lang}.json`, config.data_url)); let l10n; if (l10nReq.ok) l10n = await l10nReq.json(); else return res.status(404).send("Localization not found!"); // Merge rooms and localization const merged = rooms.map(room => { const localizationKey = l10n.__rooms[room.id]; return { ...room, ...localizationKey }; }); 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()}`) })