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