aboutsummaryrefslogtreecommitdiff
path: root/lib/forum.js
blob: f09e8dd1c0fbf47f14b9f8e87177af590d781794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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('      (.) ');
  }
}