]> git.r.bdr.sh - rbdr/dead-drop/blobdiff - lib/dead_drop.js
Merge branch 'release/1.0.0'
[rbdr/dead-drop] / lib / dead_drop.js
index 1ea86e8079f602b4f8a181b7c636501dba437a35..3336444e7e1246489a504738ac8c665ba9ae6b3e 100644 (file)
@@ -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.