]>
Commit | Line | Data |
---|---|---|
1 | 'use strict'; | |
2 | ||
3 | const Koa = require('koa'); | |
4 | const KoaRoute = require('koa-route'); | |
5 | ||
6 | const internals = {}; | |
7 | ||
8 | /** | |
9 | * The Dead Drop class is the main entry point for the application. | |
10 | * | |
11 | * @class DeadDrop | |
12 | * @param {DeadDrop.tConfiguration} config the initialization options to | |
13 | * extend the instance | |
14 | */ | |
15 | module.exports = internals.DeadDrop = class DeadDrop { | |
16 | ||
17 | constructor(config) { | |
18 | ||
19 | Object.assign(this, config); | |
20 | } | |
21 | ||
22 | /** | |
23 | * Initializes the application and starts listening. Also prints a | |
24 | * nice robotic banner with information. | |
25 | * | |
26 | * @function run | |
27 | * @memberof DeadDrop | |
28 | * @instance | |
29 | */ | |
30 | run() { | |
31 | ||
32 | this._initializeServer(); | |
33 | this._startServer(); | |
34 | this._printBanner(); | |
35 | ||
36 | return Promise.resolve(); | |
37 | } | |
38 | ||
39 | // Initializes the Koa application and all the handlers. | |
40 | ||
41 | _initializeServer() { | |
42 | ||
43 | this._app = Koa(); | |
44 | ||
45 | this._app.use(KoaRoute.get('/menus/main', function * () { | |
46 | ||
47 | this.body = 'I will return the main menu.'; | |
48 | })); | |
49 | ||
50 | this._app.use(KoaRoute.post('/menus/main', function * () { | |
51 | ||
52 | this.body = 'I will parse the main menu.'; | |
53 | })); | |
54 | ||
55 | this._app.use(KoaRoute.get('/menus/recording', function * () { | |
56 | ||
57 | this.body = 'I will return the select recording menu.'; | |
58 | })); | |
59 | ||
60 | this._app.use(KoaRoute.post('/menus/recording', function * () { | |
61 | ||
62 | this.body = 'I will parse the select recording menu.'; | |
63 | })); | |
64 | ||
65 | this._app.use(KoaRoute.get('/recordings', function * () { | |
66 | ||
67 | this.body = 'I will initiate recording process'; | |
68 | })); | |
69 | ||
70 | this._app.use(KoaRoute.post('/recordings', function * () { | |
71 | ||
72 | this.body = 'I will create a new recording'; | |
73 | })); | |
74 | ||
75 | this._app.use(KoaRoute.get('/recordings/:id', function * (id) { | |
76 | ||
77 | id = parseInt(id); | |
78 | ||
79 | if (id === 0) { | |
80 | this.body = 'I will return a random recording'; | |
81 | } | |
82 | else { | |
83 | this.body = 'I will return a specific recording'; | |
84 | } | |
85 | })); | |
86 | ||
87 | this._app.use(function * () { | |
88 | ||
89 | this.body = 'hello, world'; | |
90 | }); | |
91 | ||
92 | } | |
93 | ||
94 | // Starts listening | |
95 | ||
96 | _startServer() { | |
97 | ||
98 | this._app.listen(this.port); | |
99 | } | |
100 | ||
101 | // Prints the banner. | |
102 | ||
103 | _printBanner() { | |
104 | ||
105 | console.log(' >o<'); | |
106 | console.log(' /-----\\'); | |
107 | console.log(` |ú ù| - Happy to listen on: ${this.port}`); | |
108 | console.log(' | U |'); | |
109 | console.log(' \\---/'); | |
110 | console.log(' +---------+'); | |
111 | console.log(' ~| () |~'); | |
112 | console.log(' ~| /\\ | ~'); | |
113 | console.log(' ~| \\/ | ~c'); | |
114 | console.log(' ^+---------+'); | |
115 | console.log(' (o==o==o) '); | |
116 | ||
117 | } | |
118 | }; |