72 lines
2.2 KiB
JavaScript
72 lines
2.2 KiB
JavaScript
const express = require("express");
|
|
require('dotenv').config();
|
|
|
|
module.exports = class ServerSingleton {
|
|
static instance;
|
|
|
|
constructor() {
|
|
if (ServerSingleton.instance)
|
|
return this.instance;
|
|
ServerSingleton.instance = this;
|
|
}
|
|
|
|
static getInstance() {
|
|
if (!ServerSingleton.instance)
|
|
ServerSingleton.instance = new ServerSingleton();
|
|
return ServerSingleton.instance;
|
|
}
|
|
|
|
setup() {
|
|
this.setupDatabases();
|
|
this.setupExpress();
|
|
}
|
|
start() {
|
|
if (this.server === null || typeof this.server === "undefined")
|
|
this.setup();
|
|
|
|
this.server.listen(process.env.SERVER_PORT,
|
|
() => console.log(`Server listening on port ${process.env.SERVER_PORT}`));
|
|
}
|
|
stop() {
|
|
|
|
}
|
|
|
|
setupDatabases() {
|
|
[
|
|
{ name: 'Postgres', instance: require('../databases/database.postgres').getInstance() },
|
|
{ name: 'Mongo', instance: require('../databases/database.mongo').getInstance() }
|
|
].forEach((database) => database.instance.start());
|
|
}
|
|
setupExpress() {
|
|
const express = require('express');
|
|
const app = express();
|
|
|
|
ServerSingleton.getInstance().setupExpressPublic(express, app);
|
|
ServerSingleton.getInstance().setupExpressViewEngine(app);
|
|
ServerSingleton.getInstance().setupExpressParsers(app);
|
|
ServerSingleton.getInstance().setupExpressRoutes(app);
|
|
|
|
this.server = require('http').createServer(app);
|
|
}
|
|
setupExpressPublic(express, app) {
|
|
app.use(express.static('./public'));
|
|
}
|
|
setupExpressViewEngine(app) {
|
|
app.set('view engine', 'ejs');
|
|
}
|
|
setupExpressParsers(app) {
|
|
[
|
|
{ name: 'JSON', instance: require('body-parser').json() },
|
|
{ name: 'UrlEncoder', instance: require('body-parser').urlencoded({ extended: true }) },
|
|
{ name: 'Cookie', instance: require("cookie-parser")() }
|
|
].forEach(parser => app.use(parser.instance));
|
|
}
|
|
setupExpressRoutes(app) {
|
|
[
|
|
{ name: 'Authentication', path: '/', router: require('./routes/authentication.router') },
|
|
].forEach(route => app.use(route.path, route.router));
|
|
}
|
|
|
|
|
|
getServer() { return this.server; }
|
|
} |