]> git.r.bdr.sh - rbdr/forum/blob - lib/forum.js
d095e47e4c74e168dc1bc2fd1cb50bdf07d42ce5
[rbdr/forum] / lib / forum.js
1 import Koa from 'koa';
2
3 import HandleErrors from './middleware/handle_errors.js';
4 import KoaSend from 'koa-send';
5 import KoaStatic from 'koa-static';
6 import Path from 'path';
7
8 /**
9 * The Forum class is the main entry point for the backend application.
10 *
11 * @module Forum
12 * @param {tForumBackendConfiguration} config the initialization options to
13 * extend the instance
14 */
15 export default class Forum {
16
17 constructor(config) {
18
19 Object.assign(this, config);
20 }
21
22 /**
23 * Initializes the application and starts listening. Also prints a
24 * nice robotic banner with information.
25 */
26 run() {
27
28 this._initializeServer();
29 this._startServer();
30 this._printBanner();
31
32 return Promise.resolve();
33 }
34
35 // Initializes the Koa application and all the handlers.
36
37 _initializeServer() {
38
39 const self = this;
40
41 this._app = new Koa();
42 this._app.use(KoaStatic(this.staticDirectory));
43
44 // Error handler
45
46 this._app.use(HandleErrors);
47
48 this._initializePostsRoutes();
49
50 this._app.use(async function () {
51
52 await KoaSend(this, Path.join(self.staticDirectory, 'index.html'));
53 });
54
55 }
56
57 // Initialize routes for posts
58
59 _initializePostsRoutes() {
60
61 // Under construction
62 }
63
64
65 // Starts listening
66
67 _startServer() {
68
69 this._app.listen(this.port);
70 }
71
72 // Prints the banner.
73
74 _printBanner() {
75
76 console.log(' .');
77 console.log(' /');
78 console.log(' +-----+');
79 console.log(` | o o | - Listening Gladly, Try me on port: ${this.port}`);
80 console.log(' +-----+');
81 console.log(' +---------+');
82 console.log(' /| [][] |\\');
83 console.log(' || | |');
84 console.log(' || | \\c');
85 console.log(' ^+---------+');
86 console.log(' (.) ');
87 }
88 };