From f7572202f97d5a537ef8744465b3c7d21aa51038 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Fri, 13 Sep 2019 23:18:59 +0200 Subject: [PATCH] Config fix, structure - fixed problem with the config file creation from the default config - changed structure of the home route - added abstract Route class from whooshy to manage all connection types --- CHANGELOG.md | 1 + package-lock.json | 11 +++----- src/app.ts | 1 + src/index.ts | 7 ------ src/lib/QueryHelper.ts | 22 ++++++++++++++++ src/lib/Route.ts | 27 ++++++++++++++++++++ src/lib/globals.ts | 15 +++++++++++ src/routes/home.ts | 57 +++++++++++++++++++++++++++++++++++++----- src/routes/index.ts | 36 ++++++++++++++++++++------ 9 files changed, 149 insertions(+), 28 deletions(-) create mode 100644 src/lib/Route.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index ddc745c..c7d9c5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Graphql Schema - default-config file and generation of config file on startup - DTOs +- Home Route diff --git a/package-lock.json b/package-lock.json index bb5ff31..45b28c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2691,14 +2691,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2717,7 +2715,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -2897,8 +2894,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -3004,8 +3000,7 @@ "yallist": { "version": "3.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, diff --git a/src/app.ts b/src/app.ts index c5086c1..698a895 100644 --- a/src/app.ts +++ b/src/app.ts @@ -24,6 +24,7 @@ class App { */ public async init() { await this.dto.init(); + await routes.ioListeners(this.io); this.app.set("views", path.join(__dirname, "views")); this.app.set("view engine", "pug"); this.app.use(routes.router); diff --git a/src/index.ts b/src/index.ts index 4e814b5..e6c66b9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,9 @@ -import * as fsx from "fs-extra"; import App from "./app"; -const configPath = "config.yaml"; -const defaultConfig = __dirname + "/default-config.yaml"; - /** * async main function wrapper. */ (async () => { - if (!(await fsx.pathExists(configPath))) { - await fsx.copy(defaultConfig, configPath); - } const app = new App(); await app.init(); app.start(); diff --git a/src/lib/QueryHelper.ts b/src/lib/QueryHelper.ts index 3431994..7586276 100644 --- a/src/lib/QueryHelper.ts +++ b/src/lib/QueryHelper.ts @@ -1,10 +1,24 @@ +/** + * @author Trivernis + * @remarks + * + * Taken from {@link https://github.com/Trivernis/whooshy} + */ + import * as fsx from "fs-extra"; import {Pool, PoolClient, QueryConfig, QueryResult} from "pg"; import globals from "./globals"; const logger = globals.logger; +/** + * Transaction class to wrap SQL transactions. + */ export class SqlTransaction { + /** + * Constructor. + * @param client + */ constructor(private client: PoolClient) { } @@ -45,9 +59,17 @@ export class SqlTransaction { } } +/** + * Query helper for easyer fetching of a specific row count. + */ export class QueryHelper { private pool: Pool; + /** + * Constructor. + * @param pgPool + * @param tableCreationFile + */ constructor(pgPool: Pool, private tableCreationFile?: string) { this.pool = pgPool; } diff --git a/src/lib/Route.ts b/src/lib/Route.ts new file mode 100644 index 0000000..63c25ac --- /dev/null +++ b/src/lib/Route.ts @@ -0,0 +1,27 @@ +/** + * @author Trivernis + * @remarks + * + * Taken from {@link https://github.com/Trivernis/whooshy} + */ + +import {Router} from "express"; +import {Namespace, Server} from "socket.io"; + +/** + * Abstract Route class to be implemented by each route. + * This class contains the socket-io Server, router and resolver + * for each route. + */ +abstract class Route { + + public router?: Router; + protected io?: Server; + protected ions?: Namespace; + + public abstract async init(...params: any): Promise; + public abstract async destroy(...params: any): Promise; + public abstract async resolver(request: any, response: any): Promise; +} + +export default Route; diff --git a/src/lib/globals.ts b/src/lib/globals.ts index a64aaf0..a85ffea 100644 --- a/src/lib/globals.ts +++ b/src/lib/globals.ts @@ -1,7 +1,22 @@ +/** + * @author Trivernis + * @remarks + * + * Partly taken from {@link https://github.com/Trivernis/whooshy} + */ + import * as fsx from "fs-extra"; import * as yaml from "js-yaml"; import * as winston from "winston"; +const configPath = "config.yaml"; +const defaultConfig = __dirname + "/../default-config.yaml"; + +// ensure that the config exists by copying the default config. +if (!(fsx.pathExistsSync(configPath))) { + fsx.copySync(defaultConfig, configPath); +} + /** * Defines global variables to be used. */ diff --git a/src/routes/home.ts b/src/routes/home.ts index d1015f7..1af3be0 100644 --- a/src/routes/home.ts +++ b/src/routes/home.ts @@ -1,10 +1,55 @@ import {Router} from "express"; +import {Server} from "socket.io"; +import Route from "../lib/Route"; -const router = Router(); +/** + * Class for the home route. + */ +class HomeRoute extends Route { + /** + * Constructor, creates new router. + */ + constructor() { + super(); + this.router = Router(); + this.configure(); + } -/* GET home page. */ -router.get("/", (req, res) => { - res.render("home"); -}); + /** + * Asynchronous init for socket.io. + * @param io - the io instance + */ + public async init(io: Server) { + this.io = io; + } -export default router; + /** + * Destroys the instance by dereferencing the router and resolver. + */ + public async destroy(): Promise { + this.router = null; + this.resolver = null; + } + + /** + * Returns the resolvers for the graphql api. + * @param req - the request object + * @param res - the response object + */ + public async resolver(req: any, res: any): Promise { + return { + // TODO: Define grapql resolvers + }; + } + + /** + * Configures the route. + */ + private configure() { + this.router.get("/", (req, res) => { + res.render("home"); + }); + } +} + +export default HomeRoute; diff --git a/src/routes/index.ts b/src/routes/index.ts index b8e9138..84cc7c5 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,21 +1,43 @@ +/** + * @author Trivernis + * @remarks + * + * Taken from {@link https://github.com/Trivernis/whooshy} + */ + import {Router} from "express"; import {Server} from "socket.io"; -import homeRouter from "./home"; +import HomeRoute from "./home"; + +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("/", homeRouter); + router.use("/", homeRoute.router); + /** + * Asnyc function to create a graphql resolver that takes the request and response + * of express.js as arguments. + * @param request + * @param response + */ export const resolvers = async (request: any, response: any): Promise => { - return { - }; + return homeRoute.resolver(request, response); }; - // tslint:disable-next-line:no-empty - export const ioListeners = (io: Server) => { - + /** + * Assigns the io listeners or namespaces to the routes + * @param io + */ + export const ioListeners = async (io: Server) => { + await homeRoute.init(io); }; }