workadventure/back/src/Controller/MapController.ts

71 lines
2.5 KiB
TypeScript
Raw Normal View History

import {OK} from "http-status-codes";
import {URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
2020-09-28 18:52:54 +02:00
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
import {BaseController} from "./BaseController";
2020-10-13 15:12:24 +02:00
import {parse} from "query-string";
import {adminApi} from "../Services/AdminApi";
2020-09-28 15:02:37 +02:00
//todo: delete this
2020-09-28 18:52:54 +02:00
export class MapController extends BaseController{
2020-09-28 18:52:54 +02:00
constructor(private App : TemplatedApp) {
super();
this.App = App;
2020-10-13 15:12:24 +02:00
this.getMapUrl();
}
// Returns a map mapping map name to file name of the map
2020-10-13 15:12:24 +02:00
getMapUrl() {
this.App.options("/map", (res: HttpResponse, req: HttpRequest) => {
2020-09-28 18:52:54 +02:00
this.addCorsHeaders(res);
res.end();
});
2020-10-13 15:12:24 +02:00
this.App.get("/map", (res: HttpResponse, req: HttpRequest) => {
2020-09-28 18:52:54 +02:00
res.onAborted(() => {
console.warn('/map request was aborted');
})
2020-10-13 15:12:24 +02:00
const query = parse(req.getQuery());
if (typeof query.organizationSlug !== 'string') {
console.error('Expected organizationSlug parameter');
res.writeStatus("400 Bad request");
this.addCorsHeaders(res);
res.end("Expected organizationSlug parameter");
2020-10-13 15:12:24 +02:00
}
if (typeof query.worldSlug !== 'string') {
console.error('Expected worldSlug parameter');
res.writeStatus("400 Bad request");
this.addCorsHeaders(res);
res.end("Expected worldSlug parameter");
2020-10-13 15:12:24 +02:00
}
if (typeof query.roomSlug !== 'string' && query.roomSlug !== undefined) {
console.error('Expected only one roomSlug parameter');
res.writeStatus("400 Bad request");
this.addCorsHeaders(res);
res.end("Expected only one roomSlug parameter");
2020-10-13 15:12:24 +02:00
}
(async () => {
try {
const mapDetails = await adminApi.fetchMapDetails(query.organizationSlug as string, query.worldSlug as string, query.roomSlug as string|undefined);
res.writeStatus("200 OK");
this.addCorsHeaders(res);
res.end(JSON.stringify(mapDetails));
} catch (e) {
console.error(e.message || e);
res.writeStatus("500 Internal Server Error")
this.addCorsHeaders(res);
res.end("An error occurred");
}
2020-10-13 15:12:24 +02:00
})();
});
}
}