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