]> git.r.bdr.sh - rbdr/dead-drop/blob - lib/dead_drop.js
Merge branch 'hotfix/1.0.1' into 'master'
[rbdr/dead-drop] / lib / dead_drop.js
1 'use strict';
2
3 const Koa = require('koa');
4 const KoaBodyParser = require('koa-bodyparser');
5 const KoaRoute = require('koa-route');
6
7 const MainMenuController = require('./controllers/main_menu');
8 const RecordingMenuController = require('./controllers/recording_menu');
9 const RecordingsController = require('./controllers/recordings');
10
11 const internals = {};
12
13 /**
14 * The Dead Drop class is the main entry point for the application.
15 *
16 * @class DeadDrop
17 * @param {DeadDrop.tConfiguration} config the initialization options to
18 * extend the instance
19 */
20 module.exports = internals.DeadDrop = class DeadDrop {
21
22 constructor(config) {
23
24 Object.assign(this, config);
25 }
26
27 /**
28 * Initializes the application and starts listening. Also prints a
29 * nice robotic banner with information.
30 *
31 * @function run
32 * @memberof DeadDrop
33 * @instance
34 */
35 run() {
36
37 this._initializeServer();
38 this._startServer();
39 this._printBanner();
40
41 return Promise.resolve();
42 }
43
44 // Initializes the Koa application and all the handlers.
45
46 _initializeServer() {
47
48 const self = this;
49
50 this._app = Koa();
51
52 this._app.use(KoaBodyParser());
53
54 this._app.use(function * (next) {
55
56 const accountSid = this.request.body.AccountSid || this.request.query.AccountSid;
57
58 if (accountSid === self.twilioAccountSid) {
59 yield next;
60 }
61 else {
62 this.throw('Unauthorized', 401);
63 }
64 });
65
66 this._initializeMainMenuRoutes();
67 this._initializeRecordingMenuRoutes();
68 this._initializeRecordingsRoutes();
69
70 this._app.use(function * () {
71
72 this.body = 'How did you get here? Shoo!';
73 });
74
75 }
76
77 // Starts listening
78
79 _startServer() {
80
81 this._app.listen(this.port);
82 }
83
84 // Initializes the main menu routes.
85
86 _initializeMainMenuRoutes() {
87
88 const mainMenuController = new MainMenuController();
89
90 this._app.use(KoaRoute.get('/menus/main', mainMenuController.serveMenu()));
91 this._app.use(KoaRoute.post('/menus/main', mainMenuController.parseMenuSelection()));
92 }
93
94 // Initializes the recording menu routes.
95
96 _initializeRecordingMenuRoutes() {
97
98 const recordingMenuController = new RecordingMenuController();
99
100 this._app.use(KoaRoute.get('/menus/recording', recordingMenuController.serveMenu()));
101 this._app.use(KoaRoute.post('/menus/recording', recordingMenuController.parseMenuSelection()));
102 }
103
104 // Initializes the recordings routes.
105
106 _initializeRecordingsRoutes() {
107
108 const recordingsController = new RecordingsController({
109 redis: this.redis
110 });
111
112 this._app.use(KoaRoute.get('/recordings', recordingsController.startRecording()));
113 this._app.use(KoaRoute.post('/recordings', recordingsController.saveRecording()));
114 this._app.use(KoaRoute.get('/recordings/:id', recordingsController.getRecording()));
115 }
116
117 // Prints the banner.
118
119 _printBanner() {
120
121 console.log(' >o<');
122 console.log(' /-----\\');
123 console.log(` |ú ù| - Happy to listen on: ${this.port}`);
124 console.log(' | U |');
125 console.log(' \\---/');
126 console.log(' +---------+');
127 console.log(' ~| () |~');
128 console.log(' ~| /\\ | ~');
129 console.log(' ~| \\/ | ~c');
130 console.log(' ^+---------+');
131 console.log(' (o==o==o) ');
132
133 }
134 };