aboutsummaryrefslogtreecommitdiff
path: root/lib/dead_drop.js
blob: 1ea86e8079f602b4f8a181b7c636501dba437a35 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';

const Koa = require('koa');
const KoaRoute = require('koa-route');

const internals = {};

/**
 * The Dead Drop class is the main entry point for the application.
 *
 * @class DeadDrop
 * @param {DeadDrop.tConfiguration} config the initialization options to
 * extend the instance
 */
module.exports = internals.DeadDrop = class DeadDrop {

  constructor(config) {

    Object.assign(this, config);
  }

  /**
   * Initializes the application and starts listening. Also prints a
   * nice robotic banner with information.
   *
   * @function run
   * @memberof DeadDrop
   * @instance
   */
  run() {

    this._initializeServer();
    this._startServer();
    this._printBanner();

    return Promise.resolve();
  }

  // Initializes the Koa application and all the handlers.

  _initializeServer() {

    this._app = Koa();

    this._app.use(KoaRoute.get('/menus/main', function * () {

      this.body = 'I will return the main menu.';
    }));

    this._app.use(KoaRoute.post('/menus/main', function * () {

      this.body = 'I will parse the main menu.';
    }));

    this._app.use(KoaRoute.get('/menus/recording', function * () {

      this.body = 'I will return the select recording menu.';
    }));

    this._app.use(KoaRoute.post('/menus/recording', function * () {

      this.body = 'I will parse the select recording menu.';
    }));

    this._app.use(KoaRoute.get('/recordings', function * () {

      this.body = 'I will initiate recording process';
    }));

    this._app.use(KoaRoute.post('/recordings', function * () {

      this.body = 'I will create a new recording';
    }));

    this._app.use(KoaRoute.get('/recordings/:id', function * (id) {

      id = parseInt(id);

      if (id === 0) {
        this.body = 'I will return a random recording';
      }
      else {
        this.body = 'I will return a specific recording';
      }
    }));

    this._app.use(function * () {

      this.body = 'hello, world';
    });

  }

  // Starts listening

  _startServer() {

    this._app.listen(this.port);
  }

  // Prints the banner.

  _printBanner() {

    console.log('      >o<');
    console.log('    /-----\\');
    console.log(`    |ú   ù|  - Happy to listen on: ${this.port}`);
    console.log('    |  U  |');
    console.log('     \\---/');
    console.log('  +---------+');
    console.log(' ~|    ()   |~');
    console.log(' ~|    /\\   | ~');
    console.log(' ~|    \\/   |  ~c');
    console.log(' ^+---------+');
    console.log('   (o==o==o) ');

  }
};