-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
177 lines (161 loc) · 5.2 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
// Libaries
const express = require('express');
const path = require('path');
const morgan = require('morgan');
const http = require('http');
const https = require('https');
const fs = require('fs');
const logger = require('./logger');
const chalk = require('chalk');
const helmet = require('helmet');
const sso = require('./auth/login.js');
// Config
const config = require(path.join(__dirname, 'config.js'));
// Router
const api = require(path.join(__dirname, 'routes/api'));
const pages = require(path.join(__dirname, 'routes/pages'));
// Database
const db = require(path.join(__dirname, '/database/database.js'));
// Objects
const app = express();
// Register helmet
app.use(helmet(config.server.HELMET));
// Register logger for network requests
if (config.dev.DEBUG) {
const morganStream = {
write: (message) => {
logger.network(message.trim());
}
};
app.use(morgan('dev', { stream: morganStream }));
}
// Register middleware for static variable and configurable data for rendering
app.use(async (req, res, next) => {
res.locals.imprinturl = config.web.IMPRINT_URL;
res.locals.faqurl = config.web.FAQ_URL;
res.locals.faqurlactivemenu = config.web.FAQ_URL_ACTIVE;
res.locals.faqactivatestartpage = config.web.FAQ_STARTPAGE_ACTIVE;
res.locals.socialmedia = config.web.SOCIAL_MEDIA_ACTIVE;
res.locals.facebookurl = config.web.FACEBOOK_URL;
res.locals.twitterurl = config.web.TWITTER_URL;
res.locals.instagramurl = config.web.INSTAGRAM_URL;
res.locals.pagename = config.web.PAGE_NAME;
res.locals.privacypolicyurl = config.web.PRIVACY_POLICY_URL;
res.locals.supportresponsible = config.web.SUPPORT_RESPONSIBLE;
res.locals.supportemailactive = config.web.SUPPORT_EMAIL_ACTIVE;
res.locals.supportemail = config.web.SUPPORT_EMAIL;
res.locals.supportlinkactive = config.web.SUPPORT_LINK_ACTIVE;
res.locals.supportlink = config.web.SUPPORT_LINK;
res.locals.developmentmode = config.dev.DEVELOPMENT_MODE;
next();
});
// Static assets
app.use('/assets', express.static('public'));
app.use(
'/assets/bootstrap',
express.static(path.join(__dirname, 'node_modules/bootstrap/dist'))
);
app.use(
'/assets/animate',
express.static(path.join(__dirname, 'node_modules/animate.css'))
);
app.use(
'/assets/animateonscroll',
express.static(path.join(__dirname, 'node_modules/aos/dist'))
);
app.use(
'/assets/typed',
express.static(path.join(__dirname, 'node_modules/typed.js/dist'))
);
app.use(
'/assets/sweetalert2',
express.static(path.join(__dirname, 'node_modules/sweetalert2/dist'))
);
// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
app.use(
'/assets/jquery',
express.static(path.join(__dirname, 'node_modules/jquery/dist'))
);
app.use(
'/assets/fontawesome',
express.static(path.join(__dirname, '/node_modules/font-awesome'))
);
app.use(
'/assets/select2',
express.static(path.join(__dirname, 'node_modules/select2/dist'))
);
// Set view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Register login
sso.setupSessionAndOpenID(app);
// Routing
app.use('/api', api);
app.use('/', pages);
try {
if (process.env.NODE_ENV !== 'test') {
// Datbase
db.sequelize
.sync()
// for changing the underlying database (delets all content, updates scheme) adjust the line above as decribed in the readme file
/* eslint-enable */
.then(() => {
logger.info('Synced db.');
})
.catch((err) => {
logger.info('Failed to sync db: ' + err.message);
});
// HTTP-Server
if (config.server.ALLOW_HTTP) {
let httpServer = null;
if (config.server.HTTP_REDIRECT) {
logger.debug('HTTP REDIRECT ACTIVE');
httpServer = http.createServer((req, res) => {
res.writeHead(301, {
Location:
'https://' +
config.server.host +
':' +
config.PORT_HTTPS +
req.url
});
});
} else {
httpServer = http.createServer(app);
}
httpServer.listen(config.server.PORT_HTTP, () => {
logger.info(
`The application is available on ${chalk.cyanBright(
`http://${config.server.HOST}:${config.server.PORT_HTTP}`
)}.`
);
});
}
// HTTPS-Server
/* eslint-disable security/detect-non-literal-fs-filename */
if (config.server.ALLOW_HTTPS) {
logger.info('Reading certificate file from ' + config.server.CERT_PATH);
const options = {
key: fs.readFileSync(config.server.CERT_SECRET_PATH, 'utf8'),
cert: fs.readFileSync(config.server.CERT_PATH, 'utf8')
};
/* eslint-enable security/detect-non-literal-fs-filename */
https.createServer(options, app).listen(config.server.PORT_HTTPS, () => {
logger.info(
chalk.green(
`The application is available on ${chalk.cyanBright(
`https://${config.server.HOST}:${config.server.PORT_HTTPS}`
)}.`
)
);
});
}
}
} catch (ex) {
// if an fatal error occurs, log it and exit the application
logger.error(ex);
}
module.exports.app = app;