Skip to content

Commit ee47a6f

Browse files
committed
Merge pull request #55 from rosedu/53-improve-dev
Improve dev env
2 parents 1871048 + e71beb5 commit ee47a6f

File tree

11 files changed

+116
-75
lines changed

11 files changed

+116
-75
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test:
3131

3232
run:
3333
@mongod &
34-
@./node_modules/nodemon/bin/nodemon.js app.js
34+
@./node_modules/nodemon/bin/nodemon.js app.js $(user)
3535

3636
production:
3737
@export NODE_ENV=production

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,31 @@ Then **make run** will start the db and app at
3636
node app.js
3737

3838

39-
## Dependencies
40-
This is a list of the modules we use (package.json):
39+
## Development
4140

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

43+
make run
5044

51-
Use package.json to install them all:
52-
53-
npm install package.json
45+
This will launch the app using [nodemon](http://nodemon.io/) which automatically
46+
restarts the app when you make changes to the code.
47+
48+
If you need to stay logged in, you can provide a username to the **run** command.
49+
The app will authenticate you after the first handled request.
50+
51+
make run user=justin
52+
53+
You can also switch user once the app is running, by visiting the following URL
54+
55+
localhost:3000/login/justin
56+
57+
Superusers are defined in **model/macro.js** file. The default one is **mariuscoto**.
58+
Use it to get access to the admin console (localhost:3000/admin).
59+
60+
This repo provides you with some db data so you can easily test what you implement.
61+
If you want to restore the db, please use
62+
63+
make db-import
5464

5565

5666
### Happy coding !

app.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
var express = require('express');
2-
var app = module.exports = express();
3-
global.config = [];
1+
var express = require('express')
2+
var app = module.exports = express()
3+
global.config = []
44

55

66
app.configure('development', function(){
7-
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
7+
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
88
global.config.redis_secret = 'big secret'
99
//global.config = require('./lib/config')
10-
global.config.status = 'dev';
10+
global.config.status = 'dev'
11+
global.config.login = process.argv[2]
1112
});
1213

1314
app.configure('production', function(){
@@ -31,7 +32,9 @@ var MACRO = require('./model/macro.js')
3132
, everyauth = require('everyauth')
3233
, mongoose = require('mongoose')
3334
, core = require('./core.js')
34-
, cron = require('cron').CronJob;
35+
, cron = require('cron').CronJob
36+
37+
var Users = mongoose.model('Users')
3538

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

7477

78+
// Automatically login if username argument provided, in dev env
79+
app.all('*', function(req, res, next) {
80+
81+
if (global.config.status === 'dev') {
82+
username = ''
83+
regex = '/login/(.*)'
84+
85+
if (req.path.match(regex)) {
86+
username = req.path.match(regex)[1]
87+
} else if (global.config.login) {
88+
username = global.config.login
89+
global.config.login = false
90+
} else {
91+
return next()
92+
}
93+
94+
userid = parseInt(username, 36)
95+
core.add_user(userid, username, generate_session)
96+
97+
function generate_session (err) {
98+
if (err) console.log('[ERR] Could not add user to db.')
99+
100+
req.session.regenerate(function (err) {
101+
req.session.auth = {
102+
'loggedIn': true,
103+
'github': {
104+
'user': {
105+
'id': userid,
106+
'login': username
107+
}
108+
}
109+
}
110+
111+
return next()
112+
})
113+
}
114+
115+
} else {
116+
return next()
117+
}
118+
})
119+
120+
75121
// routes defined here
76122
var other = require('./routes/other.js');
77123
app.get('/', other.index);

core.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,34 @@ exports.refresh_challenges = function() {
333333
}
334334
}
335335
}
336+
337+
/*
338+
Adds dummy user with given id and username to db.
339+
This is used during development when multiple users are needed for testing
340+
purposes.
341+
*/
342+
exports.add_user = function(userid, username, callback) {
343+
var u = {'id': userid, 'login': username}
344+
345+
// Add some content for user
346+
var repo = {
347+
'name': username + '\'s cool repo',
348+
'description': 'A very nice description should be added here.',
349+
'html_url': 'http://www.github.com',
350+
'fork': true,
351+
'forks_count': 3,
352+
'watchers_count': 5,
353+
'closed_pulls': 3,
354+
}
355+
var update = {
356+
'user_id': u.id,
357+
'user_name': u.login,
358+
'user_fullname': 'Development user',
359+
'user_email': '[email protected]',
360+
'avatar_url': 'https://avatars.githubusercontent.com/u/0',
361+
'location': 'Somewhere',
362+
'repos': [repo]
363+
}
364+
365+
Users.update({'user_id': u.id}, update, {'upsert': true}).exec(callback)
366+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"mocha": "1.20.1",
1313
"mongoose": "3.2.0",
1414
"nodemailer": "~0.6.3",
15-
"nodemon": "^1.2.1"
15+
"nodemon": "^1.3.2"
1616
},
1717
"engines": {
1818
"node": "0.8.x",
7.68 KB
Binary file not shown.
116 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "rosedu-challenge.notifications" } ] }
Binary file not shown.
2.62 KB
Binary file not shown.

0 commit comments

Comments
 (0)