Skip to content

Commit

Permalink
Refactor config
Browse files Browse the repository at this point in the history
  • Loading branch information
memochou1993 committed Dec 24, 2022
1 parent b665b0a commit 901a7cd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 165 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
APP_ENV=local
APP_DEBUG=true
APP_URL=
APP_PORT=3000
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.vscode
.env
.env.*
104 changes: 25 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,34 +135,14 @@ Install dependencies.
npm ci
```

Copy `.env.example` to `.env`.

```bash
cp .env.example .env
```

Set the environment variables as follows:

```env
APP_ENV=local
APP_DEBUG=true
APP_PORT=3000
# optional
# VERCEL_GIT_REPO_SLUG=gpt-ai-assistant
# VERCEL_ACCESS_TOKEN=<your_vercel_access_token>
# VERCEL_DEPLOY_HOOK_URL=<your_vercel_deploy_hook_url>
### Tests

# optional
# OPENAI_API_KEY=<your_openai_api_key>
Copy `.env.example` to `.env.test`.

# optional
# LINE_CHANNEL_ACCESS_TOKEN=<your_line_channel_access_token>
# LINE_CHANNEL_SECRET=<your_line_channel_secret>
```bash
cp .env.example .env.test
```

### Tests

Run the tests.

```bash
Expand All @@ -172,34 +152,44 @@ npm run test
Check the results.

```bash
> gpt-ai-assistant@1.0.0 test
> gpt-ai-assistant@0.0.0 test
> jest

console.info
=== 000000 ===

AI: 嗨!我可以怎麼幫助你?
Human: 嗨?
AI: OK

at Assistant.info [as debug] (assistant/assistant.js:55:28)

PASS assistant/index.test.js
✓ assistant works (1689 ms)
AI: OK!

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.579 s, estimated 4 s
Ran all test suites.
Time: 1 s
```

### Using Proxy Server

Copy `.env.example` to `.env`.

```bash
cp .env.example .env
```

Set the environment variables as follows:

```env
APP_ENV=production
APP_DEBUG=true
APP_PORT=3000
VERCEL_GIT_REPO_SLUG=gpt-ai-assistant
VERCEL_ACCESS_TOKEN=<your_vercel_access_token>
VERCEL_DEPLOY_HOOK_URL=<your_vercel_deploy_hook_url>
OPENAI_API_KEY=<your_openai_api_key>
LINE_CHANNEL_ACCESS_TOKEN=<your_line_channel_access_token>
LINE_CHANNEL_SECRET=<your_line_channel_secret>
```

Start a local server.
Expand All @@ -214,7 +204,7 @@ Start a proxy server.
ngrok http 3000
```

Go back to the website, modify the "Webhook URL" to e.g. "<https://0000-0000-0000.jp.ngrok.io>" and click the "Update" button.
Go back to the [LINE](https://developers.line.biz/) website, modify the "Webhook URL" to e.g. "<https://0000-0000-0000.jp.ngrok.io/webhook>" and click the "Update" button.

Send a message from the LINE mobile app.

Expand All @@ -231,50 +221,6 @@ Human: 嗨?
AI: 很高興見到你!有什麼可以為你服務的嗎?
```

### Using Local Server

Start a local server.

```bash
npm run dev
```

Send a dummy request to the local server.

```bash
curl --request POST \
--url http://localhost:3000/webhook \
--header 'Content-Type: application/json' \
--data '{
"events": [
{
"type": "message",
"source": {
"type": "user",
"userId": "000000"
},
"message": {
"type": "text",
"text": "我是誰"
}
}
]
}'
```

Check the results.

```bash
> [email protected] dev
> node api/index.js

=== 000000 ===

AI: 嗨!我可以怎麼幫助你?
Human: 我是誰?
AI: OK
```

## Changelog

Detailed changes for each release are documented in the [release notes](https://github.com/memochou1993/gpt-ai-assistant/releases).
Expand Down
110 changes: 28 additions & 82 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,35 +169,15 @@ cd gpt-ai-assistant
npm ci
```

建立 `.env` 檔。

```bash
cp .env.example .env
```

設置環境變數如下:

```env
APP_ENV=local
APP_DEBUG=true
APP_PORT=3000
# optional
# VERCEL_GIT_REPO_SLUG=gpt-ai-assistant
# VERCEL_ACCESS_TOKEN=<your_vercel_access_token>
# VERCEL_DEPLOY_HOOK_URL=<your_vercel_deploy_hook_url>
### 測試

# optional
# OPENAI_API_KEY=<your_openai_api_key>
建立 `.env.test` 檔。

# optional
# LINE_CHANNEL_ACCESS_TOKEN=<your_line_channel_access_token>
# LINE_CHANNEL_SECRET=<your_line_channel_secret>
```bash
cp .env.example .env.test
```

### 測試

在終端機使用以下指令,運行測試,向 OpenAI 伺服器發送請求。
在終端機使用以下指令,運行測試。

```bash
npm run test
Expand All @@ -206,34 +186,44 @@ npm run test
查看結果。

```bash
> gpt-ai-assistant@1.0.0 test
> gpt-ai-assistant@0.0.0 test
> jest

console.info
=== 000000 ===

AI: 嗨!我可以怎麼幫助你?
Human: 嗨?
AI:

at Assistant.info [as debug] (assistant/assistant.js:55:28)

PASS assistant/index.test.js
✓ assistant works (1689 ms)
AI: OK!

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.579 s, estimated 4 s
Ran all test suites.
Time: 1 s
```

### 反向代理
### 使用代理伺服器

建立 `.env` 檔。

```bash
cp .env.example .env
```

修改環境變數如下
設置環境變數如下

```env
APP_ENV=production
APP_DEBUG=true
APP_PORT=3000
VERCEL_GIT_REPO_SLUG=gpt-ai-assistant
VERCEL_ACCESS_TOKEN=<your_vercel_access_token>
VERCEL_DEPLOY_HOOK_URL=<your_vercel_deploy_hook_url>
OPENAI_API_KEY=<your_openai_api_key>
LINE_CHANNEL_ACCESS_TOKEN=<your_line_channel_access_token>
LINE_CHANNEL_SECRET=<your_line_channel_secret>
```

在終端機使用以下指令,啟動一個本地伺服器。
Expand All @@ -248,7 +238,7 @@ npm run dev
ngrok http 3000
```

回到 Line 平台,修改「Webhook URL」,例如「<https://0000-0000-0000.jp.ngrok.io>」,點選「Update」按鈕。
回到 [LINE](https://developers.line.biz/) 平台,修改「Webhook URL」,例如「<https://0000-0000-0000.jp.ngrok.io/webhook>」,點選「Update」按鈕。

使用 LINE 手機應用程式發送訊息。

Expand All @@ -265,50 +255,6 @@ Human: 嗨?
AI: 很高興見到你!有什麼可以為你服務的嗎?
```

### 模擬請求

在終端機使用以下指令,啟動一個本地伺服器。

```bash
npm run dev
```

在另一個終端機使用以下指令,模擬 LINE 伺服器向本地伺服器發送請求,再由本地伺服器向 OpenAI 伺服器發送請求。

```bash
curl --request POST \
--url http://localhost:3000/webhook \
--header 'Content-Type: application/json' \
--data '{
"events": [
{
"type": "message",
"source": {
"type": "user",
"userId": "000000"
},
"message": {
"type": "text",
"text": "我是誰"
}
}
]
}'
```

查看結果。

```bash
> [email protected] dev
> node api/index.js

=== 000000 ===

AI: 嗨!我可以怎麼幫助你?
Human: 我是誰?
AI:
```

## 更新日誌

請到「[Releases](https://github.com/memochou1993/gpt-ai-assistant/releases)」頁面查看發布通知。
Expand Down
8 changes: 5 additions & 3 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import dotenv from 'dotenv';

dotenv.config();

const { env } = process;

dotenv.config({
path: env.NODE_ENV ? `.env.${env.NODE_ENV}` : '.env',
});

const config = Object.freeze({
APP_ENV: env.APP_ENV || 'production',
APP_ENV: env.NODE_ENV || 'production',
APP_DEBUG: env.APP_DEBUG === 'true' || false,
APP_URL: env.APP_URL || null,
APP_PORT: env.APP_PORT || null,
Expand Down

0 comments on commit 901a7cd

Please sign in to comment.