Initial Commit

This commit is contained in:
plane000
2018-08-18 18:49:16 +01:00
parent 7082ce38a2
commit e975b34546
11 changed files with 1553 additions and 0 deletions

14
src/app.js Normal file
View File

@@ -0,0 +1,14 @@
import {Logger} from './models/logger';
import {Config} from './config/config'
import {Server} from './server';
import {Router} from './controllers/routes/router';
init();
async function init() {
await Config.load();
await Server.start();
await Router.initEndpoints();
}

20
src/config/config.js Normal file
View File

@@ -0,0 +1,20 @@
import fs from 'fs';
import {Logger} from '../models/logger';
let server;
export class Config {
static get Server() {return server}
static async load() {
try {
let temp = fs.readFileSync('src/config/configs/server.json')
server = JSON.parse(temp);
} catch (e) {
Logger.panic('ERROR LOADING: src/config/configs/server.json');
}
Logger.info('Config loaded');
}
}

View File

@@ -0,0 +1,4 @@
{
"HTTPPort": 8080,
"SocketPort": 8081
}

View File

@@ -0,0 +1,23 @@
import {Logger} from '../../models/logger';
import {Server} from '../../server';
let app;
export class Router {
static async initEndpoints() {
Logger.info('Setting up API HTTP endpoints');
app = Server.App;
app.get('/', (req, res) => {
res.end('DEV SERVER');
});
app.get('/user/:id', (req, res) => {});
app.delete('/user/:id', (req, res) => {});
app.post('/user', (req, res) => {});
Logger.info('HTTP endpoints settup');
}
}

33
src/models/logger.js Normal file
View File

@@ -0,0 +1,33 @@
const colours = require('colors/safe');
export class Logger {
static info(message) {
if (!message) throw 'No message defined';
let d = new Date();
console.log('[' + d.toLocaleString() + '] ['
+ colours.green('INFO') + '] ' + message);
}
static warn(message) {
if (!message) throw 'No message defined';
let d = new Date();
console.log('[' + d.toLocaleString() + '] ['
+ colours.yellow('WARN') + '] ' + message);
}
static error(message) {
if (!message) throw 'No message defined';
let d = new Date();
console.log('[' + d.toLocaleString() + '] ['
+ colours.red('ERROR') + '] ' + message);
}
static panic(message) {
if (!message) throw 'No message defined';
let d = new Date();
console.log('[' + d.toLocaleString() + '] ['
+ colours.red('PANIC') + '] ' + message);
process.exit();
}
}

30
src/server.js Normal file
View File

@@ -0,0 +1,30 @@
import express from 'express'
import bodyParser from 'body-parser';
import {Logger} from './models/logger';
import {Config} from './config/config';
let app;
let server;
let router
export class Server {
static get App() {return app}
static get Server() {return server}
static async start() {
app = express();
server = require('http').createServer(app);
Logger.info('Server created');
let port = Config.Server.HTTPPort;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
try {
app.listen(port);
} catch (e) {
Logger.panic(`Could not open a connection on port ${port}, maybe the port is populated or permissions are not met`);
}
Logger.info(`HTTP service is listening at port ${port}`);
}
}