From 827ac1ff95e4875ac52e47edfea3371ff1843b9e Mon Sep 17 00:00:00 2001 From: Trivernis Date: Thu, 9 Jan 2020 17:13:51 +0100 Subject: [PATCH] Change routes structure - Rename home to HomeRoute - Rename upload to UploadRoute - Remove index.ts - Remove unused imports --- src/app.ts | 23 +++++++++------- src/routes/{home.ts => HomeRoute.ts} | 0 src/routes/{upload.ts => UploadRoute.ts} | 12 ++++---- src/routes/index.ts | 35 ------------------------ 4 files changed, 19 insertions(+), 51 deletions(-) rename src/routes/{home.ts => HomeRoute.ts} (100%) rename src/routes/{upload.ts => UploadRoute.ts} (100%) delete mode 100644 src/routes/index.ts diff --git a/src/app.ts b/src/app.ts index 3c16565..98964fe 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,8 +3,6 @@ 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"); @@ -15,15 +13,13 @@ 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"; import dataaccess from "./lib/dataAccess"; import globals from "./lib/globals"; -import {User} from "./lib/models"; -import routes from "./routes"; -import {UploadRoute} from "./routes/upload"; +import HomeRoute from "./routes/HomeRoute"; +import {UploadRoute} from "./routes/UploadRoute"; const SequelizeStore = require("connect-session-sequelize")(session.Store); const logger = globals.logger; @@ -65,13 +61,10 @@ class App { secret: globals.config.session.secret, store: new SequelizeStore({db: this.sequelize}), }); - const uploadRoute = new UploadRoute(this.publicPath); - await uploadRoute.init(); await this.sequelize.sync({ logging: (msg) => logger.silly(msg)}); this.sequelize.options.logging = (msg) => logger.silly(msg); logger.info("Setting up socket.io"); - await routes.ioListeners(this.io); try { this.io.adapter(socketIoRedis()); } catch (err) { @@ -116,8 +109,18 @@ class App { logger.verbose(`${req.method} ${req.url}`); next(); }); - this.app.use(routes.router); + + // add custom routes + + const uploadRoute = new UploadRoute(this.publicPath); + const homeRoute = new HomeRoute(); + await uploadRoute.init(); + await homeRoute.init(this.io); + + this.app.use("/home", homeRoute.router); this.app.use("/upload", uploadRoute.router); + + // listen for graphql requests this.app.use("/graphql", graphqlHTTP((request, response) => { return { diff --git a/src/routes/home.ts b/src/routes/HomeRoute.ts similarity index 100% rename from src/routes/home.ts rename to src/routes/HomeRoute.ts diff --git a/src/routes/upload.ts b/src/routes/UploadRoute.ts similarity index 100% rename from src/routes/upload.ts rename to src/routes/UploadRoute.ts index ed957f7..54b3d30 100644 --- a/src/routes/upload.ts +++ b/src/routes/UploadRoute.ts @@ -1,14 +1,14 @@ +import * as crypto from "crypto"; import {Router} from "express"; +import * as fileUpload from "express-fileupload"; import {UploadedFile} from "express-fileupload"; -import {User} from "../lib/models"; -import Route from "../lib/Route"; import * as fsx from "fs-extra"; -import globals from "../lib/globals"; -import * as path from "path"; -import * as crypto from "crypto"; -import * as fileUpload from "express-fileupload"; import * as status from "http-status"; +import * as path from "path"; import * as sharp from "sharp"; +import globals from "../lib/globals"; +import {User} from "../lib/models"; +import Route from "../lib/Route"; const dataDirName = "data"; diff --git a/src/routes/index.ts b/src/routes/index.ts deleted file mode 100644 index ac52791..0000000 --- a/src/routes/index.ts +++ /dev/null @@ -1,35 +0,0 @@ -/** - * @author Trivernis - * @remarks - * - * Taken from {@link https://github.com/Trivernis/whooshy} - */ - -import {Router} from "express"; -import {Server} from "socket.io"; - -import HomeRoute from "./home"; -import {UploadRoute} from "./upload"; - -const homeRoute = new HomeRoute(); - -/** - * Namespace to manage the routes of the server. - * Allows easier assignments of graphql endpoints, socket.io connections and routers when - * used with {@link Route}. - */ -namespace routes { - export const router = Router(); - - router.use("/", homeRoute.router); - - /** - * Assigns the io listeners or namespaces to the routes - * @param io - */ - export const ioListeners = async (io: Server) => { - await homeRoute.init(io); - }; -} - -export default routes;