X-Git-Url: https://git.r.bdr.sh/rbdr/dead-drop/blobdiff_plain/5e981bca19ace2a7704f267175b6aa368e63bda0..4546edbd1634cde4c9ffe90459269c17ba9f8bfb:/lib/dead_drop.js?ds=sidebyside diff --git a/lib/dead_drop.js b/lib/dead_drop.js index 1ea86e8..3336444 100644 --- a/lib/dead_drop.js +++ b/lib/dead_drop.js @@ -1,8 +1,13 @@ 'use strict'; const Koa = require('koa'); +const KoaBodyParser = require('koa-bodyparser'); const KoaRoute = require('koa-route'); +const MainMenuController = require('./controllers/main_menu'); +const RecordingMenuController = require('./controllers/recording_menu'); +const RecordingsController = require('./controllers/recordings'); + const internals = {}; /** @@ -40,62 +45,73 @@ module.exports = internals.DeadDrop = class DeadDrop { _initializeServer() { + const self = this; + this._app = Koa(); - this._app.use(KoaRoute.get('/menus/main', function * () { + this._app.use(KoaBodyParser()); - this.body = 'I will return the main menu.'; - })); + this._app.use(function * (next) { - this._app.use(KoaRoute.post('/menus/main', function * () { + const accountSid = this.request.body.AccountSid || this.request.query.AccountSid; - this.body = 'I will parse the main menu.'; - })); + if (accountSid === self.twilioAccountSid) { + yield next; + } + else { + this.throw('Unauthorized', 401); + } + }); - this._app.use(KoaRoute.get('/menus/recording', function * () { + this._initializeMainMenuRoutes(); + this._initializeRecordingMenuRoutes(); + this._initializeRecordingsRoutes(); - this.body = 'I will return the select recording menu.'; - })); + this._app.use(function * () { - this._app.use(KoaRoute.post('/menus/recording', function * () { + this.body = 'How did you get here? Shoo!'; + }); - this.body = 'I will parse the select recording menu.'; - })); + } - this._app.use(KoaRoute.get('/recordings', function * () { + // Starts listening - this.body = 'I will initiate recording process'; - })); + _startServer() { - this._app.use(KoaRoute.post('/recordings', function * () { + this._app.listen(this.port); + } - this.body = 'I will create a new recording'; - })); + // Initializes the main menu routes. - this._app.use(KoaRoute.get('/recordings/:id', function * (id) { + _initializeMainMenuRoutes() { - id = parseInt(id); + const mainMenuController = new MainMenuController(); - if (id === 0) { - this.body = 'I will return a random recording'; - } - else { - this.body = 'I will return a specific recording'; - } - })); + this._app.use(KoaRoute.get('/menus/main', mainMenuController.serveMenu())); + this._app.use(KoaRoute.post('/menus/main', mainMenuController.parseMenuSelection())); + } - this._app.use(function * () { + // Initializes the recording menu routes. - this.body = 'hello, world'; - }); + _initializeRecordingMenuRoutes() { + const recordingMenuController = new RecordingMenuController(); + + this._app.use(KoaRoute.get('/menus/recording', recordingMenuController.serveMenu())); + this._app.use(KoaRoute.post('/menus/recording', recordingMenuController.parseMenuSelection())); } - // Starts listening + // Initializes the recordings routes. - _startServer() { + _initializeRecordingsRoutes() { - this._app.listen(this.port); + const recordingsController = new RecordingsController({ + redis: this.redis + }); + + this._app.use(KoaRoute.get('/recordings', recordingsController.startRecording())); + this._app.use(KoaRoute.post('/recordings', recordingsController.saveRecording())); + this._app.use(KoaRoute.get('/recordings/:id', recordingsController.getRecording())); } // Prints the banner.