]>
Commit | Line | Data |
---|---|---|
cc69fef4 RBR |
1 | 'use strict'; |
2 | ||
287fa13b | 3 | const Koa = require('koa'); |
a6ccda0f | 4 | const KoaBodyParser = require('koa-bodyparser'); |
287fa13b | 5 | const KoaJwt = require('koa-jwt'); |
287fa13b | 6 | const KoaRoute = require('koa-route'); |
a6ccda0f RBR |
7 | const KoaSend = require('koa-send'); |
8 | const KoaStatic = require('koa-static'); | |
9 | const Path = require('path'); | |
287fa13b RBR |
10 | |
11 | const AuthHandler = require('./handlers/auth'); | |
a6ccda0f | 12 | const PostsHandler = require('./handlers/posts'); |
287fa13b | 13 | |
cc69fef4 RBR |
14 | const internals = {}; |
15 | ||
287fa13b RBR |
16 | internals.k401Location = '/401.html'; |
17 | internals.kMainLocation = '/'; | |
18 | ||
a6ccda0f RBR |
19 | /** |
20 | * The Dasein class is the main entry point for the application. | |
21 | * | |
22 | * @class Dasein | |
23 | * @param {Dasein.tConfiguration} config the initialization options to | |
24 | * extend the instance | |
25 | */ | |
cc69fef4 RBR |
26 | module.exports = internals.Dasein = class Dasein { |
27 | ||
287fa13b RBR |
28 | constructor(config) { |
29 | ||
30 | Object.assign(this, config); | |
31 | } | |
32 | ||
a6ccda0f RBR |
33 | /** |
34 | * Initializes the application and starts listening. Also prints a | |
35 | * nice robotic banner with information. | |
36 | * | |
37 | * @function run | |
38 | * @memberof Dasein | |
39 | * @instance | |
40 | */ | |
cc69fef4 RBR |
41 | run() { |
42 | ||
287fa13b RBR |
43 | this._initializeServer(); |
44 | this._startServer(); | |
45 | this._printBanner(); | |
cc69fef4 RBR |
46 | |
47 | return Promise.resolve(); | |
48 | } | |
287fa13b | 49 | |
a6ccda0f RBR |
50 | // Initializes the Koa application and all the handlers. |
51 | ||
287fa13b RBR |
52 | _initializeServer() { |
53 | ||
a6ccda0f | 54 | const self = this; |
287fa13b | 55 | |
a6ccda0f | 56 | this._app = Koa(); |
287fa13b RBR |
57 | |
58 | this._app.use(KoaStatic(this.staticDirectory)); | |
a6ccda0f | 59 | this._app.use(KoaBodyParser()); |
287fa13b | 60 | |
a6ccda0f | 61 | // Error handler |
287fa13b RBR |
62 | |
63 | this._app.use(function * (next) { | |
64 | ||
65 | try { | |
66 | yield next; | |
67 | } | |
68 | catch (err) { | |
a6ccda0f RBR |
69 | this.status = err.status || 500; |
70 | ||
71 | const response = { | |
72 | error: err.message, | |
73 | status: this.status | |
74 | }; | |
75 | ||
76 | if (response.status === 401) { | |
77 | response.error === 'Protected resource, use Authorization header to get access'; | |
287fa13b RBR |
78 | } |
79 | ||
a6ccda0f RBR |
80 | this.body = response; |
81 | ||
82 | this.app.emit('error', err, this); | |
287fa13b RBR |
83 | } |
84 | }); | |
85 | ||
86 | this._app.use(KoaJwt({ | |
87 | secret: this.jwt.secret, | |
a6ccda0f | 88 | passthrough: true |
287fa13b RBR |
89 | })); |
90 | ||
a6ccda0f RBR |
91 | this._initializeAuthRoutes(); |
92 | this._initializePostsRoutes(); | |
93 | ||
94 | this._app.use(function * () { | |
95 | ||
96 | yield KoaSend(this, Path.join(self.staticDirectory, 'index.html')); | |
97 | }); | |
98 | ||
99 | } | |
100 | ||
101 | // Initialize routes for auth | |
102 | ||
103 | _initializeAuthRoutes() { | |
287fa13b RBR |
104 | |
105 | const authHandler = new AuthHandler({ | |
287fa13b RBR |
106 | jwt: this.jwt, |
107 | twitter: this.twitter | |
108 | }); | |
a6ccda0f RBR |
109 | this._app.use(KoaRoute.get('/api/auth/login', authHandler.login())); |
110 | this._app.use(KoaRoute.post('/api/auth/callback', authHandler.callback())); | |
111 | } | |
287fa13b | 112 | |
a6ccda0f | 113 | // Initialize routes for posts |
287fa13b | 114 | |
a6ccda0f | 115 | _initializePostsRoutes() { |
287fa13b | 116 | |
a6ccda0f RBR |
117 | const postsHandler = new PostsHandler({ |
118 | ttl: this.ttl, | |
119 | redis: this.redis | |
287fa13b | 120 | }); |
a6ccda0f RBR |
121 | this._app.use(KoaRoute.get('/api/posts', postsHandler.findAll())); |
122 | this._app.use(KoaRoute.get('/api/posts/:id', postsHandler.find())); | |
123 | this._app.use(KoaRoute.post('/api/posts', postsHandler.create())); | |
124 | this._app.use(KoaRoute.delete('/api/posts/:id', postsHandler.delete())); | |
125 | ||
287fa13b RBR |
126 | } |
127 | ||
a6ccda0f RBR |
128 | // Starts listening |
129 | ||
287fa13b RBR |
130 | _startServer() { |
131 | ||
132 | this._app.listen(this.port); | |
133 | } | |
134 | ||
135 | // Prints the banner. | |
a6ccda0f | 136 | |
287fa13b RBR |
137 | _printBanner() { |
138 | ||
139 | console.log(' .'); | |
140 | console.log(' /'); | |
141 | console.log(' +-----+'); | |
142 | console.log(` | o o | - Listening Gladly, Try me on port: ${this.port}`); | |
143 | console.log(' +-----+'); | |
144 | console.log(' +---------+'); | |
145 | console.log(' /| [][] |\\'); | |
146 | console.log(' || | |'); | |
147 | console.log(' || | \\c'); | |
148 | console.log(' ^+---------+'); | |
149 | console.log(' (.) '); | |
150 | } | |
cc69fef4 | 151 | }; |