forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (67 loc) · 1.86 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
const Koa = require('koa');
const logger = require('./utils/logger');
const config = require('./config');
const onerror = require('./middleware/onerror');
const header = require('./middleware/header.js');
const utf8 = require('./middleware/utf8');
const memoryCache = require('./middleware/lru-cache.js');
const redisCache = require('./middleware/redis-cache.js');
const filter = require('./middleware/filter.js');
const template = require('./middleware/template.js');
const favicon = require('koa-favicon');
const debug = require('./middleware/debug.js');
const router = require('./router');
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);
// 6 debug
app.context.debug = {
hitCache: 0,
request: 0,
routes: [],
ips: [],
};
app.use(debug);
// 5 fix incorrect `utf-8` characters
app.use(utf8);
// 4 generate body
app.use(template);
// 3 filter content
app.use(filter);
// 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.');
},
})
);
}
// router
app.use(router.routes()).use(router.allowedMethods());
app.listen(config.port, parseInt(config.listenInaddrAny) ? null : '127.0.0.1');