-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
118 lines (87 loc) · 3.21 KB
/
server.ts
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
import tracer from 'tracer';
const logger = tracer.colorConsole({format: '{{message}}'});
import express from 'express';
import path from 'path';
const server = express();
import {generateNextConfig} from './config/server/nextConfig';
import createAPIEventMiddleware from './routes/siteTrafficMiddlware';
import StaticFiles from './config/server/staticFiles';
import Security from './config/server/security';
import Tools from './config/server/tools';
import Setup from './config/server/setup';
// const FrontEndPageMap = require('./../src/page-map/');
import Routes from './routes';
import database from './lib/database';
const dev = (process.env.NODE_ENV !== 'production');
const CONSTANTS = {
PAGES_DIRECTORY: path.join(__dirname), //this points to the location of our next.js app
UNSECURED_PORT: 3000
};
const PORT = process.env.PORT || CONSTANTS.UNSECURED_PORT || 3000;
const app = generateNextConfig({
dev,
directory: CONSTANTS.PAGES_DIRECTORY,
port: PORT as number
});
const handle = app.getRequestHandler();
//===----=---=-=--=--===--=-===----=---=-=--=--===--=-===----=---=-=--=--===--=-//
async function run() {
return new Promise(async (resolve, reject) => {
app.prepare().then(async () => {
Setup.checkForAllEnvironmentVariables();
//this checks for a valid database connection
try {
const db = await database;
} catch (error) {
logger.warn('DATABASE CONNECTION ERROR');
logger.warn(error);
}
Tools.configure(server);
StaticFiles.include(server);
// FrontEndPageMap.include(server, app);
Security.configure(server);
Routes.include(server);
// server.use(function (err, req, res, next) {
//
// });
server.use((req, res, next) => {
const domains = {
'brshooter.com': true,
'www.brshooter.com': true
};
const hostname = domains[req.hostname] ? 'www.brshooter.com' : req.hostname;
if (req.headers['x-forwarded-proto'] === 'http') {
res.redirect(301, `https://${hostname}${req.url}`);
return;
}
next();
});
server.use('*', (req, res, next) => createAPIEventMiddleware(req, res, next));
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen(PORT, () => {
// if (err) {
// throw err;
// }
resolve(server);
logger.trace('Server Listening http://localhost:' + PORT);
}).on('error', function (error) {
logger.error(error);
throw error;
});
// server.on('error', (error) => {
// logger.error(error);
// throw error;
// });
}).catch((ex) => {
logger.error(ex.stack);
process.exit(1);
});
});
}
function closeServer() {
app.close();
}
module.exports = run();
module.exports.close = closeServer;