From e0f39bd6f34a942a6ca2d03fe358d83453888dab Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 6 May 2019 00:21:07 +0800 Subject: [PATCH] feat(core): support .env file to config environment variables --- .gitignore | 1 + docs/en/install/README.md | 13 ++++++------- docs/install/README.md | 13 ++++++------- lib/index.js | 1 + package.json | 1 + yarn.lock | 5 +++++ 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index f422c008f..7e603f35d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ tmp *.swp *.iml coverage +.env diff --git a/docs/en/install/README.md b/docs/en/install/README.md index 19e5af992..7661531b6 100644 --- a/docs/en/install/README.md +++ b/docs/en/install/README.md @@ -82,15 +82,14 @@ $ git pull `RSSHub` reads its configurations from `lib/config.js` or system environment variables. -**How to set system environment variables** +Create a `.env` file in the root directory of your project. Add environment-specific variables on new lines in the form of `NAME=VALUE`. For example: -Under Windows, enter `$ set PORT=1000` in cmd +``` +CACHE_TYPE=redis +CACHE_EXPIRE=600 +``` -Under UNIX-based OS, enter `$ PORT=1000` in terminal - -Enter `$ npm start` to start a `RSSHub` install with port `1000`. - -System environment variables set here will be purged after closing cmd/terminal, if you want persist the variables, you can create a simple [batch script](https://en.wikipedia.org/wiki/Batch_file) or [shell script](https://en.wikipedia.org/wiki/Shell_script). +Please notice that it will not override already existed environment variables, more rules please refer to [dotenv](https://github.com/motdotla/dotenv) To configure more options please refer to [Settings](#Settings). diff --git a/docs/install/README.md b/docs/install/README.md index ab545179b..2bdfa7a37 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -86,15 +86,14 @@ $ git pull 可以通过设置环境变量来配置 RSSHub. -**如何设置环境变量** +在项目根目录新建一个 `.env` 文件,每行以 `NAME=VALUE` 格式添加环境变量,例如 -Windows 系统在 cmd.exe 中运行 `$ set PORT=1000` +``` +CACHE_TYPE=redis +CACHE_EXPIRE=600 +``` -macOS & Linux 运行 `$ PORT=1000` - -再运行 `$ npm start` 启动 RSSHub 即可将监听端口设置为 `1000`. - -此处设置的环境变量在关闭终端后就会被清除, 如果您想保存这些配置可以编写一个简单的 [批处理文件](https://en.wikipedia.org/wiki/Batch_file) 或 [shell](https://en.wikipedia.org/wiki/Shell_script). +注意它不会覆盖已有的环境变量,更多规则请参考 [dotenv](https://github.com/motdotla/dotenv) 更多配置项请看 [应用配置](#应用配置) diff --git a/lib/index.js b/lib/index.js index 106db3ded..3eaa7e5da 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,4 @@ +require('dotenv').config(); const config = require('./config'); const Koa = require('koa'); const fs = require('fs'); diff --git a/package.json b/package.json index d01321553..e648352f7 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "co-redis": "2.1.1", "currency-symbol-map": "4.0.4", "dayjs": "1.8.13", + "dotenv": "8.0.0", "etag": "1.8.1", "form-data": "2.3.3", "git-rev-sync": "1.12.0", diff --git a/yarn.lock b/yarn.lock index 5ce6dfb8c..0d2b81530 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3550,6 +3550,11 @@ dot-prop@^4.1.0, dot-prop@^4.1.1: dependencies: is-obj "^1.0.0" +dotenv@8.0.0: + version "8.0.0" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.0.0.tgz#ed310c165b4e8a97bb745b0a9d99c31bda566440" + integrity sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg== + double-ended-queue@^2.1.0-0: version "2.1.0-0" resolved "https://registry.yarnpkg.com/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz#103d3527fd31528f40188130c841efdd78264e5c"