forked from ifuyun/iFuyun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·117 lines (112 loc) · 3.67 KB
/
index.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
/* jslint nomen:true es5:true */
/* global console,process,__dirname */
/**
* 网站总入口
* @module index
* @main index
* @requires redis, core, routes
* @author Fuyun
* @version 2.0.0
* @since 1.0.0
*/
const express = require('express');
const cluster = require('cluster');
const app = express();
const numCPUs = require('os').cpus().length;
const path = require('path');
const favicon = require('serve-favicon');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const http = require('http');
const ejs = require('ejs');
const compress = require('compression');
const csrf = require('csurf');
const log4js = require('log4js');
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const redis = require('redis');
const redisCfg = require('./config/redis');
const redisClient = redis.createClient(redisCfg.port, redisCfg.host, {'auth_pass': redisCfg.passwd});
const config = require('./config/core');
const routes = require('./config/routes');
const {sysLog, threadLog, accessLog, formatOpLog, updateContext} = require('./helper/logger');
if (cluster.isMaster) {
for (let cpuIdx = 0; cpuIdx < numCPUs; cpuIdx += 1) {
cluster.fork();
}
cluster.on('exit', function (worker, code, signal) {
threadLog.warn(formatOpLog({
msg: `Worker ${worker.process.pid} exit.`,
data: {
code,
signal
}
}));
process.nextTick(function () {
threadLog.info(formatOpLog({
msg: 'New process is forking...'
}));
cluster.fork();
});
});
} else {
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
ejs.delimiter = '?';
app.engine('.html', ejs.__express);
app.use(log4js.connectLogger(accessLog, {
level: log4js.levels.INFO,
format: ':remote-addr - :method :status HTTP/:http-version :url - [:response-time ms/:content-length B] ":referrer" ":user-agent"'
}));
app.use(compress());
app.use(favicon(path.join(__dirname, 'public', 'static', 'favicon.ico')));
// 通过req.cookies调用cookie
app.use(cookieParser(config.cookieSecret));
app.use(session({
name: 'jsid',
store: new RedisStore({
client: redisClient,
ttl: 7 * 24 * 60 * 60
}),
secret: config.sessionSecret,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: config.cookieExpires,
domain: 'ifuyun.com'
}
// expires: new Date(Date.now() + config.cookieExpires),
}));
app.use(bodyParser.json({
limit: '20mb'
}));
app.use(bodyParser.urlencoded({
limit: '20mb',
extended: true
}));
app.use(csrf());
app.enable('trust proxy');
// 设置全局响应头
app.use(function (req, res, next) {
// config.name:响应头不支持中文
res.setHeader('Server', config.author + '/' + config.version);
res.setHeader('X-Powered-By', config.domain);
next();
});
app.use(function (req, res, next) {
updateContext();
threadLog.trace(formatOpLog({
msg: `Request [${req.url}] is processed by ${cluster.isWorker ? 'Worker' : 'Master'}: ${cluster.worker.id}`
}));
next();
});
routes(app, express);
if (require.main === module) {
http.createServer(app).listen(config.port, config.host, () => sysLog.info(formatOpLog({
msg: `Server listening on: ${config.host}:${config.port}`
})));
}
}
if (require.main !== module) {// cluster.isWorker
module.exports = app;
}