Skip to content

Commit

Permalink
Merge pull request #55 from rosedu/53-improve-dev
Browse files Browse the repository at this point in the history
Improve dev env
  • Loading branch information
maria committed Jan 31, 2015
2 parents 1871048 + e71beb5 commit ee47a6f
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test:

run:
@mongod &
@./node_modules/nodemon/bin/nodemon.js app.js
@./node_modules/nodemon/bin/nodemon.js app.js $(user)

production:
@export NODE_ENV=production
Expand Down
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,31 @@ Then **make run** will start the db and app at
node app.js


## Dependencies
This is a list of the modules we use (package.json):
## Development

* [express](https://www.npmjs.org/package/express) - web development framework
* [everyauth](https://www.npmjs.org/package/everyauth) - authentication solution
* [connect](https://www.npmjs.org/package/connect) - high performance middleware framework
* [jade](https://www.npmjs.org/package/jade) - Jade template engine
* [mongoose](https://www.npmjs.org/package/mongoose) - MongoDB ODM
* [markdown](https://www.npmjs.org/package/markdown) - Markdown parser for javascript
* [nodemailer](https://www.npmjs.org/package/nodemailer) - send emails
To run the app in development use the corresponding Makefile target

make run

Use package.json to install them all:

npm install package.json
This will launch the app using [nodemon](http://nodemon.io/) which automatically
restarts the app when you make changes to the code.

If you need to stay logged in, you can provide a username to the **run** command.
The app will authenticate you after the first handled request.

make run user=justin

You can also switch user once the app is running, by visiting the following URL

localhost:3000/login/justin

Superusers are defined in **model/macro.js** file. The default one is **mariuscoto**.
Use it to get access to the admin console (localhost:3000/admin).

This repo provides you with some db data so you can easily test what you implement.
If you want to restore the db, please use

make db-import


### Happy coding !
58 changes: 52 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
var express = require('express');
var app = module.exports = express();
global.config = [];
var express = require('express')
var app = module.exports = express()
global.config = []


app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
global.config.redis_secret = 'big secret'
//global.config = require('./lib/config')
global.config.status = 'dev';
global.config.status = 'dev'
global.config.login = process.argv[2]
});

app.configure('production', function(){
Expand All @@ -31,7 +32,9 @@ var MACRO = require('./model/macro.js')
, everyauth = require('everyauth')
, mongoose = require('mongoose')
, core = require('./core.js')
, cron = require('cron').CronJob;
, cron = require('cron').CronJob

var Users = mongoose.model('Users')

// Refresh challenge cron job
var job = new cron(MACRO.CRON.CHALLENGE, function(){
Expand Down Expand Up @@ -72,6 +75,49 @@ app.configure(function() {
});


// Automatically login if username argument provided, in dev env
app.all('*', function(req, res, next) {

if (global.config.status === 'dev') {
username = ''
regex = '/login/(.*)'

if (req.path.match(regex)) {
username = req.path.match(regex)[1]
} else if (global.config.login) {
username = global.config.login
global.config.login = false
} else {
return next()
}

userid = parseInt(username, 36)
core.add_user(userid, username, generate_session)

function generate_session (err) {
if (err) console.log('[ERR] Could not add user to db.')

req.session.regenerate(function (err) {
req.session.auth = {
'loggedIn': true,
'github': {
'user': {
'id': userid,
'login': username
}
}
}

return next()
})
}

} else {
return next()
}
})


// routes defined here
var other = require('./routes/other.js');
app.get('/', other.index);
Expand Down
31 changes: 31 additions & 0 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,34 @@ exports.refresh_challenges = function() {
}
}
}

/*
Adds dummy user with given id and username to db.
This is used during development when multiple users are needed for testing
purposes.
*/
exports.add_user = function(userid, username, callback) {
var u = {'id': userid, 'login': username}

// Add some content for user
var repo = {
'name': username + '\'s cool repo',
'description': 'A very nice description should be added here.',
'html_url': 'http://www.github.com',
'fork': true,
'forks_count': 3,
'watchers_count': 5,
'closed_pulls': 3,
}
var update = {
'user_id': u.id,
'user_name': u.login,
'user_fullname': 'Development user',
'user_email': '[email protected]',
'avatar_url': 'https://avatars.githubusercontent.com/u/0',
'location': 'Somewhere',
'repos': [repo]
}

Users.update({'user_id': u.id}, update, {'upsert': true}).exec(callback)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"mocha": "1.20.1",
"mongoose": "3.2.0",
"nodemailer": "~0.6.3",
"nodemon": "^1.2.1"
"nodemon": "^1.3.2"
},
"engines": {
"node": "0.8.x",
Expand Down
Binary file modified rchallenge_db/rosedu-challenge/challenges.bson
Binary file not shown.
Binary file added rchallenge_db/rosedu-challenge/notifications.bson
Binary file not shown.
1 change: 1 addition & 0 deletions rchallenge_db/rosedu-challenge/notifications.metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "rosedu-challenge.notifications" } ] }
Binary file modified rchallenge_db/rosedu-challenge/system.indexes.bson
Binary file not shown.
Binary file modified rchallenge_db/rosedu-challenge/users.bson
Binary file not shown.
63 changes: 8 additions & 55 deletions routes/other.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,64 +58,17 @@ exports.index = function(req, res) {


/*
Login with or without GitHub auth.
This will provide a session and create a new user if necessary.
Visit /login/$USER to login as $USER.
Show login button or redirect to page if user already logged in.
*/
exports.login = function(req, res) {
// Use an offline account. Add user if not existent.
if (global.config.status == 'dev') {
if (!req.params.user) {
// If no username provided, redirect to default.
return res.redirect('/login/dev_user');

} else {
// Create default user with given name and autogenerated id.
var u = {id: parseInt(req.params.user, 36), login: req.params.user};

// Add some content for user
var repo = {
name: req.params.user + '\'s cool repo',
description: 'A very nice description should be added here.',
html_url: 'http://www.github.com',
fork: true,
forks_count: 3,
watchers_count: 5,
closed_pulls: 3,
};
var update = {
user_id: u.id,
user_name: u.login,
user_fullname: 'Development user',
user_email: '[email protected]',
avatar_url: 'https://avatars.githubusercontent.com/u/0',
location: 'Somewhere',
repos: [repo]
};

// Make sure user exists and build session for him.
Users.update({user_id: u.id}, update, {upsert: true}, function(err, num) {
req.session.regenerate(function (err) {
req.session.auth = {};
req.session.auth.loggedIn = true;
req.session.auth.github = {};
req.session.auth.github.user = u;
res.redirect('/' + u.login);
});
});
}

// Load login or redirect user to profile page if already logged in.
} else {
if (req.session.auth)
return res.redirect('/' + req.session.auth.github.user.login);
if (req.session.auth)
return res.redirect('/' + req.session.auth.github.user.login);

res.render('login', {
'title': "Log in",
'status': global.config.status,
'tab': req.query.rf
});
}
res.render('login', {
'title': "Log in",
'status': global.config.status,
'tab': req.query.rf
});
};


Expand Down

0 comments on commit ee47a6f

Please sign in to comment.