Skip to content

Commit 0420d67

Browse files
committed
fixed env read
1 parent 3a8fc3f commit 0420d67

File tree

8 files changed

+36
-20
lines changed

8 files changed

+36
-20
lines changed

.env.sample

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
# When adding additional environment variables, the schema in "/src/env.js"
2-
# should be updated accordingly.
3-
4-
# Prisma, ref: https://www.prisma.io/docs/reference/database-reference/connection-urls#env
5-
# 可前可后,服务端应设置公网地址
1+
########################################
2+
# =
63
DATABASE_URL=
74
#DATABASE_URL=
85

6+
NEXT_PUBLIC_SOCKET_URL=
7+
#NEXT_PUBLIC_SOCKET_URL=
8+
9+
NEXT_PUBLIC_BACKEND_URL=
10+
#NEXT_PUBLIC_BACKEND_URL=
11+
12+
PROXY=
13+
#PROXY=
14+
########################################
15+
16+
917
# nginx 打过来,本地和网址一样
1018
NEXT_PUBLIC_APP_URL=
19+
1120
# 可前可后,服务端应设置公网地址
12-
NEXT_PUBLIC_SOCKET_URL=
21+
1322
# 可前可后,服务端应设置公网地址
14-
NEXT_PUBLIC_BACKEND_URL=
1523

1624
# Next Auth
1725
# You can generate a new secret on the command line with:
@@ -58,3 +66,4 @@ DASHSCOPE_API_KEY=
5866
VERCEL_GITHUB_COMMIT_SHA=
5967
NEXT_PUBLIC_PINO_LOGFLARE_API_KEY=
6068
NEXT_PUBLIC_PINO_LOGFLARE_SOURCE_TOKEN=
69+

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cs-magic",
3-
"version": "V0.7.177 (α)",
3+
"version": "V0.7.178 (α)",
44
"repository": "[email protected]:MarkShawn2020/p01.git",
55
"author": "markshawn2020 <[email protected]>",
66
"license": "MIT",
@@ -20,6 +20,7 @@
2020
},
2121
"devDependencies": {
2222
"@types/jest": "^29.5.12",
23+
"dotenv": "^16.4.5",
2324
"jest": "^29.7.0",
2425
"pino-pretty": "^11.0.0",
2526
"ts-jest": "^29.1.2",
+2-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1+
import { env } from "../common-env"
12
import { createHttpInstance } from "./core"
23

3-
// 因为这个api主要用于客户端使用,但是环境变量的读取需要用fs,所以就单独拆一下
4-
const baseURL = process.env.NEXT_PUBLIC_BACKEND_URL
5-
if (!baseURL) throw new Error("no NEXT_PUBLIC_BACKEND_URL env")
6-
74
export const backendApi = createHttpInstance({
8-
baseURL,
5+
baseURL: env.NEXT_PUBLIC_BACKEND_URL,
96
})

packages/common-env/utils/load-env.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Path } from "../../common-path"
33
import { getEnv } from "../index"
44

55
/**
6+
* @deprecated 不建议使用,推荐在package.json 里使用 source .env
67
* 加载并返回环境变量
78
* 返回的原因是因为不能在 browser 端使用
89
*/

packages/common-llm/safe-call-llm.ts

+4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import { HttpsProxyAgent } from "https-proxy-agent"
55
import OpenAI from "openai/index"
66
import { v4 } from "uuid"
77
import { env } from "../common-env"
8+
import { logEnv } from "../common-env/utils/log-env"
89
import { callLlm } from "./call-llm"
910
import { model2provider } from "./model2provider"
1011
import { ICallLlmOptions, ICallLlmResponse } from "./schema/llm"
1112

1213
export const safeCallLLM = async (
1314
options: ICallLlmOptions,
1415
): Promise<ICallLlmResponse> => {
16+
logEnv("api_key")
17+
console.log({ env })
18+
1519
const llmModelType = options.model
1620
const llmProviderType = model2provider(llmModelType)
1721

services/api/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"build": "tsc",
88
"start": "node dist/server.js",
9-
"dev-watch": "tsx watch src/server.ts",
9+
"dev-watch": "tsx atch src/server.ts",
1010
"dev": "tsx src/server.ts"
1111
},
1212
"dependencies": {
@@ -15,11 +15,13 @@
1515
"@cs-magic/wechaty": "0.1.0",
1616
"@fastify/websocket": "^10.0.1",
1717
"@types/lodash": "^4.14.202",
18+
"dotenv": "^16.4.5",
1819
"fastify": "^4.26.2",
1920
"fastify-websocket": "^4.3.0",
2021
"lodash": "^4.17.21"
2122
},
2223
"devDependencies": {
24+
"@types/dotenv": "^8.2.0",
2325
"@types/ws": "^8.5.10",
2426
"typescript": "^5.4.5"
2527
}

services/api/src/server.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// load for api and so on
21
import { genId } from "@cs-magic/common/utils/gen-id"
32
import { logger } from "@cs-magic/log/logger"
43
import { createWechatyBot } from "@cs-magic/wechaty/create-wechaty-bot"
54
import fw from "@fastify/websocket"
5+
import dotenv from "dotenv"
6+
67
import Fastify from "fastify"
78
import remove from "lodash/remove"
8-
import * as process from "process"
9-
import { loadEnv } from "../../../packages/common-env/utils/load-env"
9+
import { Path } from "../../../packages/common-path"
1010
import { IContext } from "./schema/context"
1111
import { handleMessage } from "./utils/handle-message"
1212
import { syncClients } from "./utils/sync-clients"
1313
import { transferMessage } from "./utils/transfer-message"
1414

15-
loadEnv()
15+
dotenv.config({ path: Path.envFile })
1616

1717
logger.info("fastify initializing...")
1818
const fastify = Fastify({

services/wechaty/create-wechaty-bot.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { logger } from "@cs-magic/log/logger"
2+
import dotenv from "dotenv"
23
import qrcodeTerminal from "qrcode-terminal"
34
import { type Wechaty, WechatyBuilder } from "wechaty"
45
import { PuppetPadlocal } from "wechaty-puppet-padlocal"
5-
import { loadEnv } from "../../packages/common-env/utils/load-env"
6+
import { env } from "../../packages/common-env"
67
import { logEnv } from "../../packages/common-env/utils/log-env"
8+
import { Path } from "../../packages/common-path"
79
import { handleMessage } from "./handle-messages/handle-message"
810
import { SenderQueue } from "./handle-messages/sender-queue"
911
import { initBotStaticContext } from "./utils/bot-context"
1012
import { getBotWxid } from "./utils/bot-wxid"
1113

12-
const env = loadEnv()
14+
dotenv.config({ path: Path.envFile })
1315
logEnv("wechaty")
1416

1517
export const createWechatyBot = ({ name }: { name?: string }) => {

0 commit comments

Comments
 (0)