]>
git.r.bdr.sh - rbdr/dead-drop/blob - lib/dead_drop.js
3 const Koa
= require('koa');
4 const KoaBodyParser
= require('koa-bodyparser');
5 const KoaRoute
= require('koa-route');
7 const MainMenuController
= require('./controllers/main_menu');
8 const RecordingMenuController
= require('./controllers/recording_menu');
9 const RecordingsController
= require('./controllers/recordings');
14 * The Dead Drop class is the main entry point for the application.
17 * @param {DeadDrop.tConfiguration} config the initialization options to
20 module
.exports
= internals
.DeadDrop
= class DeadDrop
{
24 Object
.assign(this, config
);
28 * Initializes the application and starts listening. Also prints a
29 * nice robotic banner with information.
37 this._initializeServer();
41 return Promise
.resolve();
44 // Initializes the Koa application and all the handlers.
52 this._app
.use(KoaBodyParser());
54 this._app
.use(function * (next
) {
56 const accountSid
= this.request
.body
.AccountSid
|| this.request
.query
.AccountSid
;
58 if (accountSid
=== self
.twilioAccountSid
) {
62 this.throw('Unauthorized', 401);
66 this._initializeMainMenuRoutes();
67 this._initializeRecordingMenuRoutes();
68 this._initializeRecordingsRoutes();
70 this._app
.use(function * () {
72 this.body
= 'How did you get here? Shoo!';
81 this._app
.listen(this.port
);
84 // Initializes the main menu routes.
86 _initializeMainMenuRoutes() {
88 const mainMenuController
= new MainMenuController();
90 this._app
.use(KoaRoute
.get('/menus/main', mainMenuController
.serveMenu()));
91 this._app
.use(KoaRoute
.post('/menus/main', mainMenuController
.parseMenuSelection()));
94 // Initializes the recording menu routes.
96 _initializeRecordingMenuRoutes() {
98 const recordingMenuController
= new RecordingMenuController();
100 this._app
.use(KoaRoute
.get('/menus/recording', recordingMenuController
.serveMenu()));
101 this._app
.use(KoaRoute
.post('/menus/recording', recordingMenuController
.parseMenuSelection()));
104 // Initializes the recordings routes.
106 _initializeRecordingsRoutes() {
108 const recordingsController
= new RecordingsController({
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()));
117 // Prints the banner.
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) ');