提交 eb11a8b14861112a5d63fae75b155940f3954a4c

作者 Nick Baugh
2 个父辈 6cb9a217 a7b22049

Merge pull request #6 from s-silva/development

Integrated Electrolyte and updated Express to version 4.2.x
1 1
2 2 // # home
3 3
4   -module.exports = home
  4 +var mongoose = require('mongoose')
  5 +exports = module.exports = home
5 6
6   -function home(req, res, next) {
7   - res.render('home')
  7 +function home(lib, settings) {
  8 + return {
  9 +
  10 + render: function(req, res, next) {
  11 + res.render('home')
  12 + },
  13 +
  14 + about: function(req, res, next) {
  15 + res.send(200, 'Hello, this is Igloo.')
  16 + }
  17 +
  18 + }
8 19 }
  20 +
  21 +exports['@require'] = [ 'lib', 'settings' ];
\ No newline at end of file
... ...
1 1
2 2 // # user
3 3
4   -var mongoose = require('mongoose')
5   -var common = require('./common')
  4 +var mongoose = require('mongoose'),
  5 + common = require('./common'),
  6 + Schema = mongoose.Schema,
  7 + mongooseTypes = require('mongoose-types')
6 8
7   -var Schema = mongoose.Schema
8   -var mongooseTypes = require('mongoose-types')
9 9 mongooseTypes.loadTypes(mongoose)
10 10
11 11 var Email = mongoose.SchemaTypes.Email
12 12
13   -var User = module.exports = new Schema({
  13 +var User = new Schema({
14 14 email: {
15 15 type: Email,
16 16 required: true,
... ... @@ -19,3 +19,5 @@ var User = module.exports = new Schema({
19 19 })
20 20
21 21 User.plugin(common)
  22 +
  23 +module.exports = User;
\ No newline at end of file
... ...
... ... @@ -7,7 +7,17 @@ var dynamicHelpers = require('./dynamic-helpers')
7 7 var development = require('./development')
8 8 var production = require('./production')
9 9 var auth = require('./auth')
  10 +var settings = require('./settings')
10 11 var routes = require('./routes')
  12 +var ioc = require('electrolyte')
  13 +var session = require('express-session')
  14 +var methodOverride = require('method-override')
  15 +var errorHandler = require('errorhandler')
  16 +var serveFavicon = require('serve-favicon')
  17 +var cookieParser = require('cookie-parser')
  18 +var bodyParser = require('body-parser')
  19 +var dataNode = require('../lib/dataNode')
  20 +
11 21
12 22 module.exports = function(lib, callback) {
13 23
... ... @@ -22,6 +32,15 @@ module.exports = function(lib, callback) {
22 32 // trust proxy
23 33 app.enable('trust proxy')
24 34
  35 + // set-up electrolyte
  36 + ioc.loader('controllers', ioc.node('app/controllers'));
  37 + ioc.loader('models', ioc.node('app/models'));
  38 + ioc.loader(ioc.node('config'));
  39 +
  40 + // set-up data nodes (inject data)
  41 + ioc.loader(dataNode(lib, 'lib'));
  42 + ioc.loader(dataNode(app, 'app'));
  43 +
25 44 // set the default views directory
26 45 app.set('views', lib.config.viewsDir)
27 46
... ... @@ -32,27 +51,31 @@ module.exports = function(lib, callback) {
32 51 app.locals.pretty = true
33 52
34 53 // ignore GET /favicon.ico
35   - app.use(express.favicon(lib.config.favicon))
  54 + app.use(serveFavicon(lib.config.favicon))
36 55
37 56 // development only config
38   - app.configure('development', development(lib, app))
  57 + switch(lib.config.env){
  58 + case 'development':
  59 + development(lib, app);
  60 + break;
39 61
40   - // production only config
41   - app.configure('production', production(lib, app))
  62 + case 'production':
  63 + production(lib, app)
  64 + break;
  65 + }
42 66
43 67 // pass a secret to cookieParser() for signed cookies
44   - app.use(express.cookieParser(lib.config.cookieParser))
  68 + app.use(cookieParser(lib.config.cookieParser))
45 69
46 70 // parse request bodies
47   - app.use(express.json())
48   - app.use(express.urlencoded())
  71 + app.use(bodyParser())
49 72
50 73 // support _method (PUT in forms etc)
51   - app.use(express.methodOverride())
  74 + app.use(methodOverride())
52 75
53 76 // add req.session cookie support
54   - app.use(express.session(lib.config.session))
55   -
  77 + app.use(session(lib.config.session))
  78 +
56 79 // add flash message support
57 80 app.use(flash())
58 81
... ... @@ -60,20 +83,20 @@ module.exports = function(lib, callback) {
60 83 app.use(dynamicHelpers)
61 84
62 85 // add support for authentication
63   - app.configure(auth(lib, app))
  86 + auth(lib, app)()
64 87
65 88 // load the routes
66   - app.configure(routes(lib, app))
67   -
68   - // make the middleware order matter
69   - app.use(app.router)
  89 + routes(lib, app)()
70 90
71 91 // static server
72 92 app.use(express.static(lib.config.publicDir, lib.config.staticServer))
73 93
74 94 // error handling
75   - app.use(express.errorHandler())
  95 + app.use(errorHandler())
  96 +
76 97
77 98 callback(null, app)
78 99
  100 + return {app: app, lib: lib}
79 101 }
  102 +
... ...
... ... @@ -2,7 +2,6 @@
2 2 // # auth
3 3
4 4 var passport = require('passport')
5   -
6 5 var lib, app
7 6
8 7 module.exports = function(_lib, _app) {
... ... @@ -32,3 +31,7 @@ function deserializeUser(id, done) {
32 31 done(err, user)
33 32 })
34 33 }
  34 +
  35 +
  36 +
  37 +
... ...
... ... @@ -18,7 +18,7 @@ var environments = {
18 18 host: 'localhost',
19 19 db: {
20 20 host: 'localhost',
21   - dbname: 'TODO',
  21 + dbname: 'mydb',
22 22 port: '27017',
23 23 opts: {}
24 24 },
... ...
1 1
2 2 // # routes
3 3
  4 +var IoC = require('electrolyte')
4 5 var lib, app
5 6
  7 +
6 8 module.exports = function(_lib, _app) {
7 9 lib = _lib
8 10 app = _app
... ... @@ -11,8 +13,10 @@ module.exports = function(_lib, _app) {
11 13
12 14 function routes() {
13 15
14   - var controllers = require('../app/controllers')(lib)
  16 + var home = IoC.create('controllers/home')
15 17
16   - app.get('/', controllers.home)
  18 + app.get('/', home.render)
  19 + app.get('/about', home.about)
17 20
18 21 }
  22 +
... ...
  1 +/**
  2 + * Module dependencies.
  3 + */
  4 +var config = require('./index');
  5 +
  6 +
  7 +/**
  8 + * Initialize settings.
  9 + */
  10 +exports = module.exports = function(lib, app) {
  11 + var settings = new Settings(config);
  12 + return settings;
  13 +}
  14 +
  15 +
  16 +/**
  17 + * Component annotations.
  18 + */
  19 +exports['@singleton'] = true;
  20 +
  21 +
  22 +/**
  23 + * Settings component
  24 + */
  25 +
  26 +function Settings(initial) {
  27 + if(initial)
  28 + this._hash = initial;
  29 + else
  30 + this._hash = {};
  31 +}
  32 +
  33 +Settings.prototype.get = function(key) {
  34 + return this._hash[key];
  35 +}
  36 +
  37 +Settings.prototype.set = function(key, val) {
  38 + this._hash[key] = val;
  39 +}
\ No newline at end of file
... ...
  1 +/**
  2 + * Add following code to auth() function in
  3 + * ./config/auth.js
  4 + *
  5 + * It's better to keep 'returnURL' and 'realm'
  6 + * in settings.
  7 + */
  8 +
  9 +
  10 +var GoogleStrategy = require('passport-google').Strategy
  11 +
  12 +passport.use( new GoogleStrategy({
  13 + returnURL: 'http://localhost:3000/auth/google/callback',
  14 + realm: 'http://localhost:3000',
  15 + passReqToCallback: true
  16 + },
  17 + function(req, identifier, profile, done) {
  18 +
  19 + var email = profile.emails[ 0 ].value
  20 + var User = lib.db.model('User')
  21 +
  22 + // save user
  23 + User.findOne({
  24 + email: email
  25 + }, function( err, user ) {
  26 +
  27 + if (!user) {
  28 + User.create({
  29 + email: email,
  30 + name: profile.displayName,
  31 + }, function(err, user) {
  32 +
  33 + // Update lastToken
  34 + user.lastToken = identifier
  35 + user.save()
  36 + return done( err, user )
  37 + });
  38 +
  39 + } else {
  40 + // Update lastToken
  41 + user.lastToken = identifier
  42 + user.save()
  43 + return done( err, user )
  44 + }
  45 +
  46 + });
  47 + }
  48 +));
\ No newline at end of file
... ...
  1 +/**
  2 + * Add following code to routes() function in
  3 + * your ./config/routes.js file.
  4 + */
  5 +
  6 +var passport = require('passport')
  7 +
  8 +// auth route
  9 +
  10 +app.get('/auth/google', function(req,res,next) {
  11 + // add remember me details etc. here
  12 + next()
  13 + },
  14 + passport.authenticate('google', { failureRedirect: '/', failureFlash: true }),
  15 + function(req, res) {
  16 + res.redirect('/')
  17 + });
  18 +
  19 +// auth callback
  20 +
  21 +app.get('/auth/google/callback',
  22 + passport.authenticate( 'google', {
  23 + failureRedirect: '/'
  24 + }),
  25 + function(req, res, next) {
  26 + var redirectTo = req.session.redirectTo;
  27 +
  28 + // Successful authentication, redirect to dashboard page.
  29 + if ( redirectTo ) {
  30 + req.session.redirectTo = null
  31 + res.redirect(redirectTo)
  32 + } else {
  33 + res.redirect('/logged-in')
  34 + }
  35 + }
  36 +);
  37 +
  38 +// log out
  39 +
  40 +app.get('/logout', function(req, res){
  41 + req.logout()
  42 + res.redirect('/')
  43 +});
  44 +
  45 +// sample log-in message
  46 +
  47 +app.get('/logged-in', function(req, res){
  48 + req.send(200, 'Hey!')
  49 +});
\ No newline at end of file
... ...
  1 +/**
  2 + * Mongoose create route.
  3 + * Use this in a controller and setup a route
  4 + * to create function.
  5 + */
  6 +
  7 +exports = module.exports = home
  8 +
  9 +function home(db, _User) {
  10 + return {
  11 +
  12 + create: function(req, res, next) {
  13 +
  14 + // create user object using db ('lib/db') connection
  15 + // and models/user
  16 +
  17 + var User = db.model('User', _User);
  18 +
  19 + User.create({email: 'somebody@example.com'}, function( err, user ) {
  20 + if(err)
  21 + res.send(500, 'Igloo: Something went wrong.')
  22 + else
  23 + res.send(200, 'Igloo: Created user.')
  24 + });
  25 +
  26 + }
  27 +
  28 + }
  29 +}
  30 +
  31 +exports['@require'] = [ 'lib/db', 'models/user' ];
\ No newline at end of file
... ...
  1 +
  2 +/**
  3 + * This is the missing data injection functionality in
  4 + * electrolyte. Based on electrolyte/loaders/node.js, the
  5 + * default script loader.
  6 + *
  7 + */
  8 +
  9 +module.exports = function(data, options) {
  10 + if ('string' == typeof options) {
  11 + options = { dirname: options }
  12 + }
  13 +
  14 + return function(id) {
  15 +
  16 + var newId = null;
  17 +
  18 + if(id){
  19 + var dir = options.dirname + '/'
  20 +
  21 + if(id.indexOf(dir) === 0)
  22 + newId = id.substr(dir.length)
  23 + }
  24 +
  25 + if(newId)
  26 + return data[newId]
  27 + else
  28 + return data
  29 + }
  30 +}
... ...
... ... @@ -10,4 +10,3 @@ module.exports = function(config) {
10 10 lib.config = config
11 11 return lib
12 12 }
13   -
... ...
... ... @@ -10,3 +10,4 @@ var logger = module.exports = new (winston.Logger)({
10 10 })
11 11 ]
12 12 })
  13 +
... ...
... ... @@ -2,7 +2,8 @@
2 2 // # redis
3 3
4 4 var express = require('express')
5   -var RedisStore = require('connect-redis')(express)
  5 +var session = require('express-session')
  6 +var RedisStore = require('connect-redis')(session)
6 7
7 8 var config
8 9
... ...
... ... @@ -20,9 +20,9 @@
20 20 "passport": "~0.2.0",
21 21 "express-rate": "0.0.1",
22 22 "jade": "~1.1.5",
23   - "express": "~3.4.8",
  23 + "express": "~4.2.0",
24 24 "mongoose": "~3.8.7",
25   - "connect-redis": "~1.4.6",
  25 + "connect-redis": "~2.0.0",
26 26 "underscore": "~1.6.0",
27 27 "async": "~0.2.10",
28 28 "winston": "~0.7.2",
... ... @@ -30,6 +30,13 @@
30 30 "less-middleware": "~0.1.15",
31 31 "connect-flash": "~0.1.1",
32 32 "winston-mongodb": "~0.4.3",
33   - "winston-request-logger": "~1.0.4"
  33 + "winston-request-logger": "~1.0.4",
  34 + "electrolyte": "*",
  35 + "express-session": "~1.1.0",
  36 + "method-override": "~1.0.1",
  37 + "cookie-parser": "~1.1.0",
  38 + "errorhandler": "~1.0.1",
  39 + "body-parser": "~1.2.0",
  40 + "serve-favicon": "~2.0.0"
34 41 }
35 42 }
... ...
注册登录 后发表评论