forked from writerstrail/writerstrail-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
172 lines (149 loc) · 4.4 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
session = require('express-session'),
MysqlStore = require('express-mysql-session'),
passport = require('passport'),
flash = require('connect-flash'),
i18n = require('i18n'),
csrf = require('csurf'),
uglify = require('./utils/middlewares/uglify'),
paginate = require('express-paginate'),
moment = require('moment'),
_ = require('lodash'),
mkdirp = require('mkdirp'),
tosecure = require('./utils/middlewares/tosecure');
var app = express();
var env = process.env.NODE_ENV || "development";
var config = require('./config/config.js')[env];
mkdirp.sync(config.imagesdir);
// create session store
var sessionStore = new MysqlStore({
host: config.host,
port: config.port || 3306,
user: config.username,
password: config.password,
database: config.database,
expiration: 604800000, // 7 days
checkExpirationInterval: 3600000 // every hour
});
// ignore openshift's haproxy health check
app.use(function (req, res, next) {
if (typeof req.header('user-agent') === 'undefined') {
return res.status(200).send('OK').end();
}
next();
});
// Trusting Openshift proxy
app.enable('trust proxy');
// Redirecting from HTTP to HTTPS
app.use(tosecure);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// i18n configuration
i18n.configure({
locales: ['en', 'pt'],
directory: path.join(__dirname, 'locales')
});
// passport configuration
require('./config/passport')(passport);
// uncomment after placing your favicon in /public
app.use(favicon(__dirname + '/public/favicon.png'));
if (app.get('env') !== 'test') {
app.use(logger('dev'));
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(session({
secret: (process.env.WRITERSTRAIL_SESSION_SECRET || 'changemeasimnotsecret'),
store: sessionStore,
saveUninitialized: false,
resave: false,
cookie: {
maxAge: null
}
}));
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use('/bower_components', express.static(path.join(__dirname, 'bower_components')));
app.use(uglify(path.join(__dirname, 'public'), {
generateSourceMap: env !== 'production'
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(flash());
app.use(i18n.init);
app.use(csrf());
app.use(passport.initialize());
app.use(passport.session());
app.use(paginate.middleware(30, 100));
var maintenance = require('./utils/middlewares/maintenance');
var routes = require('./routes/index');
var infoRoutes = require('./routes/info');
var authRoutes = require('./routes/auth')(passport);
var adminRoutes = require('./routes/admin');
var userRoutes = require('./routes/users');
var feedbackRoutes = require('./routes/feedback');
var navlist = require('./routes/navlist');
// Adding locals
app.use(function (req, res, next) {
res.locals.csrf = req.csrfToken();
if (req.isAuthenticated()) {
res.locals.user = req.user;
}
res.locals.navlist = navlist(req);
res.locals._ = _;
res.locals.moment = moment;
res.locals.baseurl = config.baseurl;
res.locals.fbnamespace = config.facebook.namespace;
next();
});
app.use(maintenance);
app.use('/', routes);
app.use('/', infoRoutes);
app.use('/', authRoutes);
app.use('/admin', adminRoutes);
app.use('/', userRoutes);
app.use('/feedback', feedbackRoutes);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
/*jshint -W098*/
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// 404 error handler
app.use(function (err, req, res, next) {
if (404 !== err.status) { return next(err); }
res.status(404);
res.render('error/404', {
title: req.__('Page not found - 404'),
section: '404'
});
});
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
console.log(err);
res.render('error/500', {
title: 'Server error - 500',
section: '505'
});
});
module.exports = app;