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