@@ -10,7 +10,11 @@ var path = require('path');
10
10
var mongoose = require ( 'mongoose' ) ;
11
11
var passport = require ( 'passport' ) ;
12
12
var 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 ) ;
14
18
15
19
/**
16
20
* Load controllers.
@@ -20,6 +24,7 @@ var homeController = require('./controllers/home');
20
24
var userController = require ( './controllers/user' ) ;
21
25
var apiController = require ( './controllers/api' ) ;
22
26
var contactController = require ( './controllers/contact' ) ;
27
+ var dashboardController = require ( './controllers/dashboard' ) ;
23
28
24
29
/**
25
30
* API keys + Passport configuration.
@@ -37,11 +42,15 @@ mongoose.connection.on('error', function() {
37
42
console . log ( '✗ MongoDB Connection Error. Please make sure MongoDB is running.' . red ) ;
38
43
} ) ;
39
44
40
- var app = express ( ) ;
41
-
42
45
/**
43
46
* Express configuration.
44
47
*/
48
+
49
+ var hour = 3600000 ; //milliseconds
50
+ var day = ( hour * 24 ) ;
51
+ var week = ( day * 7 ) ;
52
+ var month = ( day * 30 ) ;
53
+
45
54
app . locals . cacheBuster = Date . now ( ) ;
46
55
app . set ( 'port' , process . env . PORT || 3000 ) ;
47
56
app . set ( 'views' , path . join ( __dirname , 'views' ) ) ;
@@ -69,17 +78,26 @@ app.use(function(req, res, next) {
69
78
app . use ( flash ( ) ) ;
70
79
app . use ( less ( { src : __dirname + '/public' , compress : true } ) ) ;
71
80
app . 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 } ) ) ;
73
82
app . use ( function ( req , res ) {
74
83
res . render ( '404' , { status : 404 } ) ;
75
84
} ) ;
76
85
app . use ( express . errorHandler ( ) ) ;
77
86
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
+
78
95
/**
79
96
* Application routes.
80
97
*/
81
98
82
99
app . get ( '/' , homeController . index ) ;
100
+ app . get ( '/dashboard' , dashboardController . getDashboard ) ;
83
101
app . get ( '/login' , userController . getLogin ) ;
84
102
app . post ( '/login' , userController . postLogin ) ;
85
103
app . get ( '/logout' , userController . logout ) ;
@@ -118,6 +136,54 @@ app.get('/auth/foursquare/callback', passport.authorize('foursquare', { failureR
118
136
app . get ( '/auth/tumblr' , passport . authorize ( 'tumblr' ) ) ;
119
137
app . get ( '/auth/tumblr/callback' , passport . authorize ( 'tumblr' , { failureRedirect : '/api' } ) , function ( req , res ) { res . redirect ( '/api/tumblr' ) ; } ) ;
120
138
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
+ } ) ;
123
162
} ) ;
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