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
pull/1/head
Trivernis 5 years ago
parent 387d9e0187
commit f7572202f9

@ -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

11
package-lock.json generated

@ -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
}
}
},

@ -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);

@ -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();

@ -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;
}

@ -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<any>;
public abstract async destroy(...params: any): Promise<any>;
public abstract async resolver(request: any, response: any): Promise<object>;
}
export default Route;

@ -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.
*/

@ -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) => {
/**
* Asynchronous init for socket.io.
* @param io - the io instance
*/
public async init(io: Server) {
this.io = io;
}
/**
* Destroys the instance by dereferencing the router and resolver.
*/
public async destroy(): Promise<void> {
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<object> {
return {
// TODO: Define grapql resolvers
};
}
/**
* Configures the route.
*/
private configure() {
this.router.get("/", (req, res) => {
res.render("home");
});
}
}
export default router;
export default HomeRoute;

@ -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<object> => {
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);
};
}

Loading…
Cancel
Save