|
| 1 | +var express = require('express'); |
| 2 | +var http = require('http'); |
| 3 | +var bodyParser = require('body-parser'); |
| 4 | +var cookieParser = require('cookie-parser'); |
| 5 | +var session = require('express-session'); |
| 6 | +var methodOverride = require('method-override'); |
| 7 | +var GitHubStrategy = require('passport-github').Strategy; |
| 8 | +var path = require('path'); |
| 9 | +var container = require('./container'); |
| 10 | + |
| 11 | +exports.createServer = function () { |
| 12 | + return container.resolve(function (safeCall, posts, comments, errorHandler, queryRewrite, passport, config) { |
| 13 | + var app = express(); |
| 14 | + |
| 15 | + // 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. |
| 20 | + function ensureAuthenticated(req, res, next) { |
| 21 | + if (req.isAuthenticated()) { |
| 22 | + return next(); |
| 23 | + } |
| 24 | + res.redirect('/login'); |
| 25 | + } |
| 26 | + |
| 27 | + function renderIndex(req, res, next) { |
| 28 | + res.render('index.html', {}, function (err, html) { |
| 29 | + if (err) { |
| 30 | + next(err); |
| 31 | + } else { |
| 32 | + res.send(html); |
| 33 | + } |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + // 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. |
| 44 | + passport.serializeUser(function (user, done) { |
| 45 | + done(null, user); |
| 46 | + }); |
| 47 | + |
| 48 | + passport.deserializeUser(function (obj, done) { |
| 49 | + done(null, obj); |
| 50 | + }); |
| 51 | + |
| 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 | + */ |
| 58 | + passport.use(new GitHubStrategy({ |
| 59 | + clientID: config.GITHUB_CLIENT_ID, |
| 60 | + clientSecret: config.GITHUB_CLIENT_SECRET, |
| 61 | + callbackURL: config.GITHUB_CALLBACK_URL |
| 62 | + }, |
| 63 | + function (accessToken, refreshToken, profile, done) { |
| 64 | + // asynchronous verification, for effect... |
| 65 | + process.nextTick(function () { |
| 66 | + |
| 67 | + // To keep the example simple, the user's GitHub profile is returned to |
| 68 | + // represent the logged-in user. In a typical application, you would want |
| 69 | + // to associate the GitHub account with a user record in your database, |
| 70 | + // and return that user instead. |
| 71 | + return done(null, profile); |
| 72 | + }); |
| 73 | + } |
| 74 | + )); |
| 75 | + |
| 76 | + // middleware |
| 77 | + app.use(queryRewrite); |
| 78 | + app.use(bodyParser.json()); |
| 79 | + app.use(cookieParser()); |
| 80 | + app.use(bodyParser.urlencoded({ extended: true })); |
| 81 | + app.use(methodOverride()); |
| 82 | + console.log(path.join(__dirname, config.PUBLIC)); |
| 83 | + app.set('views', path.join(__dirname, config.PUBLIC)); |
| 84 | + app.set('view engine', 'ejs'); |
| 85 | + app.engine('html', require('ejs').renderFile); |
| 86 | + app.use(session({ secret: 'keyboard cat' })); |
| 87 | + app.use(passport.initialize()); |
| 88 | + app.use(passport.session()); |
| 89 | + app.use(express.static(path.join(__dirname, config.PUBLIC))); |
| 90 | + |
| 91 | + // app settings |
| 92 | + app.enable('trust proxy'); |
| 93 | + |
| 94 | + app.get('/', renderIndex); |
| 95 | + |
| 96 | + /******************************/ |
| 97 | + /********** comments **********/ |
| 98 | + /******************************/ |
| 99 | + app.route('/api/comments') |
| 100 | + // GET /comments |
| 101 | + .get(safeCall(comments.findAll)) |
| 102 | + // POST /comments |
| 103 | + .post(ensureAuthenticated, safeCall(comments.createOne)); |
| 104 | + |
| 105 | + app.route('/api/comments/:id') |
| 106 | + .get(safeCall(comments.findOneById)) |
| 107 | + .put(ensureAuthenticated, safeCall(comments.updateOneById)) |
| 108 | + .delete(ensureAuthenticated, safeCall(comments.deleteOneById)); |
| 109 | + |
| 110 | + /*******************************/ |
| 111 | + /********** posts **********/ |
| 112 | + /*******************************/ |
| 113 | + app.route('/api/posts') |
| 114 | + .get(safeCall(posts.findAll)) |
| 115 | + .post(ensureAuthenticated, safeCall(posts.createOne)); |
| 116 | + |
| 117 | + app.route('/api/posts/:id') |
| 118 | + .get(safeCall(posts.findOneById)) |
| 119 | + .put(ensureAuthenticated, safeCall(posts.updateOneById)) |
| 120 | + .delete(ensureAuthenticated, safeCall(posts.deleteOneById)); |
| 121 | + |
| 122 | + app.get('/auth/github', passport.authenticate('github')); |
| 123 | + app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), function (req, res) { |
| 124 | + res.redirect('/'); |
| 125 | + }); |
| 126 | + app.post('/api/logout', function (req, res) { |
| 127 | + req.logout(); |
| 128 | + res.redirect('/'); |
| 129 | + }); |
| 130 | + |
| 131 | + // redirect all others to the index (HTML5 history) |
| 132 | + app.get('*', renderIndex); |
| 133 | + |
| 134 | + app.use(function (err, req, res, next) { |
| 135 | + errorHandler(err, req, res, next); |
| 136 | + }); |
| 137 | + |
| 138 | + return app; |
| 139 | + }); |
| 140 | +}; |
| 141 | + |
| 142 | +if (module === require.main || process.NODE_ENV === 'prod') { |
| 143 | + var app = exports.createServer(); |
| 144 | + var server = http.createServer(app); |
| 145 | + var config = container.get('config'); |
| 146 | + |
| 147 | + server.listen(config.PORT); |
| 148 | + |
| 149 | + var io = require('socket.io').listen(server); |
| 150 | + |
| 151 | + container.register('io', function () { |
| 152 | + return io; |
| 153 | + }); |
| 154 | +} |
0 commit comments