]> git.r.bdr.sh - rbdr/dasein/blame_incremental - lib/dasein.js
Merge branch 'release/1.0.0'
[rbdr/dasein] / lib / dasein.js
... / ...
CommitLineData
1'use strict';
2
3const Koa = require('koa');
4const KoaBodyParser = require('koa-bodyparser');
5const KoaJwt = require('koa-jwt');
6const KoaRoute = require('koa-route');
7const KoaSend = require('koa-send');
8const KoaStatic = require('koa-static');
9const Path = require('path');
10
11const AuthHandler = require('./handlers/auth');
12const PostsHandler = require('./handlers/posts');
13const CommentsHandler = require('./handlers/comments');
14
15const internals = {};
16
17internals.k401Location = '/401.html';
18internals.kMainLocation = '/';
19
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 */
27module.exports = internals.Dasein = class Dasein {
28
29 constructor(config) {
30
31 Object.assign(this, config);
32 }
33
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 */
42 run() {
43
44 this._initializeServer();
45 this._startServer();
46 this._printBanner();
47
48 return Promise.resolve();
49 }
50
51 // Initializes the Koa application and all the handlers.
52
53 _initializeServer() {
54
55 const self = this;
56
57 this._app = Koa();
58
59 this._app.use(KoaStatic(this.staticDirectory));
60 this._app.use(KoaBodyParser());
61
62 // Error handler
63
64 this._app.use(function * (next) {
65
66 try {
67 yield next;
68 }
69 catch (err) {
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';
79 }
80
81 this.body = response;
82
83 this.app.emit('error', err, this);
84 }
85 });
86
87 this._app.use(KoaJwt({
88 secret: this.jwt.secret,
89 passthrough: true
90 }));
91
92 this._initializeAuthRoutes();
93 this._initializePostsRoutes();
94 this._initializeCommentsRoutes();
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() {
106
107 const authHandler = new AuthHandler({
108 jwt: this.jwt,
109 twitter: this.twitter
110 });
111 this._app.use(KoaRoute.get('/api/auth/login', authHandler.login()));
112 this._app.use(KoaRoute.post('/api/auth/callback', authHandler.callback()));
113 }
114
115 // Initialize routes for posts
116
117 _initializePostsRoutes() {
118
119 const postsHandler = new PostsHandler({
120 ttl: this.ttl,
121 redis: this.redis
122 });
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
128 }
129
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
142 // Starts listening
143
144 _startServer() {
145
146 this._app.listen(this.port);
147 }
148
149 // Prints the banner.
150
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 }
165};