Merge branch 'develop' of Software_Engineering_I/greenvironment-server into tommy-dev

pull/1/head
Tomislav_K 5 years ago committed by Gitea
commit 21caebe005

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

@ -1,16 +1,43 @@
import * as express from "express"; import * as express from "express";
import * as http from "http"; import * as http from "http";
import * as socketIo from "socket.io"; import * as socketIo from "socket.io";
import {DAO} from "./lib/DAO";
import globals from "./lib/globals";
class App { class App {
public app: express.Application; public app: express.Application;
public io: socketIo.Server; public io: socketIo.Server;
public server: http.Server; public server: http.Server;
public dao: DAO;
constructor() { constructor() {
this.app = express(); this.app = express();
this.server = new http.Server(this.app); this.server = new http.Server(this.app);
this.io = socketIo(this.server); 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.");
}
} }
} }

@ -8,4 +8,4 @@ database:
# http server configuration # http server configuration
server: server:
port: port: 8080

@ -12,5 +12,7 @@ const defaultConfig = __dirname + "/default-config.yaml";
await fsx.copy(defaultConfig, configPath); await fsx.copy(defaultConfig, configPath);
} }
const app = new App(); const app = new App();
await app.init();
app.start();
})(); })();

@ -3,6 +3,7 @@ import globals from "./globals";
import {QueryHelper} from "./QueryHelper"; import {QueryHelper} from "./QueryHelper";
const config = globals.config; const config = globals.config;
const tableCreationFile = __dirname + "/../sql/create-tables.sql";
export class DAO { export class DAO {
private queryHelper: QueryHelper; private queryHelper: QueryHelper;
@ -14,6 +15,13 @@ export class DAO {
port: config.database.port, port: config.database.port,
user: config.database.user, 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();
} }
} }

@ -1,3 +1,4 @@
import * as fsx from "fs-extra";
import {Pool, PoolClient, QueryConfig, QueryResult} from "pg"; import {Pool, PoolClient, QueryConfig, QueryResult} from "pg";
import globals from "./globals"; import globals from "./globals";
@ -47,10 +48,21 @@ export class SqlTransaction {
export class QueryHelper { export class QueryHelper {
private pool: Pool; private pool: Pool;
constructor(pgPool: Pool) { constructor(pgPool: Pool, private tableCreationFile?: string) {
this.pool = pgPool; 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. * executes the sql query with values and returns all results.
* @param query * @param query

@ -14,7 +14,7 @@ namespace globals {
winston.format.timestamp(), winston.format.timestamp(),
winston.format.colorize(), winston.format.colorize(),
winston.format.printf(({ level, message, label, timestamp }) => { winston.format.printf(({ level, message, label, timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`; return `${timestamp} ${level}: ${message}`;
}), }),
), ),
}), }),

Loading…
Cancel
Save