aboutsummaryrefslogtreecommitdiff
path: root/lib/dasein.js
diff options
context:
space:
mode:
authorRubén Beltrán del Río <ben@nsovocal.com>2017-01-30 02:47:14 -0600
committerGitHub <noreply@github.com>2017-01-30 02:47:14 -0600
commita6ccda0fbc4df683f9568d85eb22b21684d2a0bd (patch)
tree967e98f2d8bf6718858da1fbc279987208b25e06 /lib/dasein.js
parent287fa13b3e600b2340895a5463a288bf08101bb5 (diff)
Create and Show Posts (#3)
* 📝 Update changelog (retroactive) SORRY * Add JSDoc * Document twitter helper * Add posts handler * Remove cookies, set routes under /api * Update readme for clearer callback instructions * Redirect callback to login with token * Add dasein paw helper * Remove unused hostname * Remove hostname, add expiration to config * Rename expiration ttl * Send TTL to posts handler * Add redis dependency * Add redis config to config file * Add DASEIN_REDIS_HOST to env.dist * Correct redis config structure * Add redis db to compose * Add redis to posts handler * Update dasein paw file * Git add uuid * Add first iteration of post handlers * Ignore docs in linter * Ignore generated assets * Ignore docs and assets * Add frontend dependencies + more koa * Add post creation to backend * Adjust readme to show actual callback route * Add npm build as part of docker process * Configure babel * Update paw * Add webpack config * Add frontend code * List globals
Diffstat (limited to 'lib/dasein.js')
-rw-r--r--lib/dasein.js91
1 files changed, 68 insertions, 23 deletions
diff --git a/lib/dasein.js b/lib/dasein.js
index 38654a4..3a537bd 100644
--- a/lib/dasein.js
+++ b/lib/dasein.js
@@ -1,17 +1,28 @@
'use strict';
const Koa = require('koa');
+const KoaBodyParser = require('koa-bodyparser');
const KoaJwt = require('koa-jwt');
-const KoaStatic = require('koa-static');
const KoaRoute = require('koa-route');
+const KoaSend = require('koa-send');
+const KoaStatic = require('koa-static');
+const Path = require('path');
const AuthHandler = require('./handlers/auth');
+const PostsHandler = require('./handlers/posts');
const internals = {};
internals.k401Location = '/401.html';
internals.kMainLocation = '/';
+/**
+ * The Dasein class is the main entry point for the application.
+ *
+ * @class Dasein
+ * @param {Dasein.tConfiguration} config the initialization options to
+ * extend the instance
+ */
module.exports = internals.Dasein = class Dasein {
constructor(config) {
@@ -19,6 +30,14 @@ module.exports = internals.Dasein = class Dasein {
Object.assign(this, config);
}
+ /**
+ * Initializes the application and starts listening. Also prints a
+ * nice robotic banner with information.
+ *
+ * @function run
+ * @memberof Dasein
+ * @instance
+ */
run() {
this._initializeServer();
@@ -28,15 +47,18 @@ module.exports = internals.Dasein = class Dasein {
return Promise.resolve();
}
+ // Initializes the Koa application and all the handlers.
+
_initializeServer() {
- this._app = Koa();
+ const self = this;
- this._app.keys = this.cookieKeys;
+ this._app = Koa();
this._app.use(KoaStatic(this.staticDirectory));
+ this._app.use(KoaBodyParser());
- // Redirect all 401s to the 401 static page
+ // Error handler
this._app.use(function * (next) {
@@ -44,51 +66,74 @@ module.exports = internals.Dasein = class Dasein {
yield next;
}
catch (err) {
- if (err.status === 401) {
- return this.redirect(internals.k401Location);
+ this.status = err.status || 500;
+
+ const response = {
+ error: err.message,
+ status: this.status
+ };
+
+ if (response.status === 401) {
+ response.error === 'Protected resource, use Authorization header to get access';
}
- throw err;
+ this.body = response;
+
+ this.app.emit('error', err, this);
}
});
this._app.use(KoaJwt({
secret: this.jwt.secret,
- passthrough: true,
- cookie: this.jwt.cookieName
+ passthrough: true
}));
- // Handlers for Twitter Auth Related Routes
+ this._initializeAuthRoutes();
+ this._initializePostsRoutes();
+
+ this._app.use(function * () {
+
+ yield KoaSend(this, Path.join(self.staticDirectory, 'index.html'));
+ });
+
+ }
+
+ // Initialize routes for auth
+
+ _initializeAuthRoutes() {
const authHandler = new AuthHandler({
- hostname: this.hostname,
jwt: this.jwt,
twitter: this.twitter
});
- this._app.use(KoaRoute.get('/login', authHandler.login()));
- this._app.use(KoaRoute.get('/login-callback', authHandler.callback()));
- this._app.use(KoaRoute.get('/logout', authHandler.logout()));
+ this._app.use(KoaRoute.get('/api/auth/login', authHandler.login()));
+ this._app.use(KoaRoute.post('/api/auth/callback', authHandler.callback()));
+ }
- // The index
+ // Initialize routes for posts
- this._app.use(function * () {
+ _initializePostsRoutes() {
- if (this.state.user) {
- this.body = `<img src="${this.state.user.profile_image_url_https}"> Hello ${this.state.user.screen_name}`;
- return;
- }
-
- this.body = 'Go to /login to login';
- return;
+ const postsHandler = new PostsHandler({
+ ttl: this.ttl,
+ redis: this.redis
});
+ this._app.use(KoaRoute.get('/api/posts', postsHandler.findAll()));
+ this._app.use(KoaRoute.get('/api/posts/:id', postsHandler.find()));
+ this._app.use(KoaRoute.post('/api/posts', postsHandler.create()));
+ this._app.use(KoaRoute.delete('/api/posts/:id', postsHandler.delete()));
+
}
+ // Starts listening
+
_startServer() {
this._app.listen(this.port);
}
// Prints the banner.
+
_printBanner() {
console.log(' .');