From 48c3e31fb35988f7e38c615108cd12735b0891d5 Mon Sep 17 00:00:00 2001 From: Christopher Chen Date: Sat, 9 Aug 2025 21:22:30 -0400 Subject: [PATCH] Create Express Server --- .env | 13 ++++++ .gitignore | 8 ++++ agent/app.js | 0 databases/database.mongo.js | 31 +++++++++++++ databases/database.postgres.js | 39 ++++++++++++++++ web/app.js | 3 ++ web/app.server.js | 72 +++++++++++++++++++++++++++++ web/routes/authentication.router.js | 9 ++++ web/tailwind.css | 1 + 9 files changed, 176 insertions(+) create mode 100644 .env create mode 100644 .gitignore create mode 100644 agent/app.js create mode 100644 databases/database.mongo.js create mode 100644 databases/database.postgres.js create mode 100644 web/app.js create mode 100644 web/app.server.js create mode 100644 web/routes/authentication.router.js create mode 100644 web/tailwind.css diff --git a/.env b/.env new file mode 100644 index 0000000..bb47179 --- /dev/null +++ b/.env @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e288ab --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Node Modules +node_modules + +# Environment Files +.env + +# Keys +keys \ No newline at end of file diff --git a/agent/app.js b/agent/app.js new file mode 100644 index 0000000..e69de29 diff --git a/databases/database.mongo.js b/databases/database.mongo.js new file mode 100644 index 0000000..4500b23 --- /dev/null +++ b/databases/database.mongo.js @@ -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() { + + } +} \ No newline at end of file diff --git a/databases/database.postgres.js b/databases/database.postgres.js new file mode 100644 index 0000000..a466fd0 --- /dev/null +++ b/databases/database.postgres.js @@ -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}\`; })();`); + } +} \ No newline at end of file diff --git a/web/app.js b/web/app.js new file mode 100644 index 0000000..eefc085 --- /dev/null +++ b/web/app.js @@ -0,0 +1,3 @@ +const server = new (require('./app.server'))(); +server.setup(); +server.start(); \ No newline at end of file diff --git a/web/app.server.js b/web/app.server.js new file mode 100644 index 0000000..8a73979 --- /dev/null +++ b/web/app.server.js @@ -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; } +} \ No newline at end of file diff --git a/web/routes/authentication.router.js b/web/routes/authentication.router.js new file mode 100644 index 0000000..a95da16 --- /dev/null +++ b/web/routes/authentication.router.js @@ -0,0 +1,9 @@ +const router = require('express').Router(); + +// Router +router.get('/login', (req, res) => { + return res.render('login/login'); +}); + +// Exports +module.exports = router; \ No newline at end of file diff --git a/web/tailwind.css b/web/tailwind.css new file mode 100644 index 0000000..a461c50 --- /dev/null +++ b/web/tailwind.css @@ -0,0 +1 @@ +@import "tailwindcss"; \ No newline at end of file