-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp-init.js
155 lines (123 loc) · 5.02 KB
/
app-init.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
'use strict';
require('dotenv').config();
const bodyParser = require('body-parser');
//const client = require('./client');
const cookieParser = require('cookie-parser');
const config = require('./config');
const express = require('express');
const expressSession = require('express-session');
const fs = require('fs');
const https = require('https');
const passport = require('passport');
const path = require('path');
const nunjucks = require('nunjucks');
const dateFilter = require('./nunjucks/dateFilter');
const currencyFilter = require('./nunjucks/currency');
const limitTo = require('./nunjucks/limitTo');
const jsonFilter = require('./nunjucks/json');
const timestampFilter = require('./nunjucks/timestamp');
const replaceIdeaVariablesFilter = require('./nunjucks/replaceIdeaVariables');
const flash = require('express-flash');
const expressValidator = require('express-validator');
const MongoStore = require('connect-mongo')(expressSession);
//const MemoryStore = expressSession.MemoryStore;
/*const MySQLStore = require('express-mysql-session')(expressSession);
var options = ;
const sessionStore = new MySQLStore({
port: 3306,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_SESSIONS,
});
*/
function getMongoDbConnectionString () {
// Allow the connection string builder to be overridden by an environment variable
if (process.env.MONGO_DB_CONNECTION_STRING) {
return process.env.MONGO_DB_CONNECTION_STRING.replace("{database}", 'sessions');
}
const host = process.env.MONGO_DB_HOST || 'localhost';
const port = process.env.MONGO_DB_PORT || 27017;
const user = process.env.MONGO_DB_USER || '';
const password = process.env.MONGO_DB_PASSWORD || '';
const authSource = process.env.MONGO_DB_AUTHSOURCE || '';
const useAuth = user && password;
return `mongodb://${useAuth ? `${user}:${password}@` : ''}${host}:${port}/sessions${authSource ? `?authSource=${authSource}` : ''}`;
}
const url = getMongoDbConnectionString();
const sessionStore = new MongoStore({
url: url,
ttl: 700 * 24 * 60 * 60 // = 700 days. Default
})
// Express configuration
const app = express();
const nunjucksEnv = nunjucks.configure('views', { autoescape: true, express: app });
app.set('view engine', 'html');
app.set('port', process.env.PORT || 4000);
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use((req, res, next) => {
req.nunjucksEnv = nunjucksEnv;
next();
});
let sessionCookieConfig;
// add complete config for debug purposes
if (process.env.SESSION_COOKIES_CONFIG) {
sessionCookieConfig = JSON.parse(process.env.SESSION_COOKIES_CONFIG);
} else {
sessionCookieConfig = {
maxAge: config.session.maxAge,
// domain: 'localhost',
secure: process.env.COOKIE_SECURE_OFF === 'yes' ? false : true,
httpOnly: process.env.COOKIE_SECURE_OFF === 'yes' ? false : true,
}
}
const sessionConfig = {
saveUninitialized : true,
resave : true,
secret : config.session.secret,
store : sessionStore,
key : 'openstad-authorization.sid',
cookie : sessionCookieConfig,
};
// Session Configuration
app.use(expressSession(sessionConfig));
app.use(flash());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
app.use(expressValidator());
app.use((req, res, next) => {
console.log('=====> REQUEST: ', req.originalUrl);
console.log('=====> query: ', req.query);
console.log('=====> ip: ', req.headers['x-forwarded-for'] || req.socket.remoteAddress, req.ip);
console.log('=====> body: ', req.body);
console.log('=====> session: ', req.session);
next();
});
// Passport configuration
require('./auth');
// static resources for stylesheets, images, javascript files
require('./routes/routes')(app);
// /**
// * From time to time we need to clean up any expired tokens
// * in the database
// */
// setInterval(() => {
// db
// .accessTokens
// .removeExpired()
// .catch(err => console.error('Error trying to remove expired tokens:', err.stack));
// }, config.db.timeToCheckExpiredTokens * 1000);
//
// // Set the ip-address of your trusted reverse proxy server such as
// // haproxy or Apache mod proxy or nginx configured as proxy or others.
// // The proxy server should insert the ip address of the remote client
// // through request header 'X-Forwarded-For' as
// // 'X-Forwarded-For: some.client.ip.address'
// // Insertion of the forward header is an option on most proxy software
//
// app.set('trust proxy', '127.0.0.1');
module.exports = app;