Skip to content

Commit 28ff069

Browse files
Copilotikxin
andauthored
fix: 生产环境环境变量不生效问题 (ikxin#37)
* Initial plan * fix: move MONITOR_LIST/ENABLE_VLMCSD to runtimeConfig, add PORT docs Co-authored-by: ikxin <54543761+ikxin@users.noreply.github.com> * fix: resolve conflicts with main branch (merge MONITOR_INTERVAL support) Co-authored-by: ikxin <54543761+ikxin@users.noreply.github.com> * fix: add MONITOR_INTERVAL to runtimeConfig as monitorInterval (NUXT_MONITOR_INTERVAL) Co-authored-by: ikxin <54543761+ikxin@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ikxin <54543761+ikxin@users.noreply.github.com> Co-authored-by: 一纸忘忧 <i@ikxin.com>
1 parent 701d1b6 commit 28ff069

8 files changed

Lines changed: 68 additions & 23 deletions

File tree

.env.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
NUXT_PUBLIC_API_URL=https://kms.ikxin.com
22
NUXT_PUBLIC_I18N_BASE_URL=https://kms.ikxin.com
3-
ENABLE_VLMCSD=false
4-
MONITOR_INTERVAL=10
3+
NUXT_MONITOR_LIST=kms.org.cn,win.freekms.cn
4+
NUXT_ENABLE_VLMCSD=false
5+
NUXT_MONITOR_INTERVAL=10

README.en.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ The static version uses https://kms.ikxin.com API by default. You can deploy you
7272

7373
### Environment Variables
7474

75-
| Name | Example | Description |
76-
| --------------------- | --------------------------- | --------------------------------------------------- |
77-
| `NUXT_PUBLIC_API_URL` | `https://kms.ikxin.com` | API URL for the static version |
78-
| `MONITOR_LIST` | `kms.org.cn,win.freekms.cn` | Custom KMS server monitoring list, separated by `,` |
79-
| `MONITOR_INTERVAL` | `10` | Monitoring interval in seconds, default is 10 |
80-
| `ENABLE_VLMCSD` | `false` | Whether to enable the built-in VLMCSD service |
75+
| Name | Example | Description |
76+
| -------------------------- | --------------------------- | ---------------------------------------------------------------------- |
77+
| `NUXT_PUBLIC_API_URL` | `https://kms.ikxin.com` | API URL for the static version |
78+
| `NUXT_MONITOR_LIST` | `kms.org.cn,win.freekms.cn` | Custom KMS server monitoring list, separated by `,` |
79+
| `NUXT_MONITOR_INTERVAL` | `10` | Monitoring interval in seconds, default is 10 |
80+
| `NUXT_ENABLE_VLMCSD` | `false` | Whether to enable the built-in VLMCSD service |
81+
| `PORT` | `3000` | Server listening port (also accepts `NITRO_PORT`) |
82+
83+
> [!NOTE]
84+
> The production server (`node .output/server/index.mjs`) does **not** automatically load `.env` files. Environment variables must be set in the system environment or your deployment platform before starting the server. The `.env` file is only used during development and the build phase.
8185
8286
### Full-Stack Version
8387

@@ -96,7 +100,7 @@ services:
96100
volumes:
97101
- kms-data:/app/.data
98102
environment:
99-
- MONITOR_LIST=kms.org.cn,win.freekms.cn
103+
- NUXT_MONITOR_LIST=kms.org.cn,win.freekms.cn
100104
restart: unless-stopped
101105

102106
volumes:
@@ -139,12 +143,18 @@ cd kms-tools && pnpm install
139143
pnpm run build
140144
```
141145

142-
3. Start the service (default port: `3000`)
146+
3. Start the service (default port: `3000`), configurable via the `PORT` environment variable
143147

144148
```bash
145149
node .output/server/index.mjs
146150
```
147151

152+
To set a custom port and monitor list, pass environment variables at startup:
153+
154+
```bash
155+
PORT=3512 NUXT_MONITOR_LIST=kms.example.com node .output/server/index.mjs
156+
```
157+
148158
### Static Version
149159

150160
Most SaaS platforms support static website hosting. Here's the general approach:

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ https://kms.ikxin.com
7272

7373
### 环境变量
7474

75-
| 名称 | 示例值 | 描述 |
76-
| --------------------- | --------------------------- | ---------------------------------------- |
77-
| `NUXT_PUBLIC_API_URL` | `https://kms.ikxin.com` | 静态版本 API 接口地址 |
78-
| `MONITOR_LIST` | `kms.org.cn,win.freekms.cn` | 自定义监控 KMS 服务器列表,使用 `,` 分隔 |
79-
| `MONITOR_INTERVAL` | `10` | 监控频率,单位为秒,默认 10 秒 |
80-
| `ENABLE_VLMCSD` | `false` | 是否启用内置 VLMCSD 服务 |
75+
| 名称 | 示例值 | 描述 |
76+
| -------------------------- | --------------------------- | ----------------------------------------------------------------- |
77+
| `NUXT_PUBLIC_API_URL` | `https://kms.ikxin.com` | 静态版本 API 接口地址 |
78+
| `NUXT_MONITOR_LIST` | `kms.org.cn,win.freekms.cn` | 自定义监控 KMS 服务器列表,使用 `,` 分隔 |
79+
| `NUXT_MONITOR_INTERVAL` | `10` | 监控频率,单位为秒,默认 10 秒 |
80+
| `NUXT_ENABLE_VLMCSD` | `false` | 是否启用内置 VLMCSD 服务 |
81+
| `PORT` | `3000` | 服务监听端口(也可使用 `NITRO_PORT`|
82+
83+
> [!NOTE]
84+
> 生产环境(`node .output/server/index.mjs`)不会自动读取 `.env` 文件,环境变量需要在运行前通过系统环境或部署平台进行配置。`.env` 文件仅在开发和构建阶段有效。
8185
8286
### 全栈版本
8387

@@ -96,7 +100,7 @@ services:
96100
volumes:
97101
- kms-data:/app/.data
98102
environment:
99-
- MONITOR_LIST=kms.org.cn,win.freekms.cn
103+
- NUXT_MONITOR_LIST=kms.org.cn,win.freekms.cn
100104
restart: unless-stopped
101105

102106
volumes:
@@ -139,12 +143,18 @@ cd kms-tools && pnpm install
139143
pnpm run build
140144
```
141145

142-
3. 启动服务,项目默认监听 `3000` 端口
146+
3. 启动服务,项目默认监听 `3000` 端口,可通过 `PORT` 环境变量修改
143147

144148
```bash
145149
node .output/server/index.mjs
146150
```
147151

152+
如需自定义端口和监控列表,可在启动时设置环境变量:
153+
154+
```bash
155+
PORT=3512 NUXT_MONITOR_LIST=kms.example.com node .output/server/index.mjs
156+
```
157+
148158
### 静态版本
149159

150160
几乎所有 SaaS 平台都支持静态网站托管服务,以下是通用的部署方式:

nuxt.config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// https://nuxt.com/docs/api/configuration/nuxt-config
22

33
const getMonitorCron = () => {
4-
const raw = parseInt(process.env.MONITOR_INTERVAL || '10', 10)
4+
const raw = parseInt(
5+
process.env.NUXT_MONITOR_INTERVAL || process.env.MONITOR_INTERVAL || '10',
6+
10,
7+
)
58
const seconds = isNaN(raw) || raw <= 0 ? 10 : raw
69
if (seconds < 60) {
710
return `*/${seconds} * * * * *`
@@ -36,6 +39,9 @@ export default defineNuxtConfig({
3639
},
3740
},
3841
runtimeConfig: {
42+
monitorList: '',
43+
enableVlmcsd: '',
44+
monitorInterval: '10',
3945
public: {
4046
apiUrl: '',
4147
i18n: {

server/api/monitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default defineEventHandler(async () => {
22
const results = await Promise.all(
3-
monitorList.map(async host => {
3+
getMonitorList().map(async host => {
44
let data = await storage.getItem<MonitorData[]>(`${host}.json`)
55

66
if (!Array.isArray(data)) {

server/plugins/vlmcsd.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { arch, platform } from 'os'
44
import { existsSync } from 'fs'
55

66
export default defineNitroPlugin(nitro => {
7-
if (process.env.ENABLE_VLMCSD !== 'true') {
7+
const config = useRuntimeConfig()
8+
const enableVlmcsd =
9+
config.enableVlmcsd === true ||
10+
config.enableVlmcsd === 'true' ||
11+
process.env.ENABLE_VLMCSD === 'true'
12+
13+
if (!enableVlmcsd) {
814
console.log('Vlmcsd is disabled')
915
return
1016
}

server/tasks/monitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineTask({
55
},
66
async run() {
77
const results = await Promise.all(
8-
monitorList.map(async host => {
8+
getMonitorList().map(async host => {
99
let monitorData = await storage.getItem<MonitorData[]>(`${host}.json`)
1010

1111
if (!Array.isArray(monitorData)) {

server/utils/kms.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execFile } from 'child_process'
22
import { arch, platform } from 'os'
33

4-
export const monitorList = process.env.MONITOR_LIST?.split(',') || [
4+
const defaultMonitorList = [
55
'kms.8b5.cn',
66
'kms.org.cn',
77
'win.freekms.cn',
@@ -22,6 +22,18 @@ export const monitorList = process.env.MONITOR_LIST?.split(',') || [
2222
'kms.digiboy.ir',
2323
]
2424

25+
export const getMonitorList = (() => {
26+
let cached: string[] | undefined
27+
return () => {
28+
if (cached) return cached
29+
const config = useRuntimeConfig()
30+
const listStr =
31+
(config.monitorList as string) || process.env.MONITOR_LIST
32+
cached = listStr?.split(',').filter(Boolean) || defaultMonitorList
33+
return cached
34+
}
35+
})()
36+
2537
export const runVlmcs = ({
2638
host,
2739
port = 1688,

0 commit comments

Comments
 (0)