-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
119 lines (104 loc) · 3.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// **My2Cents** is a campaign widget for calling elected
// representatives. Source is available on
// [GitHub](https://github.com/affinitybridge/my2cents).
// My2Cents is built on [Express](http://expressjs.com/). It uses the
// [Twilio API](http://twilio.com) to make phone calls and the
// [Open North Represent API](http://represent.opennorth.ca/) to receive
// representative information. [Jade](http://jade-lang.com/) is used for templating and
// [Stylus](http://learnboost.github.com/stylus/) helps with CSS.
var express = require('express'),
passport = require('passport'),
stylus = require('stylus'),
config = require('./config'),
MongoStore = require('connect-mongo')(express);
// Create the Express server.
var app = module.exports = express.createServer();
// Stylus compile function. [[docs](http://learnboost.github.com/stylus/docs/middleware.html)]
function compile(str, path) {
return stylus(str)
.set('filename', path)
// Comment the processed CSS when not on production
.set('linenos', process.env.NODE_ENV !== 'production')
// Compress the processed CSS
.set('compress', true);
}
// Configure Express application.
// Express uses [Connect](http://www.senchalabs.org/connect/) middleware.
var oneMonth = 1000 * 60 * 60 * 24 * 30;
app.configure(function() {
app.use(stylus.middleware({
src: __dirname + '/styles',
dest: __dirname + '/public',
compile: compile
}));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {
layout: false,
pretty: process.env.NODE_ENV !== 'production'
});
app.set('port', process.env.PORT || 5000);
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.compress());
app.use(express.static(__dirname + '/public', { maxAge: oneMonth }));
app.use(express.cookieParser());
app.use(express.session({
secret: process.env.SESSION_SECRET || config.session_secret,
store: new MongoStore({
url: process.env.MONGOLAB_URI || config.mongodb_url
})
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.favicon('public/favicon.ico', { maxAge: oneMonth }));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('test', function(){
app.set('port', 5001);
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Dynamic view helpers
app.dynamicHelpers({
flash: function (req, res) {
return req.flash();
},
session: function (req, res) {
return req.session;
}
});
// User authentication middleware
function requireAuth(req, res, next) {
if (req.session.user) {
next();
}
else {
req.flash('error', 'Please login to access this page!');
res.redirect('/login');
}
}
// ### Routes
// Homepage
require('./routes/index')(app);
// User authentication
require('./routes/login')(app);
// User creation
require('./routes/user')(app);
// Display and create campaigns
require('./routes/campaign')(app, requireAuth);
// Get OpenNorth Represent data
require('./routes/representatives')(app);
// Twilio app request URL
require('./routes/twiml')(app);
// Error Pages
require('./routes/error')(app);
// Bind the Express server to a port.
var port = app.settings.port;
app.listen(port, function() {
console.log("Listening on " + port);
});