Skip to content

Commit

Permalink
updated files
Browse files Browse the repository at this point in the history
  • Loading branch information
jonniespratley committed Dec 14, 2014
1 parent 88f0670 commit bceb19c
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 86 deletions.
5 changes: 4 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ module.exports = function (grunt) {
// Change this to '0.0.0.0' to access the server from outside.
hostname: '127.0.0.1',
livereload: 35729,
base: ['.tmp', '<%= yeoman.app %>']
base: ['.tmp', '<%= yeoman.app %>'],
onCreateServer: function(server, connect, options) {
console.warn('onCreateServer', options);
}
},
livereload: {
options: {
Expand Down
8 changes: 4 additions & 4 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"hostname": "localhost",
"port": 5001
},
"staticDir": "/dist",
"publicDir": "/app",
"uploadsTmpDir": "/.tmp",
"uploadsDestDir": "/www/cms-content/uploads",
"staticDir": "dist",
"publicDir": "www",
"uploadsTmpDir": ".tmp",
"uploadsDestDir": "www/cms-content/uploads",
"uploadsUrl": ":8181/cms-content/",
"logFormat": "[:date] - [:method] - :url - :status - :response-time ms"
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"logfmt": "~0.18.1",
"markdown": "~0.5.0",
"method-override": "~1.0.0",
"mongodb": "*",
"mongodb": "^1.4.23",
"mongoose": "^3.8.20",
"passport": "^0.2.1",
"passport-facebook": "^1.0.3",
Expand All @@ -59,7 +59,8 @@
"promised-io": "~0.3.4",
"q": "^1.1.2",
"request": "~2.34.0",
"socket.io": "~0.9.16"
"socket.io": "~0.9.16",
"websocket": "^1.0.14"
},
"devDependencies": {
"chai": "^1.10.0",
Expand Down
64 changes: 41 additions & 23 deletions routes/cms-passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var passport = require('passport'),
BasicStrategy = require('passport-http').Strategy,
BasicStrategy = require('passport-http').BasicStrategy,
LocalStrategy = require('passport-local').Strategy,
GoogleStrategy = require('passport-google').Strategy,
express = require('express'),
Expand Down Expand Up @@ -38,18 +38,39 @@ var ensureAuthenticated = function (req, res, next) {
*/
var cmsPassport = function (config, app) {

var user = new User();

if (!app) {
throw new Error('Must provide express application!');
}

/*
*/
var findOrCreate = function(u, done){
console.log('find', u);

for (var i = 0; i < u.emails.length; i++) {
var email = u.emails[i].value
User.findOne({ email: email }, function (err, user) {
if (err) { return done(err); }
if (!user) {
console.warn('create user', u);
return done(null, false);
}
return done(null, user);
});
}

};

/**
* I handle serializing a user.
* @param user
* @param done
*/
var serializeUser = function (user, done) {
done(null, user.id);
done(null, user._id);
};

/**
Expand All @@ -67,26 +88,18 @@ var cmsPassport = function (config, app) {

passport.deserializeUser(deserializeUser);

passport.use(new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true
}, function (username, password, done) {
console.warn('username', username, password);
process.nextTick(function () {
console.warn('find by username');
User.find({
username: username
}, function (err, user) {
console.warn(err, user);
if (err) {
return done(null, false);
}
var strategy = function(username, password, done) {
console.warn('find user', username, password);
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.validPassword(password)) { return done(null, false); }
return done(null, user);
});
});
}
));
};

passport.use(new BasicStrategy(strategy));
passport.use(new LocalStrategy(strategy));

passport.use(new GoogleStrategy({
returnURL: 'http://localhost:8181/auth/google/return',
Expand All @@ -95,7 +108,7 @@ var cmsPassport = function (config, app) {
function (identifier, profile, done) {
console.warn('googleCallback', profile);
profile.openId = identifier;
User.findOrCreate(profile, function (err, user) {
findOrCreate(profile, function (err, user) {
done(err, user);
});
}
Expand All @@ -117,11 +130,13 @@ var cmsPassport = function (config, app) {
app.use(passport.session());
app.use(flash());


app.get('/api/me', passport.authenticate('basic', {session: false}), function (req, res) {
res.json(req.user);
});

app.all('*', function (req, res, next) {
req.config = JSON.stringify(config);
console.warn('cmsAuth', req.params);
next();
});
Expand All @@ -131,7 +146,10 @@ var cmsPassport = function (config, app) {
});

app.get('/account', ensureAuthenticated, function (req, res) {
res.render('account', {user: req.user});
res.render('account', {
user: req.user,
config: req.config
});
});

app.get('/login', function (req, res) {
Expand All @@ -140,7 +158,7 @@ var cmsPassport = function (config, app) {

app.post('/login',
passport.authenticate('local', {
successRedirect: '/',
successRedirect: '/account',
failureRedirect: '/login',
failureFlash: false
})
Expand Down
25 changes: 17 additions & 8 deletions routes/cms-rest.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
var bodyParser = require( 'body-parser' ),
var express = require('express'),
bodyParser = require( 'body-parser' ),
session = require( 'express-session' ),
RestResource = require( './rest' );

module.exports = function (config, app) {
'use strict';
var router = express.Router();

app.get( config.apiBase + '/plugins', RestResource.plugins );
app.get( config.apiBase + '/readme', RestResource.readme );
app.get( config.apiBase, RestResource.index );
// a middleware with no mount path, gets executed for every request to the router
router.use(function (req, res, next) {
console.log('cms-rest Time:', Date.now());
next();
});

router.get( config.apiBase, RestResource.index );
router.get( config.apiBase + '/plugins', RestResource.plugins );
router.get( config.apiBase + '/readme', RestResource.readme );

//Dynamic REST
app.get( config.apiBase + '/:db/:collection/:id?', RestResource.get )
app.post( config.apiBase + '/:db/:collection/:id?', bodyParser.json(), RestResource.add )
app.put( config.apiBase + '/:db/:collection/:id?', bodyParser.json(), RestResource.edit )
app.delete( config.apiBase + '/:db/:collection/:id?', RestResource.destroy );
router.get( config.apiBase + '/:db/:collection/:id?', RestResource.get )
router.post( config.apiBase + '/:db/:collection/:id?', bodyParser.json(), RestResource.add )
router.put( config.apiBase + '/:db/:collection/:id?', bodyParser.json(), RestResource.edit )
router.delete( config.apiBase + '/:db/:collection/:id?', RestResource.destroy );

console.warn( 'cms-rest', 'initialized' );

app.use('/', router);
};
17 changes: 14 additions & 3 deletions routes/cms-routes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
var mongoose = require('mongoose');
var mongoose = require('mongoose'), http = require('http'), util = require('util');

module.exports.mount = function (config, app) {
'use strict';

//Connect to database
mongoose.connect(config.mongodb);

var server = http.createServer(app);

require( './cms-auth' )( config, app );
require( './cms-passport' )( config, app );
require( './cms-rest' )( config, app );
require( './cms-proxy' )( config, app );
require( './cms-sockets' )( config, app );
require( './cms-server' )( config, app );
require( './cms-upload' )( config, app );
require( './cms-sockets' )( config, server );
require( './cms-server' )( config, app );

return server.listen(config.port || process.env.PORT, function () {
util.log('App listening on port: ' + config.port + ''.verbose);
util.log(util.inspect(config, {colors: true}));
});
};
30 changes: 29 additions & 1 deletion routes/cms-server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
var express = require('express'),
path = require('path'),
bodyParser = require( 'body-parser' );

module.exports = function (config, app) {
console.warn( 'cms-server');
console.warn( 'cms-server initialized');

var router = express.Router();

var options = {
dotfiles: 'ignore',
etag: false,
extensions: ['htm', 'html'],
index: false,
maxAge: '1d',
redirect: false,
setHeaders: function (res, path) {
res.set('x-timestamp', Date.now())
}
};

router.use(express.static(config.staticDir, options));
router.use(express.static(config.publicDir, options));
router.use('/', function(res, req, next){
req.send(config.publicDir + path.sep + 'index.html');
next();
});

app.use('/', router);

};
104 changes: 99 additions & 5 deletions routes/cms-sockets.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,105 @@

const events = require('events'), util = require('util');

var cmsSockets = function (config, app) {
var q = require( 'q' ),
WebSocketServer = require('websocket').server,
WebSocketRouter = require('websocket').router;
////////////////////////////
//## Socket Server
//This is a socket server implementation for 'real' time analytics and other data.
//This is for use with geo analytics and other backend data from the app. listen for connected clients
//
// ### Server Channels
//These are the events that this socket server dispatches.
//
//1. cms:authorization
//2. cms:client:message
//3. cms:client:connect
//4. cms:client:disconnect
//5. cms:server:message
//6. cms:server:disconnect
//7. cms:server:connect
//8. cms:
module.exports = function (config, app) {
'use strict';
events.EventEmitter.call(this);
console.warn( 'cms-sockts initialized' );

console.warn( 'cms-sockts' );
//Start the websocket server
//SocketServer.init(proxyServer);
var cmsSockets = {},
connections, wsserver, wsclient, router, self = cmsSockets;

};

util.inherits(cmsSockets, events.EventEmitter);
module.exports = cmsSockets;
var delay = function (fn, time) {
var defer = q.defer();
setTimeout( function () {
fn();
defer.resolve();
}, time );
return defer.promise;
};

var delayedSocketPush = function(socket, time){
return delay( function (msg) {
socket.emit( 'msg', {
datetime: new Date(),
message: msg,
id: 'Server'
} );
}, time );
};

//Hold the names of events that this socket server listens for and emits
self.events = {
session: {
pageView: 'cms:session:pageView',
hashChange: 'cms:session:hashChange',
login: 'cms:session:login',
logout: 'cms:session:logout'
},
server: {
message: 'cms:server:message',
connected: 'cms:server:connect',
disconnected: 'cms:server:disconnect'
},
client: {
message: 'cms:client:message',
connected: 'cms:client:connect',
disconnected: 'cms:client:disconnect'
}
};


//Store a list of the connected clients
connections = [];

var serverConfig = {
httpServer: app,
};

wsserver = new WebSocketServer();
wsserver.mount(serverConfig);

router = new WebSocketRouter();
router.attachServer(wsserver);

router.mount('*', 'echo-protocol', function(request) {
console.log('mounted to echo protocol');
var conn = request.accept(request.origin);
conn.on('message', function(message) {
console.log('routed message', message);
});
conn.send('hey');
});

router.mount('*', 'update-protocol', function(request) {
console.log('mounted to update protocol');
var conn = request.accept(request.origin);
conn.on('message', function(message) {
console.log('update all the things', message);
}); });

util.inherits(cmsSockets, events.EventEmitter);

};
2 changes: 1 addition & 1 deletion routes/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ UserSchema.method( 'findOrCreate', function (profile, fn) {

UserSchema.method( 'findByUsername', function (username, fn) {
console.warn( 'findByUsername', username );
this.find( {username: username}, function (err, data) {
UserSchema.find( {username: username}, function (err, data) {
if (err) {
fn( false, err );
}
Expand Down
Loading

0 comments on commit bceb19c

Please sign in to comment.