From 20d719801030bc554469460a184764f700430511 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Sat, 7 Sep 2019 23:31:41 +0200 Subject: [PATCH 1/2] Database and App init - added database table creation - added init method to DAO - added init method to App - added start method to App to start listening on the configured port --- src/app.ts | 29 ++++++++++++++++++++++++++++- src/default-config.yaml | 2 +- src/index.ts | 2 ++ src/lib/DAO.ts | 10 +++++++++- src/lib/QueryHelper.ts | 14 +++++++++++++- src/lib/globals.ts | 2 +- 6 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/app.ts b/src/app.ts index bdb3fa3..3eee246 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,16 +1,43 @@ import * as express from "express"; import * as http from "http"; import * as socketIo from "socket.io"; - +import {DAO} from "./lib/DAO"; +import globals from "./lib/globals"; class App { public app: express.Application; public io: socketIo.Server; public server: http.Server; + public dao: DAO; constructor() { this.app = express(); this.server = new http.Server(this.app); this.io = socketIo(this.server); + this.dao = new DAO(); + } + + /** + * initializes everything that needs to be initialized asynchronous. + */ + public async init() { + await this.dao.init(); + this.app.all("/", (req, res) => { + res.send("WIP!"); + }); + } + + /** + * Starts the web server. + */ + public start() { + if (globals.config.server.port) { + globals.logger.info(`Starting server...`); + this.app.listen(globals.config.server.port); + globals.logger.info(`Server running on port ${globals.config.server.port}`); + } else { + globals.logger.error("No port specified in the config." + + "Please configure a port in the config.yaml."); + } } } diff --git a/src/default-config.yaml b/src/default-config.yaml index 6b7f17d..417ccda 100644 --- a/src/default-config.yaml +++ b/src/default-config.yaml @@ -8,4 +8,4 @@ database: # http server configuration server: - port: + port: 8080 diff --git a/src/index.ts b/src/index.ts index 7e34c95..4e814b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,5 +12,7 @@ const defaultConfig = __dirname + "/default-config.yaml"; await fsx.copy(defaultConfig, configPath); } const app = new App(); + await app.init(); + app.start(); })(); diff --git a/src/lib/DAO.ts b/src/lib/DAO.ts index 7d60b87..6349fa9 100644 --- a/src/lib/DAO.ts +++ b/src/lib/DAO.ts @@ -3,6 +3,7 @@ import globals from "./globals"; import {QueryHelper} from "./QueryHelper"; const config = globals.config; +const tableCreationFile = __dirname + "/../sql/create-tables.sql"; export class DAO { private queryHelper: QueryHelper; @@ -14,6 +15,13 @@ export class DAO { port: config.database.port, user: config.database.user, }); - this.queryHelper = new QueryHelper(dbClient); + this.queryHelper = new QueryHelper(dbClient, tableCreationFile); + } + + /** + * Initializes everything that needs to be initialized asynchronous. + */ + public async init() { + await this.queryHelper.createTables(); } } diff --git a/src/lib/QueryHelper.ts b/src/lib/QueryHelper.ts index 7f22fb5..3431994 100644 --- a/src/lib/QueryHelper.ts +++ b/src/lib/QueryHelper.ts @@ -1,3 +1,4 @@ +import * as fsx from "fs-extra"; import {Pool, PoolClient, QueryConfig, QueryResult} from "pg"; import globals from "./globals"; @@ -47,10 +48,21 @@ export class SqlTransaction { export class QueryHelper { private pool: Pool; - constructor(pgPool: Pool) { + constructor(pgPool: Pool, private tableCreationFile?: string) { this.pool = pgPool; } + /** + * creates all tables needed if a filepath was given with the constructor + */ + public async createTables() { + if (this.tableCreationFile) { + logger.info("Creating nonexistent tables..."); + const tableSql = await fsx.readFile(this.tableCreationFile, "utf-8"); + await this.query({text: tableSql}); + } + } + /** * executes the sql query with values and returns all results. * @param query diff --git a/src/lib/globals.ts b/src/lib/globals.ts index ddd044a..a64aaf0 100644 --- a/src/lib/globals.ts +++ b/src/lib/globals.ts @@ -14,7 +14,7 @@ namespace globals { winston.format.timestamp(), winston.format.colorize(), winston.format.printf(({ level, message, label, timestamp }) => { - return `${timestamp} [${label}] ${level}: ${message}`; + return `${timestamp} ${level}: ${message}`; }), ), }), From 7dd351ddf317879b1bd5d28dada98635ec99e3d5 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 9 Sep 2019 10:05:35 +0200 Subject: [PATCH 2/2] Updated Readme --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fe1d0c9..79f8a5f 100644 --- a/README.md +++ b/README.md @@ -1 +1,11 @@ -greenvironment-server +# greenvironment-server + +Server of the greenvironment social network. + +## Install + +You need to install a nodejs runtime to run the greenvironment server. +Then you need to install all requirements. To do so, open a terminal in the +greenvironment project folder and execute "npm i". You can build the project by +executing "gulp" in the terminal. To run the server you need +to execute "node ./dist".