diff options
| author | Ben Beltran <ben@nsovocal.com> | 2019-12-23 00:21:35 +0100 |
|---|---|---|
| committer | Ben Beltran <ben@nsovocal.com> | 2019-12-23 00:21:35 +0100 |
| commit | 2d5b700eb63669ca0a4bf27b5df58aa8af15735f (patch) | |
| tree | 2e11a0cc848cf3b0151385547bac7d405d347336 /lib | |
| parent | a9b3b4d512421817cc34c4efe5f4d29a9453278d (diff) | |
Add backend files
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/forum.js | 87 | ||||
| -rw-r--r-- | lib/middleware/handle_errors.js | 25 |
2 files changed, 112 insertions, 0 deletions
diff --git a/lib/forum.js b/lib/forum.js new file mode 100644 index 0000000..0253b3d --- /dev/null +++ b/lib/forum.js @@ -0,0 +1,87 @@ +import Koa from 'koa'; + +import HandleErrors from './middleware/handle_errors.js'; +import KoaSend from 'koa-send'; +import KoaStatic from 'koa-static'; +import Path from 'path'; + +/** + * The Forum class is the main entry point for the backend application. + * + * @param {tForumBackendConfiguration} config the initialization options to + * extend the instance + */ +export default class Forum { + + constructor(config) { + + Object.assign(this, config); + } + + /** + * Initializes the application and starts listening. Also prints a + * nice robotic banner with information. + */ + run() { + + this._initializeServer(); + this._startServer(); + this._printBanner(); + + return Promise.resolve(); + } + + // Initializes the Koa application and all the handlers. + + _initializeServer() { + + const self = this; + + this._app = new Koa(); + this._app.use(KoaStatic(this.staticDirectory)); + + // Error handler + + this._app.use(HandleErrors); + + this._initializePostsRoutes(); + + this._app.use(async function () { + + await KoaSend(this, Path.join(self.staticDirectory, 'index.html')); + }); + + } + + // Initialize routes for posts + + _initializePostsRoutes() { + + // Under construction + } + + + // Starts listening + + _startServer() { + + this._app.listen(this.port); + } + + // Prints the banner. + + _printBanner() { + + console.log(' .'); + console.log(' /'); + console.log(' +-----+'); + console.log(` | o o | - Listening Gladly, Try me on port: ${this.port}`); + console.log(' +-----+'); + console.log(' +---------+'); + console.log(' /| [][] |\\'); + console.log(' || | |'); + console.log(' || | \\c'); + console.log(' ^+---------+'); + console.log(' (.) '); + } +}; diff --git a/lib/middleware/handle_errors.js b/lib/middleware/handle_errors.js new file mode 100644 index 0000000..f969676 --- /dev/null +++ b/lib/middleware/handle_errors.js @@ -0,0 +1,25 @@ +/** + * Middleware to create responses for unhandled errors. + */ +export default async function HandleErrors(next) { + + try { + await next; + } + catch (err) { + this.status = err.status || 500; + + const response = { + error: err.message, + status: this.status + }; + + if (response.status === 401) { + response.error === 'Protected resource, use Authorization header to get access'; + } + + this.body = response; + + this.app.emit('error', err, this); + } +} |