Skip to content

Commit

Permalink
improved mongo crud
Browse files Browse the repository at this point in the history
  • Loading branch information
kidtronnix committed Aug 31, 2014
1 parent bda0a00 commit 5ce56dd
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 104 deletions.
7 changes: 7 additions & 0 deletions Makefile
@@ -0,0 +1,7 @@
test:
@node node_modules/lab/bin/lab
test-cov:
@node node_modules/lab/bin/lab -t 100
test-cov-html:
@node node_modules/lab/bin/lab -r html -o coverage.html
.PHONY: test test-cov test-cov-html
1 change: 0 additions & 1 deletion configure.js
@@ -1,6 +1,5 @@
var prompt = require('prompt');
var fs = require('fs');

// Remove annoying "prompt:"
prompt.message = "";
prompt.delimiter = "";
Expand Down
11 changes: 7 additions & 4 deletions package.json
Expand Up @@ -13,10 +13,11 @@
"node": "0.10.x"
},
"scripts": {
"test": "make test-cov",
"start": "node start.js"
},
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"bcryptjs": "^2.0.2",
"bower": "~1.3.5",
"extend": "^1.3.0",
"good": "~2.1.2",
Expand All @@ -36,14 +37,16 @@
"prompt": "^0.2.13",
"require-directory": "~1.2.0",
"socket.io": "^1.0.6",
"swig": "~1.3.2"
"swig": "~1.3.2",
"toothache": "^0.1.3"
},
"devDependencies": {
"gulp": "~3.8.0",
"gulp-util": "~2.2.17",
"gulp-concat": "~2.2.0",
"gulp-imagemin": "~0.6.1",
"gulp-minify-css": "~0.3.4",
"gulp-uglify": "~0.3.0",
"gulp-imagemin": "~0.6.1"
"gulp-util": "~2.2.17",
"lab": "^4.1.0"
}
}
149 changes: 58 additions & 91 deletions servers/api/User/index.js
Expand Up @@ -3,60 +3,53 @@
*/

// Packages for validation
var Hapi = require('hapi');
var Joi = require('joi');


var apiGenKey = function(length) {
// Just produces random string using these chars
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
return result;
}

// Internal config stuff
var internals = {
// All config for mongo-crud
CRUD: {
var CRUD = {
collection: 'users',
create: {
bcrypt: 'password',
create: Joi.object().keys({
date: 'created',
payload: Joi.object().keys({
email: Joi.string().required(),
password: Joi.string().required(),
fname: Joi.string().required(),
lname: Joi.string().required(),
access: Joi.string(),
apiToken: Joi.string().required(),
}),
update: Joi.object().keys({
defaults: {
access: 'normal',
activated: false
},
},
update: {
bcrypt: 'password',
date: 'updated',
payload: Joi.object().keys({
password: Joi.string(),
fname: Joi.string(),
lname: Joi.string(),
activated: Joi.boolean(),
access: Joi.string(),
guiToken: [Joi.string(), Joi.boolean()],
forgotToken: [Joi.string(), Joi.boolean()]
}),
defaults: {
access: 'normal',
guiToken: false,
forgotToken: false,
activated: false
},
validationOpts: {
abortEarly: false
}
})
},
validationOpts: {
abortEarly: false
}
};


exports.register = function(plugin, options, next) {

// Add db to our config
internals.CRUD.db = options.db;
CRUD.db = options.db;

// Require MongoCrud functions
var MongoCrud = require(__dirname+'/../../../lib/mongo-crud')(internals.CRUD);
// Require User functions
var User = require('toothache')(CRUD);

var apiGenKey = function(request, next) {
var generate = function(length) {
Expand All @@ -74,99 +67,73 @@ exports.register = function(plugin, options, next) {
next(apiKey);
}

var create = function (plugin, next) {
return {
// Create
plugin.route({
path: "/api/user",
method: "POST",
config: {
auth: 'core',
pre: [
{ method: apiGenKey, assign: 'apiGenKey' }
],
handler: MongoCrud.create,
handler: User.create
}
};

var all = function (plugin, next) {
return {
auth: 'core',
handler: MongoCrud.getAll
}
};
});

var get = function (plugin, next) {

return {
auth: 'core',
handler: MongoCrud.get,
validate: {
params: {
id: Joi.string().min(12)
}
}
}
};

var update = function (plugin, next) {

return {
auth: 'core',
handler: MongoCrud.update,
validate: {
params: {
id: Joi.string().min(12)
}
}
}
};

var del = function (plugin, next) {

return {
// Get all
plugin.route({
path: "/api/user/{id}",
method: "GET",
config: {
auth: 'core',
handler: MongoCrud.del,
handler: User.get,
validate: {
params: {
id: Joi.string().min(12)
}
}
}
};



// Create
plugin.route({
path: "/api/user",
method: "POST",
config: create()
});

// Read
plugin.route({
path: "/api/user/{id}",
method: "GET",
config: get()
});

// Get ind.
plugin.route({
path: "/api/user",
method: "GET",
config: all()
config: {
auth: 'core',
handler: User.getAll
}
});

// Update
plugin.route({
path: "/api/user/{id}",
method: "PUT",
config: update()
config: {
auth: 'core',
handler: User.update,
validate: {
params: {
id: Joi.string().min(12)
}
}
}
});

// Delete
plugin.route({
path: "/api/user/{id}",
method: "DELETE",
config: del()
config: {
auth: 'core',
handler: User.del,
validate: {
params: {
id: Joi.string().min(12)
}
}
}
});

next();


next();
}
13 changes: 10 additions & 3 deletions servers/api/server.js
Expand Up @@ -110,6 +110,13 @@ apiServer.pack.register([
if (err) throw err;
});

apiServer.start();
var message = 'Api started at: ' + apiServer.info.uri;
console.log(message);
if (!module.parent) {

This comment has been minimized.

Copy link
@Unitech

Unitech Sep 16, 2014

Hiha

apiServer.start(function() {
var message = 'API started at: ' + apiServer.info.uri;
console.log(message);
});
}

module.exports = apiServer;


2 changes: 1 addition & 1 deletion servers/gui/auth/index.js
Expand Up @@ -3,7 +3,7 @@
*/

var Joi = require('joi');
var Bcrypt = require('bcrypt-nodejs');
var Bcrypt = require('bcryptjs');
var Jwt = require('jsonwebtoken');
var nodemailer = require('nodemailer');
var Nipple = require('nipple');
Expand Down
12 changes: 8 additions & 4 deletions servers/gui/server.js
Expand Up @@ -16,8 +16,12 @@ var routes = require('./config/routes')(server);
// Add the server routes
server.route(routes);

if (!module.parent) {
server.start(function() {
var message = 'GUI started at: ' + server.info.uri;
console.log(message);
});
}

module.exports = server;

server.start(function() {
//Log to the console the host and port info
console.log('GUI started at: ' + server.info.uri);
});

0 comments on commit 5ce56dd

Please sign in to comment.