]> git.r.bdr.sh - rbdr/dead-drop/blob - lib/dead_drop.js
6f976459e18231d8ed5fbd0cb950b33b0024ca7d
[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 this._app = Koa();
49
50 this._app.use(KoaBodyParser());
51
52 this._initializeMainMenuRoutes();
53 this._initializeRecordingMenuRoutes();
54 this._initializeRecordingsRoutes();
55
56 this._app.use(function * () {
57
58 this.body = 'How did you get here? Shoo!';
59 });
60
61 }
62
63 // Starts listening
64
65 _startServer() {
66
67 this._app.listen(this.port);
68 }
69
70 // Initializes the main menu routes.
71
72 _initializeMainMenuRoutes() {
73
74 const mainMenuController = new MainMenuController();
75
76 this._app.use(KoaRoute.get('/menus/main', mainMenuController.serveMenu()));
77 this._app.use(KoaRoute.post('/menus/main', mainMenuController.parseMenuSelection()));
78 }
79
80 // Initializes the recording menu routes.
81
82 _initializeRecordingMenuRoutes() {
83
84 const recordingMenuController = new RecordingMenuController();
85
86 this._app.use(KoaRoute.get('/menus/recording', recordingMenuController.serveMenu()));
87 this._app.use(KoaRoute.post('/menus/recording', recordingMenuController.parseMenuSelection()));
88 }
89
90 // Initializes the recordings routes.
91
92 _initializeRecordingsRoutes() {
93
94 const recordingsController = new RecordingsController({
95 redis: this.redis
96 });
97
98 this._app.use(KoaRoute.get('/recordings', recordingsController.startRecording()));
99 this._app.use(KoaRoute.post('/recordings', recordingsController.saveRecording()));
100 this._app.use(KoaRoute.get('/recordings/:id', recordingsController.getRecording()));
101 }
102
103 // Prints the banner.
104
105 _printBanner() {
106
107 console.log(' >o<');
108 console.log(' /-----\\');
109 console.log(` |ú ù| - Happy to listen on: ${this.port}`);
110 console.log(' | U |');
111 console.log(' \\---/');
112 console.log(' +---------+');
113 console.log(' ~| () |~');
114 console.log(' ~| /\\ | ~');
115 console.log(' ~| \\/ | ~c');
116 console.log(' ^+---------+');
117 console.log(' (o==o==o) ');
118
119 }
120 };