2022-07-15 21:42:51 +03:00
|
|
|
import cors from "cors";
|
|
|
|
|
import express from "express";
|
|
|
|
|
import { Request, Response } from "express";
|
|
|
|
|
import morgan from "morgan";
|
|
|
|
|
import fs from "fs";
|
|
|
|
|
import { generateKey } from "../util/key_factory";
|
2022-07-30 16:14:37 +03:00
|
|
|
import { GameStore } from "../game/gamestore/GameStore";
|
2022-07-15 21:42:51 +03:00
|
|
|
|
|
|
|
|
export class ExpressApp {
|
2022-07-16 17:42:49 +03:00
|
|
|
app: Express.Application;
|
2022-07-30 16:14:37 +03:00
|
|
|
gameStore: GameStore;
|
|
|
|
|
constructor(params: { gameStore: GameStore }) {
|
|
|
|
|
this.gameStore = params.gameStore;
|
2022-07-16 17:42:49 +03:00
|
|
|
this.app = this.createExpressApp();
|
|
|
|
|
}
|
2022-07-15 21:42:51 +03:00
|
|
|
private createAccessLogMiddleware() {
|
|
|
|
|
return morgan("common", {
|
|
|
|
|
stream: fs.createWriteStream("./access.log", { flags: "a" }),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
public createExpressApp(): Express.Application {
|
|
|
|
|
const app = express();
|
|
|
|
|
app.use(cors());
|
|
|
|
|
app.use(this.createAccessLogMiddleware());
|
|
|
|
|
app.use(morgan("dev"));
|
|
|
|
|
app.get("/", (req: Request, res: Response) => {
|
2024-12-21 22:25:47 +03:00
|
|
|
res.send("Server up and running! ci test1");
|
2022-07-15 21:42:51 +03:00
|
|
|
});
|
|
|
|
|
app.get("/register/", (req: Request, res: Response) => {
|
|
|
|
|
res.send(generateKey());
|
|
|
|
|
});
|
2022-07-30 16:14:37 +03:00
|
|
|
|
|
|
|
|
app.get("/game/:id", (req: Request, res: Response) => {
|
|
|
|
|
const gameId = req.params.id;
|
|
|
|
|
const game = this.gameStore.get(gameId);
|
|
|
|
|
if(game) {
|
|
|
|
|
res.send(game)
|
|
|
|
|
} else {
|
|
|
|
|
res.sendStatus(404)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-15 21:42:51 +03:00
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
}
|