EventMapper/scripts/index.js

162 lines
4.3 KiB
JavaScript
Raw Normal View History

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
}));
2024-06-05 07:30:05 +00:00
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 => {
2024-06-09 12:23:14 +00:00
const curLang = event.lang[lang] ?? event.lang[config.default_language];
event.name = curLang.name ?? "";
event.description = curLang.description ?? "";
2024-06-10 17:37:53 +00:00
if (curLang.host != null)
event.host = curLang.host;
2024-06-09 12:23:14 +00:00
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));
2024-06-09 12:23:14 +00:00
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));
2024-06-09 12:23:14 +00:00
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 ?? "";
2024-06-10 17:37:53 +00:00
if (curLang.host != null)
event.host = curLang.host;
delete event.lang;
return event;
});
return res.send(merged);
})
2024-06-05 07:30:05 +00:00
app.get('/data/:lang/floors', async (req, res) => {
2024-06-09 12:23:14 +00:00
// Get floors
let floorsReq = await fetch(new URL("./floors.json", config.data_url));
2024-06-09 12:23:14 +00:00
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");
2024-06-09 12:23:14 +00:00
// Merge floors and localization
const lang = req.params.lang;
2024-06-09 12:23:14 +00:00
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);
})
2024-06-05 07:30:05 +00:00
app.get('/data/:lang/rooms/:floor', async (req, res) => {
// Get rooms
2024-06-05 07:30:05 +00:00
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 => {
2024-06-09 12:23:14 +00:00
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;