2024-05-29 05:16:02 +00:00
|
|
|
import express from 'express';
|
2024-06-10 18:11:54 +00:00
|
|
|
import { createProxyMiddleware } from 'http-proxy-middleware';
|
2024-05-29 05:16:02 +00:00
|
|
|
|
|
|
|
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
|
2024-06-10 18:11:54 +00:00
|
|
|
app.use("/fileServer", express.static('files')); // Example file file-server
|
|
|
|
|
|
|
|
app.use("/files", createProxyMiddleware({
|
|
|
|
target: config.files_url
|
|
|
|
}));
|
2024-05-29 05:16:02 +00:00
|
|
|
|
2024-06-05 07:30:05 +00:00
|
|
|
app.get('/data/:lang/events/:floor', async (req, res) => {
|
2024-05-29 05:16:02 +00:00
|
|
|
// Get events
|
2024-06-10 18:11:54 +00:00
|
|
|
let eventsReq = await fetch(new URL(`./events/${req.params.floor}.json`, config.data_url));
|
2024-05-29 05:16:02 +00:00
|
|
|
let events;
|
|
|
|
if (eventsReq.ok)
|
|
|
|
events = await eventsReq.json();
|
|
|
|
else
|
|
|
|
return res.status(400).send("Bad Request");
|
|
|
|
|
|
|
|
// Merge events and localization
|
2024-06-08 08:57:38 +00:00
|
|
|
const lang = req.params.lang;
|
2024-05-29 05:16:02 +00:00
|
|
|
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
|
2024-06-10 18:11:54 +00:00
|
|
|
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) => {
|
2024-06-10 18:11:54 +00:00
|
|
|
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;
|
|
|
|
|
2024-06-08 08:57:38 +00:00
|
|
|
delete event.lang;
|
|
|
|
return event;
|
2024-05-29 05:16:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
2024-06-10 18:11:54 +00:00
|
|
|
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();
|
2024-05-29 05:16:02 +00:00
|
|
|
else
|
|
|
|
return res.status(400).send("Bad Request");
|
|
|
|
|
2024-06-09 12:23:14 +00:00
|
|
|
// Merge floors and localization
|
2024-06-08 08:57:38 +00:00
|
|
|
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;
|
2024-05-29 05:16:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return res.send(merged);
|
|
|
|
})
|
|
|
|
|
2024-06-05 07:30:05 +00:00
|
|
|
app.get('/data/:lang/rooms/:floor', async (req, res) => {
|
2024-05-29 05:16:02 +00:00
|
|
|
// Get rooms
|
2024-06-05 07:30:05 +00:00
|
|
|
let roomsReq = await fetch(new URL(`rooms/${req.params.floor}.json`, config.data_url));
|
2024-05-29 05:16:02 +00:00
|
|
|
let rooms;
|
|
|
|
if (roomsReq.ok)
|
|
|
|
rooms = await roomsReq.json();
|
|
|
|
else
|
|
|
|
return res.status(400).send("Bad Request");
|
|
|
|
|
|
|
|
// Merge rooms and localization
|
2024-06-08 08:57:38 +00:00
|
|
|
const lang = req.params.lang;
|
2024-05-29 05:16:02 +00:00
|
|
|
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 ?? "";
|
2024-06-08 08:57:38 +00:00
|
|
|
delete room.lang;
|
|
|
|
return room;
|
2024-05-29 05:16:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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()}`)
|
2024-06-08 08:57:38 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
export default app;
|