import bodyParser = require('body-parser');
import * as http from "http";
import {MapController} from "./Controller/MapController";
-import {AuthenticateMiddleware} from "./Middleware/AuthenticateMiddleware";
class App {
public app: Application;
public server: http.Server;
public ioSocketController: IoSocketController;
public authenticateController: AuthenticateController;
- //public AuthenticateMiddleware: AuthenticateMiddleware;
public mapController: MapController;
constructor() {
this.app = express();
//config server http
- this.config();
this.server = http.createServer(this.app);
+ this.config();
+ this.crossOrigin();
+
+ //TODO add middleware with access token to secure api
+
//create socket controllers
this.ioSocketController = new IoSocketController(this.server);
this.authenticateController = new AuthenticateController(this.app);
- //this.AuthenticateMiddleware = new AuthenticateMiddleware(this.app);
this.mapController = new MapController(this.app);
}
private config(): void {
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({extended: false}));
+ }
+
+ private crossOrigin(){
this.app.use((req: Request, res: Response, next) => {
- res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
- res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
+ res.setHeader("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
+ // Request methods you wish to allow
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
+ // Request headers you wish to allow
+ res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
}
import express from "express";
-import path from "path";
import {Application, Request, Response} from "express";
import {OK} from "http-status-codes";
import {ROOM_STARTED, ROOMS, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
constructor(App: Application) {
this.App = App;
- this.getMpas();
+ this.getMaps();
this.assetMaps();
}
+++ /dev/null
-import {Application, Request, Response} from "express";
-import {BAD_REQUEST} from "http-status-codes";
-import Jwt, {JsonWebTokenError} from "jsonwebtoken";
-import {SECRET_KEY} from "../Enum/EnvironmentVariable";
-
-export class AuthenticateMiddleware{
- App: Application;
-
- constructor(App: Application) {
- this.App = App;
- this.tokenVerification();
- }
-
- tokenVerification() {
- this.App.use((req: Request, res: Response, next: any) => {
- let token = req.header("Access-Token");
- if (!token) {
- return res.status(BAD_REQUEST).send({
- message: "you must to be connected to get the map"
- });
- }
- return Jwt.verify(token, SECRET_KEY, (err: JsonWebTokenError, tokenDecoded: object) => {
- if (err) {
- return res.status(BAD_REQUEST).send({
- message: "you must to be connected to get the map"
- });
- }
- return next();
- });
- })
- }
-}
const SocketIo = require('socket.io-client');
import Axios from "axios";
-import {API_URL, ROOM} from "./Enum/EnvironmentVariable";
+import {API_URL} from "./Enum/EnvironmentVariable";
enum EventMessage{
WEBRTC_SIGNAL = "webrtc-signal",
throw err;
});
}
-
- loadMaps() : Promise<any>{
- return Axios.get(`${API_URL}/maps`).then((res) => {
- return res.data;
- }).catch((err) => {
- console.error(err);
- throw err;
- });
+
+ //TODO add middleware with access token to secure api
+ loadMaps() : Promise<any> {
+ return Axios.get(`${API_URL}/maps`)
+ .then((res) => {
+ return res.data;
+ }).catch((err) => {
+ console.error(err);
+ throw err;
+ });
}
/**
}
private async login(name: string) {
- Promise.all([
- gameManager.connect(name, this.selectedPlayer.texture.key),
- gameManager.loadMaps()
- ]).then((data) => {
- if (!data) {
- return;
- }
- let scene: any = data[1];
- let game = new GameScene(scene.mapStart.key, `${API_URL}${scene.mapStart.url}`);
- this.scene.add(scene.mapStart.key, game, false);
- this.scene.start(scene.mapStart.key);
+ return gameManager.connect(name, this.selectedPlayer.texture.key).then(() => {
+ return gameManager.loadMaps().then((scene : any) => {
+ if (!scene) {
+ return;
+ }
+ let game = new GameScene(scene.mapStart.key, `${API_URL}${scene.mapStart.url}`);
+ this.scene.add(scene.mapStart.key, game, false);
+ this.scene.start(scene.mapStart.key);
+ return scene;
+ }).catch((err) => {
+ console.error(err);
+ throw err;
+ });
}).catch((err) => {
console.error(err);
throw err;