Create Express Server
This commit is contained in:
13
.env
Normal file
13
.env
Normal file
@@ -0,0 +1,13 @@
|
||||
# Express Server
|
||||
SERVER_PORT=5055
|
||||
SERVER_HOST=localhost
|
||||
|
||||
# Authentication
|
||||
AUTHENTICATION_SALT=882S6Cnz
|
||||
|
||||
# Postgres
|
||||
POSTGRES_HOST=192.168.2.20
|
||||
POSTGRES_DATABASE=onepiecetcg
|
||||
POSTGRES_PORT=5432
|
||||
POSTGRES_USERNAME=onepiecetcg
|
||||
POSTGRES_PASSWORD=uYTZ8DEwS9q2mMLRzt9PpVmAhxq6FzvJ
|
||||
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Node Modules
|
||||
node_modules
|
||||
|
||||
# Environment Files
|
||||
.env
|
||||
|
||||
# Keys
|
||||
keys
|
||||
0
agent/app.js
Normal file
0
agent/app.js
Normal file
31
databases/database.mongo.js
Normal file
31
databases/database.mongo.js
Normal file
@@ -0,0 +1,31 @@
|
||||
require('dotenv').config();
|
||||
|
||||
module.exports = class MongoSingleton {
|
||||
static instance;
|
||||
|
||||
constructor() {
|
||||
if (MongoSingleton.instance)
|
||||
return this.instance;
|
||||
MongoSingleton.instance = this;
|
||||
}
|
||||
|
||||
static getInstance() {
|
||||
if (!MongoSingleton.instance)
|
||||
MongoSingleton.instance = new MongoSingleton();
|
||||
return MongoSingleton.instance;
|
||||
}
|
||||
|
||||
setup() {
|
||||
|
||||
}
|
||||
start() {
|
||||
|
||||
}
|
||||
stop() {
|
||||
|
||||
}
|
||||
|
||||
query() {
|
||||
|
||||
}
|
||||
}
|
||||
39
databases/database.postgres.js
Normal file
39
databases/database.postgres.js
Normal file
@@ -0,0 +1,39 @@
|
||||
require('dotenv').config();
|
||||
|
||||
|
||||
module.exports = class PostgresSingleton {
|
||||
static instance;
|
||||
|
||||
constructor() {
|
||||
if (PostgresSingleton.instance)
|
||||
return this.instance;
|
||||
PostgresSingleton.instance = this;
|
||||
}
|
||||
|
||||
static getInstance() {
|
||||
if (!PostgresSingleton.instance)
|
||||
PostgresSingleton.instance = new PostgresSingleton();
|
||||
return PostgresSingleton.instance;
|
||||
}
|
||||
|
||||
setup() {
|
||||
if (this.postgres === null || typeof this.postgres == 'undefined')
|
||||
this.postgres = require('postgres')({
|
||||
host: process.env.POSTGRES_HOST, post: process.env.POSTGRES_PORT, database: process.env.POSTGRES_DATABASE,
|
||||
username: process.env.POSTGRES_USERNAME, password: process.env.POSTGRES_PASSWORD
|
||||
});
|
||||
}
|
||||
async start() {
|
||||
if (this.postgres === null || typeof this.postgres == 'undefined')
|
||||
this.setup();
|
||||
}
|
||||
async stop() {
|
||||
}
|
||||
|
||||
async query(query, parameters) {
|
||||
const evaluated = Object.entries(parameters)
|
||||
.reduce((a, [k, v]) => a.concat(`const ${k}=${JSON.stringify(v)}`), '');
|
||||
|
||||
return await eval(`(async () => { ${evaluated} return await this.postgres\`${query}\`; })();`);
|
||||
}
|
||||
}
|
||||
3
web/app.js
Normal file
3
web/app.js
Normal file
@@ -0,0 +1,3 @@
|
||||
const server = new (require('./app.server'))();
|
||||
server.setup();
|
||||
server.start();
|
||||
72
web/app.server.js
Normal file
72
web/app.server.js
Normal file
@@ -0,0 +1,72 @@
|
||||
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; }
|
||||
}
|
||||
9
web/routes/authentication.router.js
Normal file
9
web/routes/authentication.router.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const router = require('express').Router();
|
||||
|
||||
// Router
|
||||
router.get('/login', (req, res) => {
|
||||
return res.render('login/login');
|
||||
});
|
||||
|
||||
// Exports
|
||||
module.exports = router;
|
||||
1
web/tailwind.css
Normal file
1
web/tailwind.css
Normal file
@@ -0,0 +1 @@
|
||||
@import "tailwindcss";
|
||||
Reference in New Issue
Block a user