This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
128 lines (110 loc) · 3.56 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
120
121
122
123
124
125
126
127
128
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var passport = require('passport')
var url = require('url')
var redis = require('redis')
var auth = require('./lib/authentication')
var accounts = require('./lib/collections/accounts')
var session = require("express-session")
var app = express();
var RedisStore, redisUrl, sessionClient;
var hbs = require('./lib/hbs-setup')(app)
var paypal_sdk = require('paypal-rest-sdk');
var debug = require('debug')('ozanam-gala:app');
paypal_sdk.configure({
'mode': process.env.NODE_ENV === 'production' ? 'live' : 'sandbox',
'client_id': process.env.PP_CLIENT_ID,
'client_secret': process.env.PP_CLIENT_SECRET
});
//passport serialize and setup
passport.use(auth.strategy());
passport.serializeUser(function(user, done) {
return done(null, user._id.toString());
});
passport.deserializeUser(function(id, done) {
return accounts.findById(id, function(err, doc) {
if (err) return done(err);
if (!doc) return done(err, null);
return done(err, doc);
});
});
if (/yes/i.test(process.env.ENABLESSL)) {
debug("SSL Enabled... adding middleware re-router")
app.use(function(req, res, next) {
var port, usingSSL;
usingSSL = req.secure || req.headers['x-forwarded-proto'] == 'https';
if (!usingSSL) {
return res.redirect(301, "https://" + process.env.HOSTNAMESSL + req.originalUrl);
} else {
return next();
}
});
}
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('.hbs', hbs.engine);
app.set('view engine', 'hbs');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
//session setup
//app.use(cookieParser(process.env.SESSION_SECRET || 'hello cooooookie'));
var cookieOptions = {
cookie : {httpOnly: true} //default setting for cookies.
,secret : process.env.SESSION_SECRET
,saveUninitialized : true //default
,resave : true //default
}
//use redis (if redis env setup)
if (process.env.REDIS) {
RedisStore = require('connect-redis')(session);
redisUrl = url.parse(process.env.REDIS);
sessionClient = redis.createClient(redisUrl.port, redisUrl.hostname);
if (redisUrl.auth) {
sessionClient.auth(redisUrl.auth.split(":")[1]);
}
cookieOptions.store = new RedisStore({
client: sessionClient
})
}
app.use(session(cookieOptions));
//static files /public == /
app.use(express.static(path.join(__dirname, 'public')));
//Passport init
app.use(passport.initialize())
app.use(passport.session())
//Handlebars templates to use client-side
app.use(hbs.exposeTemplates)
app.use(function(req,res,next) {
res.locals.production = process.env.NODE_ENV == 'production'
res.locals.development = process.env.NODE_ENV == 'development'
next()
})
//public routes.
app.use(require("./routes/public"))
//app rendered routes
app.use('/manage', require("./routes/manage"))
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
app.use(function(err, req, res, next) {
res.status(err.status || 500);
view = 'problem'
if (process.env.NODE_ENV === 'development' || (req.user && req.user.isAdmin))
view = 'error'
else
err = {}
res.render('error', {
message: err.message,
error: err,
layout : 'public'
});
});
module.exports = app;