Permalink
Browse files

Initial hapi 0.16

  • Loading branch information...
1 parent 1a80a1d commit 812689dee0850c125d6da8559f43894584c75b1a @hueniverse committed Apr 2, 2013
View
@@ -1,4 +1,4 @@
-Postmile is a collaborative list making tool built using JS, Node.js, and MongoDB.
+Postmile is a collaborative list making tool built using JS, Node.js, and MongoDB.
# Installation
@@ -27,11 +27,11 @@ If your MongoDB requires authentication, set the values of the database 'usernam
```bash
$ node install
-110827/005720.948, info, Database initialized
-110827/005720.952, info, Initial dataset created successfully
-110827/005720.952, info, >>>>> WEB client id: <id>
-110827/005720.952, info, >>>>> WEB client secret: <secret>
-110827/005720.952, info, >>>>> VIEW client id: <id>
+Database initialized
+Initial dataset created successfully
+>>>>> WEB client id: <id>
+>>>>> WEB client secret: <secret>
+>>>>> VIEW client id: <id>
```
Copy the WEB client id and secret, and VIEW client id, and save them for later.
@@ -41,7 +41,7 @@ $ cd ../web
$ cp vault.js.example vault.js
```
-Edit postmile/web/vault.js and set the values of the 'aes256Key' variables to different random secrets sufficiently long (e.g. 40 characters).
+Edit postmile/web/vault.js and set the values of the 'password' variables to different random secrets sufficiently long (e.g. 40 characters).
Set the values of the postmileAPI 'clientId' and 'clientSecret' variables to the WEB client id and secret saved earlier.
Set the value of the 'viewClientId' variable to the VIEW client id saved earlier.
View
@@ -16,14 +16,14 @@ var internals = {};
// Task details
exports.get = {
- validate: {
- query: {
- since: Hapi.types.Number().min(0)
- }
+ validate: {
+ query: {
+ since: Hapi.types.Number().min(0)
+ }
},
handler: function (request) {
- internals.load(request.params.id, request.session.user, false, function (err, details, task, project) {
+ internals.load(request.params.id, request.auth.credentials.user, false, function (err, details, task, project) {
details = details || { id: request.params.id, thread: [] };
@@ -89,15 +89,15 @@ exports.post = {
var post = function () {
- internals.load(request.params.id, request.session.user, true, function (err, details, task, project) {
+ internals.load(request.params.id, request.auth.credentials.user, true, function (err, details, task, project) {
if (err || !task) {
return request.reply(err);
}
var detail = request.payload;
detail.created = now;
- detail.user = request.session.user;
+ detail.user = request.auth.credentials.user;
if (details) {
@@ -134,7 +134,7 @@ exports.post = {
var finalize = function (task, project) {
if (request.query.last === 'true') {
- Last.setLast(request.session.user, project, task, function (err) { }); // Ignore response
+ Last.setLast(request.auth.credentials.user, project, task, function (err) { }); // Ignore response
}
Stream.update({ object: 'details', project: task.project, task: task._id }, request);
View
@@ -417,7 +417,7 @@ internals.sendEmail = function (to, subject, text) {
mailer.send(message, function (err, message) {
if (err) {
- Hapi.log.event('err', 'Email error', { to: to, subject: subject, text: text, error: err });
+ console.log('Email error', { to: to, subject: subject, text: text, error: err });
}
});
};
View
@@ -16,28 +16,21 @@ var Vault = require('./vault');
var internals = {};
-// Catch uncaught exceptions
-
-process.on('uncaughtException', function (err) {
-
- Hapi.Utils.abort('Uncaught exception: ' + err.stack);
-});
-
-
// Post handler extension middleware
-internals.onPostHandler = function (request, next) {
+internals.onPreResponse = function (request, next) {
- if (request.response instanceof Hapi.Boom ||
- !request.response.varieties.obj ||
- request.response.raw instanceof Array) {
+ var response = request.response();
+ if (response instanceof Hapi.Boom ||
+ !response.varieties.obj ||
+ response.raw instanceof Array) {
return next();
}
// Sanitize database fields
- var payload = request.response.raw;
+ var payload = response.raw;
if (payload._id) {
payload.id = payload._id;
@@ -52,40 +45,46 @@ internals.onPostHandler = function (request, next) {
}
}
- request.response.update();
+ response.update();
return next();
};
// Create server
-var configuration = {
+var options = {
auth: {
scheme: 'oz',
+ defaultMode: true,
encryptionPassword: Vault.ozTicket.password,
-
loadAppFunc: Session.loadApp,
loadGrantFunc: Session.loadGrant,
tos: 20110623
},
- monitor: true,
cors: true
};
-var server = new Hapi.Server(Config.host.api.domain, Config.host.api.port, configuration);
-server.ext('onPostHandler', internals.onPostHandler);
-server.route(Routes.endpoints);
+var server = new Hapi.Server(Config.host.api.domain, Config.host.api.port, options);
-Db.initialize(function (err) {
+server.plugin.allow({ ext: true }).require('good', function (err) {
- if (err) {
- Hapi.log.event('err', err);
- process.exit(1);
- }
+ Hapi.utils.assert(!err, 'Failed loading plugin: ' + err);
+
+ server.ext('onPreResponse', internals.onPreResponse);
+ server.route(Routes.endpoints);
+
+ Db.initialize(function (err) {
- Suggestions.initialize();
- Tips.initialize();
- server.start();
- Stream.initialize(server.listener);
+ if (err) {
+ console.log(err);
+ process.exit(1);
+ }
+
+ Suggestions.initialize();
+ Tips.initialize();
+ server.start();
+ Stream.initialize(server.listener);
+ });
});
+
View
@@ -13,11 +13,11 @@ Db.initialize(true, function (err) {
// Database connection failed
- Hapi.log.event('err', err);
+ console.log(err);
process.exit(1);
}
- Hapi.log.event('info', 'Database initialized');
+ console.log('Database initialized');
// Create required clients
@@ -36,7 +36,7 @@ Db.initialize(true, function (err) {
Db.insert('client', clients, function (err, items) {
if (err) {
- Hapi.log.event('err', err);
+ console.log(err);
process.exit(1);
}
@@ -45,14 +45,14 @@ Db.initialize(true, function (err) {
Db.insert('invite', { code: 'public' }, function (err, items) {
if (err) {
- Hapi.log.event('err', err);
+ console.log(err);
process.exit(1);
}
- Hapi.log.event('info', 'Initial dataset created successfully');
- Hapi.log.event('info', '>>>>> WEB client id: ' + clients[0]._id);
- Hapi.log.event('info', '>>>>> WEB client secret: ' + clients[0].secret);
- Hapi.log.event('info', '>>>>> VIEW client id: ' + clients[1]._id);
+ console.log('Initial dataset created successfully');
+ console.log('>>>>> WEB client id: ' + clients[0]._id);
+ console.log('>>>>> WEB client secret: ' + clients[0].secret);
+ console.log('>>>>> VIEW client id: ' + clients[1]._id);
process.exit(0);
});
});
View
@@ -10,9 +10,7 @@ var Stream = require('./stream');
// Check invitation code
exports.get = {
- auth: {
- mode: 'none'
- },
+ auth: false,
handler: function (request) {
// Check invitation code type
@@ -141,7 +139,7 @@ exports.claim = {
return request.reply(Hapi.Error.badRequest('Invalid invitation code'));
}
- Project.replacePid(project, projectPid.pid, request.session.user, function (err) {
+ Project.replacePid(project, projectPid.pid, request.auth.credentials.user, function (err) {
if (err) {
return request.reply(err);
View
@@ -17,7 +17,7 @@ exports.getProject = {
handler: function (request) {
- exports.load(request.session.user, function (err, last) {
+ exports.load(request.auth.credentials.user, function (err, last) {
if (err) {
return request.reply(err);
@@ -27,7 +27,7 @@ exports.getProject = {
!last.projects ||
!last.projects[request.params.id]) {
- return request.reply({ id: request.session.user, projects: {} });
+ return request.reply({ id: request.auth.credentials.user, projects: {} });
}
var record = { id: last._id, projects: {} };
@@ -44,13 +44,13 @@ exports.postProject = {
handler: function (request) {
- Project.load(request.params.id, request.session.user, false, function (err, project, member) {
+ Project.load(request.params.id, request.auth.credentials.user, false, function (err, project, member) {
if (err || !project) {
return request.reply(err);
}
- exports.setLast(request.session.user, project, null, function (err) {
+ exports.setLast(request.auth.credentials.user, project, null, function (err) {
if (err) {
return request.reply(err);
@@ -69,13 +69,13 @@ exports.getTask = {
handler: function (request) {
- Task.load(request.params.id, request.session.user, false, function (err, task, project) {
+ Task.load(request.params.id, request.auth.credentials.user, false, function (err, task, project) {
if (err || !task) {
return request.reply(err);
}
- exports.load(request.session.user, function (err, last) {
+ exports.load(request.auth.credentials.user, function (err, last) {
if (err) {
return request.reply(err);
@@ -87,7 +87,7 @@ exports.getTask = {
!last.projects[task.project].tasks ||
!last.projects[task.project].tasks[request.params.id]) {
- return request.reply({ id: request.session.user, projects: {} });
+ return request.reply({ id: request.auth.credentials.user, projects: {} });
}
var record = { id: last._id, projects: {} };
@@ -106,13 +106,13 @@ exports.postTask = {
handler: function (request) {
- Task.load(request.params.id, request.session.user, false, function (err, task, project) {
+ Task.load(request.params.id, request.auth.credentials.user, false, function (err, task, project) {
if (err || !task) {
return request.reply(err);
}
- exports.setLast(request.session.user, project, task, function (err) {
+ exports.setLast(request.auth.credentials.user, project, task, function (err) {
return request.reply(err || { status: 'ok' });
});
Oops, something went wrong.

0 comments on commit 812689d

Please sign in to comment.