@@ -8,15 +8,18 @@ var GitHubStrategy = require('passport-github').Strategy;
8
8
var path = require ( 'path' ) ;
9
9
var container = require ( './container' ) ;
10
10
11
+ // This allows us to programmatically start the app in a test environment
11
12
exports . createServer = function ( ) {
13
+
14
+ // Bootstrap the application, injecting a bunch of dependencies
12
15
return container . resolve ( function ( safeCall , posts , comments , errorHandler , queryRewrite , passport , config ) {
13
16
var app = express ( ) ;
14
17
15
18
// Simple route middleware to ensure user is authenticated.
16
- // Use this route middleware on any resource that needs to be protected. If
17
- // the request is authenticated (typically via a persistent login session),
18
- // the request will proceed. Otherwise, the user will be redirected to the
19
- // login page.
19
+ // Use this route middleware on any resource that needs to be protected. If
20
+ // the request is authenticated (typically via a persistent login session),
21
+ // the request will proceed. Otherwise, the user will be redirected to the
22
+ // login page.
20
23
function ensureAuthenticated ( req , res , next ) {
21
24
if ( req . isAuthenticated ( ) ) {
22
25
return next ( ) ;
@@ -35,12 +38,12 @@ exports.createServer = function () {
35
38
}
36
39
37
40
// Passport session setup.
38
- // To support persistent login sessions, Passport needs to be able to
39
- // serialize users into and deserialize users out of the session. Typically,
40
- // this will be as simple as storing the user ID when serializing, and finding
41
- // the user by ID when deserializing. However, since this example does not
42
- // have a database of user records, the complete GitHub profile is serialized
43
- // and deserialized.
41
+ // To support persistent login sessions, Passport needs to be able to
42
+ // serialize users into and deserialize users out of the session. Typically,
43
+ // this will be as simple as storing the user ID when serializing, and finding
44
+ // the user by ID when deserializing. However, since this example does not
45
+ // have a database of user records, the complete GitHub profile is serialized
46
+ // and deserialized.
44
47
passport . serializeUser ( function ( user , done ) {
45
48
done ( null , user ) ;
46
49
} ) ;
@@ -49,12 +52,10 @@ exports.createServer = function () {
49
52
done ( null , obj ) ;
50
53
} ) ;
51
54
52
- /**
53
- * Use the GitHubStrategy within Passport.
54
- * Strategies in Passport require a `verify` function, which accept
55
- * credentials (in this case, an accessToken, refreshToken, and GitHub
56
- * profile), and invoke a callback with a user object.
57
- */
55
+ // Use the GitHubStrategy within Passport.
56
+ // Strategies in Passport require a `verify` function, which accept
57
+ // credentials (in this case, an accessToken, refreshToken, and GitHub
58
+ // profile), and invoke a callback with a user object.
58
59
passport . use ( new GitHubStrategy ( {
59
60
clientID : config . GITHUB_CLIENT_ID ,
60
61
clientSecret : config . GITHUB_CLIENT_SECRET ,
@@ -79,13 +80,18 @@ exports.createServer = function () {
79
80
app . use ( cookieParser ( ) ) ;
80
81
app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
81
82
app . use ( methodOverride ( ) ) ;
82
- app . set ( 'views' , path . join ( __dirname , config . PUBLIC ) ) ;
83
+
84
+ // I'm only using Express to render/serve the index.html file and other static assets for simplicity with the example apps
85
+ app . set ( 'views' , path . join ( __dirname , process . env . PUBLIC_PATH || config . PUBLIC_PATH ) ) ;
83
86
app . set ( 'view engine' , 'ejs' ) ;
84
87
app . engine ( 'html' , require ( 'ejs' ) . renderFile ) ;
88
+
89
+ // Using Express/Passports for simplicity in the example. I would never use this in production.
85
90
app . use ( session ( { secret : 'keyboard cat' } ) ) ;
86
91
app . use ( passport . initialize ( ) ) ;
87
92
app . use ( passport . session ( ) ) ;
88
- app . use ( express . static ( path . join ( __dirname , process . env . PUBLIC || config . PUBLIC ) ) ) ;
93
+ // PUBLIC_PATH is used to choose with frontend client to use. Default is the js-data + Angular client.
94
+ app . use ( express . static ( path . join ( __dirname , process . env . PUBLIC_PATH || config . PUBLIC_PATH ) ) ) ;
89
95
90
96
// app settings
91
97
app . enable ( 'trust proxy' ) ;
@@ -126,6 +132,12 @@ exports.createServer = function () {
126
132
}
127
133
} ) ;
128
134
135
+ // Normally I would have a bunch of user-related routes, but I'm
136
+ // just using Passport.js + Github for simplicity in the example
137
+
138
+ /*******************************/
139
+ /*********** auth **************/
140
+ /*******************************/
129
141
app . get ( '/auth/github' , passport . authenticate ( 'github' ) ) ;
130
142
app . get ( '/auth/github/callback' , passport . authenticate ( 'github' , { failureRedirect : '/login' } ) , function ( req , res ) {
131
143
res . redirect ( '/' ) ;
@@ -134,10 +146,18 @@ exports.createServer = function () {
134
146
req . logout ( ) ;
135
147
res . redirect ( '/' ) ;
136
148
} ) ;
149
+ app . post ( '/api/socket.io/' , function ( req , res , next ) {
150
+ next ( ) ;
151
+ } ) ;
137
152
138
- // redirect all others to the index (HTML5 history)
139
- app . get ( '*' , renderIndex ) ;
153
+ // Redirect all others to the index (HTML5 history)
154
+ app . get ( '*' , function ( req , res , next ) {
155
+ if ( req . originalUrl . indexOf ( 'socket.io' ) === - 1 ) {
156
+ renderIndex ( req , res , next ) ;
157
+ }
158
+ } ) ;
140
159
160
+ // Catch-all error handler
141
161
app . use ( function ( err , req , res , next ) {
142
162
errorHandler ( err , req , res , next ) ;
143
163
} ) ;
@@ -146,13 +166,15 @@ exports.createServer = function () {
146
166
} ) ;
147
167
} ;
148
168
169
+ // This allows us to programmatically start the app in a test environment
149
170
if ( module === require . main || process . env . NODE_ENV === 'prod' ) {
150
171
var app = exports . createServer ( ) ;
151
172
var server = http . createServer ( app ) ;
152
173
var config = container . get ( 'config' ) ;
153
174
154
175
server . listen ( config . PORT ) ;
155
176
177
+ // Add a socket server to be used as a message bus for the clients
156
178
var io = require ( 'socket.io' ) . listen ( server ) ;
157
179
158
180
container . register ( 'io' , function ( ) {
0 commit comments