forked from Midburn/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
210 lines (179 loc) · 6.64 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
morganLogger = require('morgan'),
bodyParser = require('body-parser'),
passport = require('passport'),
session = require('express-session'),
flash = require('connect-flash'),
cookieParser = require('cookie-parser'),
fileUpload = require('express-fileupload'),
log = require('./libs/logger')(module),
recaptcha = require('express-recaptcha'),
compileSass = require('express-compile-sass'),
recaptchaConfig = require('config').get('recaptcha'),
KnexSessionStore = require('connect-session-knex')(session),
knex = require('./libs/db').knex,
compression = require('compression'),
opbeat = (process.env.NODE_ENV === 'production') ?
require('opbeat').start({
appId: process.env.OPBEAT_APP_ID,
organizationId: process.env.OPBEAT_ORGANIZATION_ID,
secretToken: process.env.OPBEAT_SECRET_TOKEN
}) : {}
log.info('Spark is starting...');
// Creating Express application
var app = express();
// FavIcon registration
app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
// Log every HTTP request
app.use(morganLogger('dev', {
stream: log.logger.stream({
level: 'info',
filter: function (message) {
if ((typeof message === "undefined") || (message === null)) return true;
return !(message.includes('/stylesheets/') || message.includes('/images/'));
}
})
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(fileUpload());
var root = process.cwd();
app.use(compileSass({
root: root + '/public',
sourceMap: true, // Includes Base64 encoded source maps in output css
sourceComments: true, // Includes source comments in output css
watchFiles: true, // Watches sass files and updates mtime on main files for each change
logToConsole: false // If true, will log to console.error on errors
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(function (req, res, next) {
res.locals.req = req;
res.locals.path = req.path.split('/');
next();
});
// Passport setup
require('./libs/passport')(passport);
// using session storage in DB - allows multiple server instances + cross session support between node js apps
var sessionStore = new KnexSessionStore({
knex: knex
});
app.use(session({
secret: 'SparklePoniesAreFlyingOnEsplanade', //TODO check - should we put this on conifg / dotenv files?
resave: false,
saveUninitialized: false,
maxAge: 1000 * 60 * 30,
store: sessionStore
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// compress all responses
app.use(compression());
// i18N Setup
var i18next = require('i18next');
var middleware = require('i18next-express-middleware');
var backend = require('i18next-node-fs-backend');
i18next
.use(middleware.LanguageDetector)
.use(backend)
.init({
whitelist: ['en', 'he'],
fallbackLng: 'en',
load: 'languageOnly',
debug: false,
//namespaces
ns: ['common', 'camps', 'npo', 'gate', 'events','suppliers'],
defaultNS: 'common',
fallbackNS: 'common',
backend: {
// path where resources get loaded from
loadPath: 'locales/{{lng}}/{{ns}}.json',
// path to post missing resources
addPath: 'locales/{{lng}}.missing.json',
// jsonIndent to use when storing json files
jsonIndent: 2
},
detection: {
// order and from where user language should be detected
order: ['path', 'session', 'querystring', 'cookie', 'header'],
// keys or params to lookup language from
lookupQuerystring: 'lng',
lookupCookie: 'i18next',
lookupSession: 'lng',
//lookupPath: 'lng',
lookupFromPathIndex: 0
// cache user language
//caches: true // ['cookie']
// optional expire and domain for set cookie
//cookieExpirationDate: new Date(),
//cookieDomain: 'SparkMidburn'
}
}, function () {
middleware.addRoute(i18next, '/:lng', ['en', 'he'], app, 'get', function (req, res) {
//endpoint function
//log.info("ROUTE");
});
});
app.use(middleware.handle(i18next, {
ignoreRoutes: ['images/', 'images', 'images/', '/images/', 'stylesheets', '/favicon.ico'],
removeLngFromUrl: false
}));
// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// user roles / permissions
var userRole = require('./libs/user_role');
app.use(userRole.middleware());
// Infrastructure Routes
if (app.get('env') === 'development' || app.get('env') === 'testing') {
app.use('/dev', require('./routes/pages/dev_routes'));
require('./routes/api/fake_drupal')(app);
}
// Mail
var mail = require('./libs/mail');
mail.setup(app);
/** #####################
* Mapping Routes
##################### */
// Mapping all Api routes
require('./routes/index.js').api(app, passport);
// Maping all page routes
app.use("/", require('./routes/index.js').app);
// Recaptcha setup with siteId & secret
recaptcha.init(recaptchaConfig.sitekey, recaptchaConfig.secretkey);
log.info('Spark environment: NODE_ENV =', process.env.NODE_ENV, ', app.env =', app.get('env'));
// Handler for unhandled rejections
process.on('unhandledRejection', function (reason, p) {
log.error("Possibly Unhandled Rejection at: Promise ", p, " reason: ", reason);
});
process.on('warning', function (warning) {
log.warn(warning.name); // Print the warning name
log.warn(warning.message); // Print the warning message
log.warn(warning.stack); // Print the stack trace
});
// ==================
// Opbeat Integration
// ==================
// We want to enable Opbeat in production only,
// so dev. errors don't obscure actual production issues
if (process.env.NODE_ENV === 'production') {
app.use(opbeat.middleware.express())
}
// == Export our app ==
module.exports = app;
log.info("--- Spark is running :) ---");
if (process.env.DRUPAL_TICKET_SYNC_EVERY_X_MINUTES) {
log.info("Drupal ticket sync is on. Sync will run every X minutes: ", process.env.DRUPAL_TICKET_SYNC_EVERY_X_MINUTES);
setTimeout(() => {
var drupalTicketSync = require('./scripts/drupal_ticket_sync');
drupalTicketSync.runSyncTicketsLoop(process.env.DRUPAL_TICKET_SYNC_EVERY_X_MINUTES);
}, 10000);
}
else {
log.warn("Drupal ticket sync is disabled. To run, set DRUPAL_TICKET_SYNC_EVERY_X_MINUTES in your .env file")
}