Skip to content

Commit 30de96d

Browse files
author
“bingtao”
committed
update
1 parent 4dc4736 commit 30de96d

1 file changed

Lines changed: 46 additions & 14 deletions

File tree

scripts/services/new-api/setup.sh

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#
1414
# 环境变量:
1515
# NEW_API_SERVICE_HOME 安装根(默认 ~/opt/new-api),内含 bin/new-api、data/(SQLite 等工作目录)
16-
# NEW_API_DATA_DIR 运行时的 cwd(默认 ${NEW_API_SERVICE_HOME}/data),库文件等写在此目录
17-
# NEW_API_PORT / PORT 监听端口(启动时 export PORT;默认 8801,可覆盖)
16+
# NEW_API_DATA_DIR 运行时的 cwd(默认 ${NEW_API_SERVICE_HOME}/data),库文件等写在此目录;.env 放此目录
17+
# NEW_API_PORT / PORT 无 .env 时:启动前注入 PORT(默认 8801)。若存在 ${NEW_API_DATA_DIR}/.env,默认不注入
18+
# PORT,由程序内 godotenv 从 .env 读取(避免 shell 已设变量覆盖 .env)。
19+
# NEW_API_FORCE_SCRIPT_PORT=1 即使存在 .env 也强制使用 NEW_API_PORT(等价于以前行为)
1820
# NEW_API_VERSION 强制版本,如 0.12.6 或 v0.12.6(不设则从 Releases 解析)
1921
# NEW_API_GITHUB_REPO owner/repo(默认 QuantumNous/new-api)
2022
# NONINTERACTIVE=1
@@ -65,8 +67,8 @@ usage() {
6567
6668
命令:
6769
install / update 从 GitHub Releases 下载预编译二进制到 ${NEW_API_BIN}
68-
start 后台启动(cwd ${NEW_API_DATA_DIR}PORT=${NEW_API_PORT}日志 ${LOG_FILE}
69-
run 前台启动(与 start 相同 cwd 与 PORT;终端附着;不写 PID;后台已在跑时拒绝)
70+
start 后台启动(cwd ${NEW_API_DATA_DIR};无 .env 时注入 PORT=${NEW_API_PORT};有 .env 时由程序读 .env;日志 ${LOG_FILE}
71+
run 前台启动(与 start 相同 cwd;终端附着;不写 PID;后台已在跑时拒绝)
7072
stop / restart / status
7173
uninstall 停止并删除 ${NEW_API_SERVICE_HOME}
7274
@@ -81,6 +83,19 @@ ensure_dirs() {
8183

8284
die() { echo "错误: $*" >&2; exit 1; }
8385

86+
# new-api 使用 godotenv.Load(".env"):默认不覆盖已在环境中的变量。本脚本若先 env PORT=… 会导致 .env 不生效。
87+
# 返回用于探测 HTTP 的端口(优先 .env 中 PORT,否则 NEW_API_PORT)。
88+
_new_api_effective_port() {
89+
local p="${NEW_API_PORT}"
90+
if [[ -f "${NEW_API_DATA_DIR}/.env" ]]; then
91+
local raw
92+
raw="$(grep -E '^[[:space:]]*PORT[[:space:]]*=' "${NEW_API_DATA_DIR}/.env" 2>/dev/null | tail -1 || true)"
93+
raw="$(printf '%s' "$raw" | sed -E 's/^[[:space:]]*PORT[[:space:]]*=[[:space:]]*//;s/#.*$//;s/^["'\'']//;s/["'\'']$//;s/[[:space:]]*$//')"
94+
[[ -n "$raw" ]] && p="$raw"
95+
fi
96+
printf '%s' "$p"
97+
}
98+
8499
process_alive() {
85100
kill -0 "$1" 2>/dev/null
86101
}
@@ -239,17 +254,27 @@ cmd_start() {
239254
exit 1
240255
fi
241256
rm -f "$PID_FILE"
242-
echo "==> 启动 new-api,PORT=${NEW_API_PORT},cwd ${NEW_API_DATA_DIR},日志 ${LOG_FILE}" >&2
257+
echo "==> 启动 new-api,cwd ${NEW_API_DATA_DIR},日志 ${LOG_FILE}" >&2
258+
if [[ -f "${NEW_API_DATA_DIR}/.env" ]] && [[ "${NEW_API_FORCE_SCRIPT_PORT:-}" != "1" ]]; then
259+
echo " 检测到 .env:不注入 PORT,由程序加载 ${NEW_API_DATA_DIR}/.env(需覆盖请设 NEW_API_FORCE_SCRIPT_PORT=1)" >&2
260+
else
261+
echo " PORT=${NEW_API_PORT}(注入进程环境)" >&2
262+
fi
243263
pushd "${NEW_API_DATA_DIR}" >/dev/null
244-
# 上游 main 优先读环境变量 PORT
245-
nohup env PORT="${NEW_API_PORT}" "${NEW_API_BIN}" >>"${LOG_FILE}" 2>&1 &
264+
# 上游 main:先 godotenv.Load(".env"),且默认不覆盖已存在环境变量;故存在 .env 时不预先 export PORT
265+
if [[ -f "${NEW_API_DATA_DIR}/.env" ]] && [[ "${NEW_API_FORCE_SCRIPT_PORT:-}" != "1" ]]; then
266+
# 清掉继承的 PORT,避免 shell 已 export 的 PORT 阻止 godotenv 写入
267+
nohup env -u PORT "${NEW_API_BIN}" >>"${LOG_FILE}" 2>&1 &
268+
else
269+
nohup env PORT="${NEW_API_PORT}" "${NEW_API_BIN}" >>"${LOG_FILE}" 2>&1 &
270+
fi
246271
local cpid=$!
247272
echo "$cpid" >"$PID_FILE"
248273
popd >/dev/null
249274
sleep 1
250275
existing="$(read_pid)"
251276
if [[ -n "$existing" ]] && process_alive "$existing"; then
252-
echo "已启动 PID ${existing}默认 http://127.0.0.1:${NEW_API_PORT}/ )"
277+
echo "已启动 PID ${existing}探测 http://127.0.0.1:$(_new_api_effective_port)/ )"
253278
else
254279
echo "警告: 进程可能已退出,请查看: tail -80 ${LOG_FILE}" >&2
255280
fi
@@ -264,9 +289,15 @@ cmd_run() {
264289
echo "new-api 已在后台运行(PID ${existing})。请先 $0 stop,再使用 run。" >&2
265290
exit 1
266291
fi
267-
echo "==> 前台启动 new-api,PORT=${NEW_API_PORT},cwd ${NEW_API_DATA_DIR}(Ctrl+C 退出;不写 PID)" >&2
268-
pushd "${NEW_API_DATA_DIR}" >/dev/null
269-
exec env PORT="${NEW_API_PORT}" "${NEW_API_BIN}"
292+
if [[ -f "${NEW_API_DATA_DIR}/.env" ]] && [[ "${NEW_API_FORCE_SCRIPT_PORT:-}" != "1" ]]; then
293+
echo "==> 前台启动 new-api,cwd ${NEW_API_DATA_DIR}(使用 .env;Ctrl+C 退出;不写 PID)" >&2
294+
pushd "${NEW_API_DATA_DIR}" >/dev/null
295+
exec env -u PORT "${NEW_API_BIN}"
296+
else
297+
echo "==> 前台启动 new-api,PORT=${NEW_API_PORT},cwd ${NEW_API_DATA_DIR}(Ctrl+C 退出;不写 PID)" >&2
298+
pushd "${NEW_API_DATA_DIR}" >/dev/null
299+
exec env PORT="${NEW_API_PORT}" "${NEW_API_BIN}"
300+
fi
270301
}
271302

272303
cmd_stop() {
@@ -305,7 +336,8 @@ cmd_status() {
305336
pid="$(read_pid)"
306337
echo "NEW_API_SERVICE_HOME=${NEW_API_SERVICE_HOME}"
307338
echo "NEW_API_DATA_DIR=${NEW_API_DATA_DIR}"
308-
echo "NEW_API_PORT=${NEW_API_PORT}"
339+
echo "NEW_API_PORT=${NEW_API_PORT}(无 .env 或未解析时用;实际监听见下)"
340+
echo "探测端口(.env 优先): $(_new_api_effective_port)"
309341
if [[ -n "$pid" ]] && process_alive "$pid"; then
310342
echo "状态: 运行中 PID ${pid}"
311343
else
@@ -317,8 +349,8 @@ cmd_status() {
317349
fi
318350
if command -v curl >/dev/null 2>&1; then
319351
echo ""
320-
echo "==> 探测 http://127.0.0.1:${NEW_API_PORT}/"
321-
curl -sS -m 3 -o /dev/null -w "HTTP %{http_code}\n" "http://127.0.0.1:${NEW_API_PORT}/" || echo "(无法连接)"
352+
echo "==> 探测 http://127.0.0.1:$(_new_api_effective_port)/"
353+
curl -sS -m 3 -o /dev/null -w "HTTP %{http_code}\n" "http://127.0.0.1:$(_new_api_effective_port)/" || echo "(无法连接)"
322354
fi
323355
}
324356

0 commit comments

Comments
 (0)