-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
135 lines (110 loc) · 3.29 KB
/
Copy pathindex.ts
File metadata and controls
135 lines (110 loc) · 3.29 KB
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
import * as cluster from 'cluster';
import express, { Request, Response } from 'express';
import { cpus } from 'os';
import { dirname } from 'path';
import { createServer as createServerHTTP } from 'http';
import { createServer as createServerHTTPS } from 'https';
/*
PASSPORT
*/
import {setupSecurity} from './src/startup/security';
import { Instance } from './src';
import { startup } from './src/startup';
import config from 'config';
import DailyRotateFile from 'winston-daily-rotate-file';
import * as logger from 'winston';
import { readFileSync } from 'fs';
import { NextFunction } from 'express-serve-static-core';
import bodyParser = require('body-parser');
logger.add(new logger.transports.Console({format: logger.format.colorize()}));
var port = (config.get('server') as any).port || 443;
var root = dirname(__dirname);
var cCPUs = cpus().length;
// HTTPS Cert options
let cert = (<any>config.get("security")).cert;
var httpsOptions = {
key: readFileSync(cert.key),
cert: readFileSync(cert.cert)
};
if (process.env.NODE_ENV != 'development') {
if (cluster.isMaster) {
startup()
.then(res => {
if (res) {
// Create a worker for each CPU
for (var i = 0; i < cCPUs; i++) {
createWorker();
}
} else {
logger.error('Startup Validation Failed');
}
return;
})
.catch(err => {
logger.error('Startup Validation Failed');
logger.error(err);
});
} else {
var app = express();
app.use(bodyParser.json());
app.all('*', (req: Request, res: Response, next: NextFunction) => {
if(req.secure){
// OK, continue
return next();
};
// handle port numbers if you need non defaults
let path = `https://${req.hostname}:${port}${req.url}`;
res.redirect(path); // express 4.x
})
var instance = Instance(app);
/*
HTTPS Server
*/
createServerHTTPS(httpsOptions, app).listen(port, () => {
logger.info('Listening on cluster: ' + process.pid);
});
}
} else {
logger.add(new DailyRotateFile({
filename: `${process.pid}-%DATE%.log`,
dirname: 'logs',
zippedArchive: true,
maxSize: '20m',
maxFiles: '7d'
}));
var app = express();
app.use(bodyParser.json());
app.all('*', (req: Request, res: Response, next: NextFunction) => {
if(req.secure){
// OK, continue
return next();
};
// handle port numbers if you need non defaults
let path = `https://${req.hostname}:${port}${req.url}`;
res.redirect(path); // express 4.x
})
setupSecurity(app);
// Should always be just before the listen call
var instance = Instance(app);
/*
Auto redirect to HTTPS
*/
createServerHTTP(app).listen(80);
/*createServerHTTP(app).listen(port, () => {
logger.info('Listening on cluster: ' + process.pid);
});*/
createServerHTTPS(httpsOptions, app).listen(port, () => {
logger.info('Listening on cluster: ' + process.pid);
});
}
function createWorker() {
let worker = cluster.fork();
worker.on('online', function() {
logger.info(worker.process.pid + ': Worker is online.');
});
worker.on('exit', function() {
logger.error(worker.process.pid + ': Worker died.');
logger.error(worker.process.pid + ': Restarting worker');
createWorker();
});
}