forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (105 loc) · 3.18 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
118
119
120
121
122
123
124
125
126
const config = require('./config');
if (config.newrelicLicenseKey) {
require('newrelic');
}
const Koa = require('koa');
const fs = require('fs');
const logger = require('./utils/logger');
const onerror = require('./middleware/onerror');
const header = require('./middleware/header');
const utf8 = require('./middleware/utf8');
const memoryCache = require('./middleware/lru-cache');
const redisCache = require('./middleware/redis-cache');
const parameter = require('./middleware/parameter');
const template = require('./middleware/template');
const favicon = require('koa-favicon');
const debug = require('./middleware/debug');
const accessControl = require('./middleware/access-control');
const router = require('./router');
const protected_router = require('./protected_router');
const mount = require('koa-mount');
// API related
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);
});
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
if (config.cacheType === 'memory') {
app.use(
memoryCache({
app: app,
expire: config.cacheExpire,
ignoreQuery: true,
})
);
} else if (config.cacheType === 'redis') {
app.use(
redisCache({
app: app,
expire: config.cacheExpire,
ignoreQuery: true,
redis: config.redis,
onerror: (e) => {
logger.error('Redis error: ', e);
},
onconnect: () => {
logger.info('Redis connected.');
},
})
);
} else {
app.context.cache = {
get: () => null,
set: () => null,
};
}
// 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
if (config.connect.port) {
app.listen(config.connect.port, parseInt(config.listenInaddrAny) ? null : '127.0.0.1');
logger.info('Listening Port ' + config.connect.port);
}
if (config.connect.socket) {
if (fs.existsSync(config.connect.socket)) {
fs.unlinkSync(config.connect.socket);
}
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();
});
}