feat: use cluster
This commit is contained in:
parent
de34f179cd
commit
d0b8987f4a
|
|
@ -81,4 +81,5 @@ module.exports = {
|
|||
},
|
||||
blacklist: process.env.BLACKLIST && process.env.BLACKLIST.split(','),
|
||||
whitelist: process.env.WHITELIST && process.env.WHITELIST.split(','),
|
||||
disableCluster: process.env.DISABLE_CLUSTER,
|
||||
};
|
||||
|
|
|
|||
151
lib/index.js
151
lib/index.js
|
|
@ -25,78 +25,87 @@ const apiTemplate = require('./middleware/api-template');
|
|||
const api_router = require('./api_router');
|
||||
const apiResponseHandler = require('./middleware/api-response-handler');
|
||||
|
||||
process.on('uncaughtException', (e) => {
|
||||
logger.error('uncaughtException: ' + e);
|
||||
});
|
||||
const cluster = require('cluster');
|
||||
const numCPUs = require('os').cpus().length;
|
||||
|
||||
logger.info('🎉 RSSHub start! Cheers!');
|
||||
|
||||
const app = new Koa();
|
||||
app.proxy = true;
|
||||
|
||||
// favicon
|
||||
app.use(favicon(__dirname + '/favicon.png'));
|
||||
|
||||
// global error handing
|
||||
app.use(onerror);
|
||||
|
||||
// 1 set header
|
||||
app.use(header);
|
||||
|
||||
app.use(accessControl);
|
||||
|
||||
// 6 debug
|
||||
app.context.debug = {
|
||||
hitCache: 0,
|
||||
request: 0,
|
||||
routes: [],
|
||||
ips: [],
|
||||
};
|
||||
app.use(debug);
|
||||
|
||||
// 5 fix incorrect `utf-8` characters
|
||||
app.use(utf8);
|
||||
|
||||
app.use(apiTemplate);
|
||||
app.use(apiResponseHandler());
|
||||
|
||||
// 4 generate body
|
||||
app.use(template);
|
||||
// 3 filter content
|
||||
app.use(parameter);
|
||||
|
||||
// 2 cache
|
||||
app.use(cache(app));
|
||||
|
||||
// router
|
||||
app.use(mount('/', router.routes())).use(router.allowedMethods());
|
||||
|
||||
// routes the require authentication
|
||||
app.use(mount('/protected', protected_router.routes())).use(protected_router.allowedMethods());
|
||||
|
||||
// API router
|
||||
app.use(mount('/api', api_router.routes())).use(api_router.allowedMethods());
|
||||
|
||||
// connect
|
||||
let server;
|
||||
if (config.connect.socket) {
|
||||
if (fs.existsSync(config.connect.socket)) {
|
||||
fs.unlinkSync(config.connect.socket);
|
||||
if (cluster.isMaster && !config.disableCluster && process.env.NODE_ENV !== 'test') {
|
||||
for (let i = 0; i < numCPUs; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
server = app.listen(config.connect.socket, parseInt(config.listenInaddrAny) ? null : '127.0.0.1');
|
||||
logger.info('Listening Unix Socket ' + config.connect.socket);
|
||||
process.on('SIGINT', () => {
|
||||
fs.unlinkSync(config.connect.socket);
|
||||
process.exit();
|
||||
} else {
|
||||
process.on('uncaughtException', (e) => {
|
||||
logger.error('uncaughtException: ' + e);
|
||||
});
|
||||
}
|
||||
if (config.connect.port) {
|
||||
server = app.listen(config.connect.port, parseInt(config.listenInaddrAny) ? null : '127.0.0.1');
|
||||
logger.info('Listening Port ' + config.connect.port);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
server: server,
|
||||
app: app,
|
||||
cache: app.context.cache.client,
|
||||
};
|
||||
logger.info('🎉 RSSHub start! Cheers!');
|
||||
|
||||
const app = new Koa();
|
||||
app.proxy = true;
|
||||
|
||||
// favicon
|
||||
app.use(favicon(__dirname + '/favicon.png'));
|
||||
|
||||
// global error handing
|
||||
app.use(onerror);
|
||||
|
||||
// 1 set header
|
||||
app.use(header);
|
||||
|
||||
app.use(accessControl);
|
||||
|
||||
// 6 debug
|
||||
app.context.debug = {
|
||||
hitCache: 0,
|
||||
request: 0,
|
||||
routes: [],
|
||||
ips: [],
|
||||
};
|
||||
app.use(debug);
|
||||
|
||||
// 5 fix incorrect `utf-8` characters
|
||||
app.use(utf8);
|
||||
|
||||
app.use(apiTemplate);
|
||||
app.use(apiResponseHandler());
|
||||
|
||||
// 4 generate body
|
||||
app.use(template);
|
||||
// 3 filter content
|
||||
app.use(parameter);
|
||||
|
||||
// 2 cache
|
||||
app.use(cache(app));
|
||||
|
||||
// router
|
||||
app.use(mount('/', router.routes())).use(router.allowedMethods());
|
||||
|
||||
// routes the require authentication
|
||||
app.use(mount('/protected', protected_router.routes())).use(protected_router.allowedMethods());
|
||||
|
||||
// API router
|
||||
app.use(mount('/api', api_router.routes())).use(api_router.allowedMethods());
|
||||
|
||||
// connect
|
||||
let server;
|
||||
if (config.connect.socket) {
|
||||
if (fs.existsSync(config.connect.socket)) {
|
||||
fs.unlinkSync(config.connect.socket);
|
||||
}
|
||||
server = app.listen(config.connect.socket, parseInt(config.listenInaddrAny) ? null : '127.0.0.1');
|
||||
logger.info('Listening Unix Socket ' + config.connect.socket);
|
||||
process.on('SIGINT', () => {
|
||||
fs.unlinkSync(config.connect.socket);
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
if (config.connect.port) {
|
||||
server = app.listen(config.connect.port, parseInt(config.listenInaddrAny) ? null : '127.0.0.1');
|
||||
logger.info('Listening Port ' + config.connect.port);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
server: server,
|
||||
app: app,
|
||||
cache: app.context.cache.client,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue