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. * * @module Forum * @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(' (.) '); } }