]>
git.r.bdr.sh - rbdr/dasein/blob - lib/dasein.js
5b8486a09806a132be9ae8d262b39c331a677313
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');
11 const AuthHandler
= require('./handlers/auth');
12 const PostsHandler
= require('./handlers/posts');
13 const CommentsHandler
= require('./handlers/comments');
17 internals
.k401Location
= '/401.html';
18 internals
.kMainLocation
= '/';
21 * The Dasein class is the main entry point for the application.
24 * @param {Dasein.tConfiguration} config the initialization options to
27 module
.exports
= internals
.Dasein
= class Dasein
{
31 Object
.assign(this, config
);
35 * Initializes the application and starts listening. Also prints a
36 * nice robotic banner with information.
44 this._initializeServer();
48 return Promise
.resolve();
51 // Initializes the Koa application and all the handlers.
59 this._app
.use(KoaStatic(this.staticDirectory
));
60 this._app
.use(KoaBodyParser());
64 this._app
.use(function * (next
) {
70 this.status
= err
.status
|| 500;
77 if (response
.status
=== 401) {
78 response
.error
=== 'Protected resource, use Authorization header to get access';
83 this.app
.emit('error', err
, this);
87 this._app
.use(KoaJwt({
88 secret: this.jwt
.secret
,
92 this._initializeAuthRoutes();
93 this._initializePostsRoutes();
94 this._initializeCommentsRoutes();
96 this._app
.use(function * () {
98 yield KoaSend(this, Path
.join(self
.staticDirectory
, 'index.html'));
103 // Initialize routes for auth
105 _initializeAuthRoutes() {
107 const authHandler
= new AuthHandler({
109 twitter: this.twitter
111 this._app
.use(KoaRoute
.get('/api/auth/login', authHandler
.login()));
112 this._app
.use(KoaRoute
.post('/api/auth/callback', authHandler
.callback()));
115 // Initialize routes for posts
117 _initializePostsRoutes() {
119 const postsHandler
= new PostsHandler({
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()));
130 // Initialize routes for comments
132 _initializeCommentsRoutes() {
134 const commentsHandler
= new CommentsHandler({
138 this._app
.use(KoaRoute
.get('/api/posts/:postId/comments', commentsHandler
.findAll()));
139 this._app
.use(KoaRoute
.post('/api/posts/:postId/comments', commentsHandler
.create()));
146 this._app
.listen(this.port
);
149 // Prints the banner.
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(' (.) ');