@@ -10,7 +10,11 @@ var path = require('path');
1010var mongoose = require ( 'mongoose' ) ;
1111var passport = require ( 'passport' ) ;
1212var expressValidator = require ( 'express-validator' ) ;
13-
13+ var http = require ( 'http' ) ;
14+ var io = require ( 'socket.io' ) ;
15+ var app = express ( )
16+ , server = require ( 'http' ) . createServer ( app )
17+ , io = io . listen ( server ) ;
1418
1519/**
1620 * Load controllers.
@@ -20,6 +24,7 @@ var homeController = require('./controllers/home');
2024var userController = require ( './controllers/user' ) ;
2125var apiController = require ( './controllers/api' ) ;
2226var contactController = require ( './controllers/contact' ) ;
27+ var dashboardController = require ( './controllers/dashboard' ) ;
2328
2429/**
2530 * API keys + Passport configuration.
@@ -37,11 +42,15 @@ mongoose.connection.on('error', function() {
3742 console . log ( '✗ MongoDB Connection Error. Please make sure MongoDB is running.' . red ) ;
3843} ) ;
3944
40- var app = express ( ) ;
41-
4245/**
4346 * Express configuration.
4447 */
48+
49+ var hour = 3600000 ; //milliseconds
50+ var day = ( hour * 24 ) ;
51+ var week = ( day * 7 ) ;
52+ var month = ( day * 30 ) ;
53+
4554app . locals . cacheBuster = Date . now ( ) ;
4655app . set ( 'port' , process . env . PORT || 3000 ) ;
4756app . set ( 'views' , path . join ( __dirname , 'views' ) ) ;
@@ -69,17 +78,26 @@ app.use(function(req, res, next) {
6978app . use ( flash ( ) ) ;
7079app . use ( less ( { src : __dirname + '/public' , compress : true } ) ) ;
7180app . use ( app . router ) ;
72- app . use ( express . static ( path . join ( __dirname , 'public' ) , { maxAge : 864000000 } ) ) ;
81+ app . use ( express . static ( path . join ( __dirname , 'public' ) , { maxAge : week } ) ) ;
7382app . use ( function ( req , res ) {
7483 res . render ( '404' , { status : 404 } ) ;
7584} ) ;
7685app . use ( express . errorHandler ( ) ) ;
7786
87+ /**
88+ * Start Server
89+ */
90+
91+ server . listen ( app . get ( 'port' ) , function ( ) {
92+ console . log ( "✔ Express server listening on port %d in %s mode" , app . get ( 'port' ) , app . settings . env ) ;
93+ } ) ;
94+
7895/**
7996 * Application routes.
8097 */
8198
8299app . get ( '/' , homeController . index ) ;
100+ app . get ( '/dashboard' , dashboardController . getDashboard ) ;
83101app . get ( '/login' , userController . getLogin ) ;
84102app . post ( '/login' , userController . postLogin ) ;
85103app . get ( '/logout' , userController . logout ) ;
@@ -118,6 +136,54 @@ app.get('/auth/foursquare/callback', passport.authorize('foursquare', { failureR
118136app . get ( '/auth/tumblr' , passport . authorize ( 'tumblr' ) ) ;
119137app . get ( '/auth/tumblr/callback' , passport . authorize ( 'tumblr' , { failureRedirect : '/api' } ) , function ( req , res ) { res . redirect ( '/api/tumblr' ) ; } ) ;
120138
121- app . listen ( app . get ( 'port' ) , function ( ) {
122- console . log ( '✔ Express server listening on port ' + app . get ( 'port' ) ) ;
139+ /**
140+ * Emit Pageviews on Socket.io
141+ */
142+
143+ io . configure ( 'production' , function ( ) {
144+ io . enable ( 'browser client minification' ) ; // send minified client
145+ io . enable ( 'browser client etag' ) ; // apply etag caching logic based on version number
146+ io . enable ( 'browser client gzip' ) ; // gzip the file
147+ io . set ( 'log level' , 1 ) ; // reduce logging
148+ io . set ( "polling duration" , 10 ) ; // increase polling frequency
149+ io . set ( 'transports' , [ // Manage transports
150+ 'websocket'
151+ , 'htmlfile'
152+ , 'xhr-polling'
153+ , 'jsonp-polling'
154+ ] ) ;
155+ io . set ( 'authorization' , function ( handshakeData , callback ) {
156+ if ( handshakeData . xdomain ) {
157+ callback ( 'Cross-domain connections are not allowed' ) ;
158+ } else {
159+ callback ( null , true ) ;
160+ }
161+ } ) ;
123162} ) ;
163+
164+ io . configure ( 'development' , function ( ) {
165+ io . set ( 'log level' , 1 ) ; // reduce logging
166+ io . set ( 'transports' , [
167+ 'websocket' // Let's just use websockets for development
168+ ] ) ;
169+ io . set ( 'authorization' , function ( handshakeData , callback ) {
170+ if ( handshakeData . xdomain ) {
171+ callback ( 'Cross-domain connections are not allowed' ) ;
172+ } else {
173+ callback ( null , true ) ;
174+ }
175+ } ) ;
176+ } ) ;
177+
178+ io . sockets . on ( 'connection' , function ( socket ) {
179+ socket . on ( 'message' , function ( message ) {
180+ console . log ( "Got message: " + message ) ;
181+ var ip = socket . handshake . address . address ;
182+ var url = message ;
183+ io . sockets . emit ( 'pageview' , { 'connections' : Object . keys ( io . connected ) . length , 'ip' : ip , 'url' : url , 'xdomain' : socket . handshake . xdomain , 'timestamp' : new Date ( ) } ) ;
184+ } ) ;
185+ socket . on ( 'disconnect' , function ( ) {
186+ console . log ( "Socket disconnected" ) ;
187+ io . sockets . emit ( 'pageview' , { 'connections' : Object . keys ( io . connected ) . length } ) ;
188+ } ) ;
189+ } ) ;
0 commit comments