mancala/apps/backend/src/server/ExpressApp.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

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;
legacyUserKeyAuth: boolean;
constructor(params: { gameStore: GameStore, legacyUserKeyAuth: boolean }) {
2022-07-30 16:14:37 +03:00
this.gameStore = params.gameStore;
this.legacyUserKeyAuth = params.legacyUserKeyAuth;
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
});
// Issues userKeys for legacy clients; token clients get their identity from Keycloak.
if (this.legacyUserKeyAuth) {
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;
}
}