Skip to content

Commit

Permalink
feat: node name
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Aug 15, 2019
1 parent eba8f46 commit c187811
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/en/install/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ Use environment variables is recommended to avoid conflicts during upgrade.

`PROXY_URL_REGEX`: regex for url of enabling proxy, default to `.*`

`NODE_NAME`: node name, used for load balancing, identify current node

### User Authentication

Routes in `protected_route.js` will be protected using HTTP Basic Authentication.
Expand Down
2 changes: 2 additions & 0 deletions docs/install/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式

`LOGGER_LEVEL`: 指明输出到 console 和日志文件的日志的最大[等级](https://github.com/winstonjs/winston#logging-levels),默认 `info`

`NODE_NAME`: 节点名,用于负载均衡,识别当前节点

### 部分 RSS 模块配置

- pixiv 全部路由: [注册地址](https://accounts.pixiv.net/signup)
Expand Down
1 change: 1 addition & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = {
password: process.env.REDIS_PASSWORD || null,
},
},
nodeName: process.env.NODE_NAME,
pixiv: {
client_id: 'MOBrBDS8blbauoSck0ZfDbtuzpyT',
client_secret: 'lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj',
Expand Down
3 changes: 3 additions & 0 deletions lib/middleware/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const headers = {
'Content-Type': 'application/xml; charset=utf-8',
'Cache-Control': `public, max-age=${config.cache.routeExpire}`,
};
if (config.nodeName) {
headers['RSSHub-Node'] = config.nodeName;
}

module.exports = async (ctx, next) => {
logger.info(`${ctx.url}, user IP: ${ctx.ips[0] || ctx.ip}`);
Expand Down
6 changes: 6 additions & 0 deletions lib/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ module.exports = async (ctx) => {
ctx.body = art(path.resolve(__dirname, '../views/welcome.art'), {
showDebug,
debug: [
config.nodeName
? {
name: '节点名',
value: config.nodeName,
}
: null,
{
name: 'git hash',
value: gitHash,
Expand Down
4 changes: 3 additions & 1 deletion lib/views/welcome.art
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
details {
text-align: left;
max-height: 400px;
overflow: scroll;
overflow: auto;
}

details::-webkit-scrollbar {
Expand Down Expand Up @@ -91,10 +91,12 @@
<details>
<summary>debug</summary>
{{ each debug }}
{{ if $value }}
<div class="debug-item">
<span class="debug-key">{{ $value.name }}: </span>
<span class="debug-value">{{@ $value.value }}</span>
</div>
{{ /if }}
{{ /each }}
</details>
{{ /if }}
Expand Down
5 changes: 5 additions & 0 deletions test/middleware/debug.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
process.env.NODE_NAME = 'mock';

const supertest = require('supertest');
const { server } = require('../../lib/index');
const request = supertest(server);
Expand Down Expand Up @@ -34,6 +36,9 @@ describe('debug', () => {
.html()
.trim();
switch (key) {
case '节点名:':
expect(value).toBe('mock');
break;
case 'git hash:':
expect(value).toBe(gitHash);
break;
Expand Down
4 changes: 3 additions & 1 deletion test/middleware/header.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
process.env.NODE_NAME = 'mock';

const supertest = require('supertest');
const { server } = require('../../lib/index');
const request = supertest(server);
Expand All @@ -15,6 +17,6 @@ describe('header', () => {
expect(response.headers['content-type']).toBe('application/xml; charset=utf-8');
expect(response.headers['cache-control']).toBe(`public, max-age=${config.cache.routeExpire}`);
expect(response.headers['last-modified']).toBe(response.text.match(/<lastBuildDate>(.*)<\/lastBuildDate>/)[1]);
// expect(response.headers.etag).toBe('"b37-MORyrF0tJ8BFw0xLLZL/zBYAFPY"');
expect(response.headers['rsshub-node']).toBe('mock');
});
});

0 comments on commit c187811

Please sign in to comment.