|
|
|
@ -3,6 +3,8 @@ import * as cookieParser from "cookie-parser";
|
|
|
|
|
import * as cors from "cors";
|
|
|
|
|
import {Request, Response} from "express";
|
|
|
|
|
import * as express from "express";
|
|
|
|
|
import {UploadedFile} from "express-fileupload";
|
|
|
|
|
import * as fileUpload from "express-fileupload";
|
|
|
|
|
import * as graphqlHTTP from "express-graphql";
|
|
|
|
|
import * as session from "express-session";
|
|
|
|
|
import sharedsession = require("express-socket.io-session");
|
|
|
|
@ -13,6 +15,7 @@ import * as http from "http";
|
|
|
|
|
import * as httpStatus from "http-status";
|
|
|
|
|
import * as path from "path";
|
|
|
|
|
import {Sequelize} from "sequelize-typescript";
|
|
|
|
|
import * as sharp from "sharp";
|
|
|
|
|
import * as socketIo from "socket.io";
|
|
|
|
|
import * as socketIoRedis from "socket.io-redis";
|
|
|
|
|
import {resolver} from "./graphql/resolvers";
|
|
|
|
@ -27,6 +30,7 @@ class App {
|
|
|
|
|
public app: express.Application;
|
|
|
|
|
public io: socketIo.Server;
|
|
|
|
|
public server: http.Server;
|
|
|
|
|
public readonly publicPath: string;
|
|
|
|
|
public readonly id?: number;
|
|
|
|
|
public readonly sequelize: Sequelize;
|
|
|
|
|
|
|
|
|
@ -36,6 +40,10 @@ class App {
|
|
|
|
|
this.server = new http.Server(this.app);
|
|
|
|
|
this.io = socketIo(this.server);
|
|
|
|
|
this.sequelize = new Sequelize(globals.config.database.connectionUri);
|
|
|
|
|
this.publicPath = globals.config.frontend.publicPath;
|
|
|
|
|
if (!path.isAbsolute(this.publicPath)) {
|
|
|
|
|
this.publicPath = path.normalize(path.join(__dirname, this.publicPath));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -62,7 +70,12 @@ class App {
|
|
|
|
|
this.sequelize.options.logging = (msg) => logger.silly(msg);
|
|
|
|
|
logger.info("Setting up socket.io");
|
|
|
|
|
await routes.ioListeners(this.io);
|
|
|
|
|
this.io.adapter(socketIoRedis());
|
|
|
|
|
try {
|
|
|
|
|
this.io.adapter(socketIoRedis());
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(err.message);
|
|
|
|
|
logger.debug(err.stack);
|
|
|
|
|
}
|
|
|
|
|
this.io.use(sharedsession(appSession, {autoSave: true}));
|
|
|
|
|
|
|
|
|
|
logger.info("Configuring express app.");
|
|
|
|
@ -73,7 +86,7 @@ class App {
|
|
|
|
|
this.app.use(compression());
|
|
|
|
|
this.app.use(express.json());
|
|
|
|
|
this.app.use(express.urlencoded({extended: false}));
|
|
|
|
|
this.app.use(express.static(path.join(__dirname, "public")));
|
|
|
|
|
this.app.use(express.static(this.publicPath));
|
|
|
|
|
this.app.use(cookieParser());
|
|
|
|
|
this.app.use(appSession);
|
|
|
|
|
// enable cross origin requests if enabled in the config
|
|
|
|
@ -112,6 +125,29 @@ class App {
|
|
|
|
|
schema: buildSchema(importSchema(path.join(__dirname, "./graphql/schema.graphql"))),
|
|
|
|
|
};
|
|
|
|
|
}));
|
|
|
|
|
this.app.use("/upload", fileUpload());
|
|
|
|
|
this.app.use("/upload", async (req, res) => {
|
|
|
|
|
const profilePic = req.files.profilePicture as UploadedFile;
|
|
|
|
|
let success = false;
|
|
|
|
|
let fileName;
|
|
|
|
|
if (profilePic && req.session.userId) {
|
|
|
|
|
const dir = path.join(this.publicPath, "data/profilePictures");
|
|
|
|
|
await fsx.ensureDir(dir);
|
|
|
|
|
await sharp(profilePic.data)
|
|
|
|
|
.resize(512, 512)
|
|
|
|
|
.normalise()
|
|
|
|
|
.png()
|
|
|
|
|
.toFile(path.join(dir, req.session.userId + ".png"));
|
|
|
|
|
success = true;
|
|
|
|
|
fileName = `/data/profilePictures/${req.session.userId}.png`;
|
|
|
|
|
} else {
|
|
|
|
|
res.status(400);
|
|
|
|
|
}
|
|
|
|
|
res.json({
|
|
|
|
|
fileName,
|
|
|
|
|
success,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
// allow access to cluster information
|
|
|
|
|
this.app.use("/cluster-info", (req: Request, res: Response) => {
|
|
|
|
|
res.json({
|
|
|
|
@ -121,7 +157,7 @@ class App {
|
|
|
|
|
// redirect all request to the angular file
|
|
|
|
|
this.app.use((req: any, res: Response) => {
|
|
|
|
|
if (globals.config.frontend.angularIndex) {
|
|
|
|
|
const angularIndex = path.join(__dirname, globals.config.frontend.angularIndex);
|
|
|
|
|
const angularIndex = path.join(this.publicPath, globals.config.frontend.angularIndex);
|
|
|
|
|
if (fsx.existsSync(path.join(angularIndex))) {
|
|
|
|
|
res.sendFile(angularIndex);
|
|
|
|
|
} else {
|
|
|
|
|