]> git.r.bdr.sh - rbdr/dead-drop/blame - lib/dead_drop.js
Merge branch 'hotfix/1.0.1' into 'master'
[rbdr/dead-drop] / lib / dead_drop.js
CommitLineData
5e981bca
RBR
1'use strict';
2
3const Koa = require('koa');
7404eac9 4const KoaBodyParser = require('koa-bodyparser');
5e981bca
RBR
5const KoaRoute = require('koa-route');
6
7404eac9
RBR
7const MainMenuController = require('./controllers/main_menu');
8const RecordingMenuController = require('./controllers/recording_menu');
9const RecordingsController = require('./controllers/recordings');
10
5e981bca
RBR
11const 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 */
20module.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
e5fdb2d4
RBR
48 const self = this;
49
5e981bca
RBR
50 this._app = Koa();
51
7404eac9 52 this._app.use(KoaBodyParser());
5e981bca 53
e5fdb2d4
RBR
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
7404eac9
RBR
66 this._initializeMainMenuRoutes();
67 this._initializeRecordingMenuRoutes();
68 this._initializeRecordingsRoutes();
5e981bca 69
7404eac9 70 this._app.use(function * () {
5e981bca 71
7404eac9
RBR
72 this.body = 'How did you get here? Shoo!';
73 });
5e981bca 74
7404eac9 75 }
5e981bca 76
7404eac9 77 // Starts listening
5e981bca 78
7404eac9 79 _startServer() {
5e981bca 80
7404eac9
RBR
81 this._app.listen(this.port);
82 }
5e981bca 83
7404eac9 84 // Initializes the main menu routes.
5e981bca 85
7404eac9 86 _initializeMainMenuRoutes() {
5e981bca 87
7404eac9 88 const mainMenuController = new MainMenuController();
5e981bca 89
7404eac9
RBR
90 this._app.use(KoaRoute.get('/menus/main', mainMenuController.serveMenu()));
91 this._app.use(KoaRoute.post('/menus/main', mainMenuController.parseMenuSelection()));
92 }
5e981bca 93
7404eac9 94 // Initializes the recording menu routes.
5e981bca 95
7404eac9 96 _initializeRecordingMenuRoutes() {
5e981bca 97
7404eac9 98 const recordingMenuController = new RecordingMenuController();
5e981bca 99
7404eac9
RBR
100 this._app.use(KoaRoute.get('/menus/recording', recordingMenuController.serveMenu()));
101 this._app.use(KoaRoute.post('/menus/recording', recordingMenuController.parseMenuSelection()));
5e981bca
RBR
102 }
103
7404eac9 104 // Initializes the recordings routes.
5e981bca 105
7404eac9 106 _initializeRecordingsRoutes() {
5e981bca 107
7404eac9
RBR
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()));
5e981bca
RBR
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};