Skip to content

Commit a3c4410

Browse files
committed
Added much more to the angular and rethinkdb examples
1 parent 93c7618 commit a3c4410

File tree

6 files changed

+47
-25
lines changed

6 files changed

+47
-25
lines changed

server/rethinkdb/app/app.js

+42-20
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ var GitHubStrategy = require('passport-github').Strategy;
88
var path = require('path');
99
var container = require('./container');
1010

11+
// This allows us to programmatically start the app in a test environment
1112
exports.createServer = function () {
13+
14+
// Bootstrap the application, injecting a bunch of dependencies
1215
return container.resolve(function (safeCall, posts, comments, errorHandler, queryRewrite, passport, config) {
1316
var app = express();
1417

1518
// 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.
2023
function ensureAuthenticated(req, res, next) {
2124
if (req.isAuthenticated()) {
2225
return next();
@@ -35,12 +38,12 @@ exports.createServer = function () {
3538
}
3639

3740
// 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.
4447
passport.serializeUser(function (user, done) {
4548
done(null, user);
4649
});
@@ -49,12 +52,10 @@ exports.createServer = function () {
4952
done(null, obj);
5053
});
5154

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.
5859
passport.use(new GitHubStrategy({
5960
clientID: config.GITHUB_CLIENT_ID,
6061
clientSecret: config.GITHUB_CLIENT_SECRET,
@@ -79,13 +80,18 @@ exports.createServer = function () {
7980
app.use(cookieParser());
8081
app.use(bodyParser.urlencoded({ extended: true }));
8182
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));
8386
app.set('view engine', 'ejs');
8487
app.engine('html', require('ejs').renderFile);
88+
89+
// Using Express/Passports for simplicity in the example. I would never use this in production.
8590
app.use(session({ secret: 'keyboard cat' }));
8691
app.use(passport.initialize());
8792
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)));
8995

9096
// app settings
9197
app.enable('trust proxy');
@@ -126,6 +132,12 @@ exports.createServer = function () {
126132
}
127133
});
128134

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+
/*******************************/
129141
app.get('/auth/github', passport.authenticate('github'));
130142
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), function (req, res) {
131143
res.redirect('/');
@@ -134,10 +146,18 @@ exports.createServer = function () {
134146
req.logout();
135147
res.redirect('/');
136148
});
149+
app.post('/api/socket.io/', function (req, res, next) {
150+
next();
151+
});
137152

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+
});
140159

160+
// Catch-all error handler
141161
app.use(function (err, req, res, next) {
142162
errorHandler(err, req, res, next);
143163
});
@@ -146,13 +166,15 @@ exports.createServer = function () {
146166
});
147167
};
148168

169+
// This allows us to programmatically start the app in a test environment
149170
if (module === require.main || process.env.NODE_ENV === 'prod') {
150171
var app = exports.createServer();
151172
var server = http.createServer(app);
152173
var config = container.get('config');
153174

154175
server.listen(config.PORT);
155176

177+
// Add a socket server to be used as a message bus for the clients
156178
var io = require('socket.io').listen(server);
157179

158180
container.register('io', function () {

server/rethinkdb/app/lib/messageService.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Our service for broadcasting messages to connected clients.
2+
// A way of implementing a sort of "3-way binding"
13
module.exports = function (container) {
24

35
function sendMessage(event, resource, instance) {

server/rethinkdb/app/lib/safeCall.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// This little generator simply saves us a lot of boilerplate code
12
module.exports = function (Promise) {
23
function safeCall(method) {
34
return function (req, res, next) {

server/rethinkdb/app/middleware/errorHandler.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = function () {
66
var responder;
77
if (err && req) {
88
if (res) {
9+
// Normally you would be using some logging library
910
console.log(err.message);
1011
console.log(err.stack);
1112
}

server/rethinkdb/app/middleware/queryRewrite.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// This is to fully parse the JSON in req.query
12
module.exports = function () {
23
return function (req, res, next) {
34
if (req.query.where) {

server/rethinkdb/app/models/Comment.js

-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ module.exports = function (r, container, Promise, mout, messageService, config,
88
table: 'comment',
99
relations: {
1010
belongsTo: {
11-
user: {
12-
localField: 'user',
13-
localKey: 'ownerId'
14-
},
1511
post: {
1612
localField: 'post',
1713
localKey: 'ownerId'
@@ -24,7 +20,6 @@ module.exports = function (r, container, Promise, mout, messageService, config,
2420
nullable: false
2521
},
2622
ownerId: {
27-
type: 'string',
2823
nullable: false
2924
},
3025
id: {

0 commit comments

Comments
 (0)