unis-oa/backend/scripts/prod-api.sh

42 lines
2.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env bash
# 生产 / 正式机启动入口:默认读生产档配置文件(config.toml + config.prod.toml)。
#
# ./scripts/prod-api.sh 用 backend/config.prod.toml
# OA_CONFIG_DIR=/etc/unisoa ./scripts/prod-api.sh 配置文件放在 /etc/unisoa 下
# OA_CONFIG=/etc/unisoa/x.toml ./scripts/prod-api.sh 只认这一个文件(老用法)
# OA_ENV=staging ./scripts/prod-api.sh 预发档:config.staging.toml
#
# 分档规矩见 app/config.py 顶部注释与 README「配置项」一节;systemd / docker 里
# 不用这个脚本也行,只要给进程一个 OA_ENV=prod。
set -euo pipefail
cd "$(dirname "$0")/.."
export OA_ENV="${OA_ENV:-prod}"
# 没指定单一文件时先确认这一档的配置文件真的在,别起来了才发现连的是默认库
if [ -z "${OA_CONFIG:-}" ]; then
dir="${OA_CONFIG_DIR:-$PWD}"
if [ ! -f "$dir/config.$OA_ENV.toml" ]; then
echo "找不到 $dir/config.$OA_ENV.toml:先 cp config.prod.toml.example config.prod.toml 再填真实值" >&2
echo "(确实只想要一个文件的话,用 OA_CONFIG=/path/to/x.toml 启动)" >&2
exit 1
fi
fi
# 起服务之前先把"这次读的到底是哪几份文件"打在日志最前面(发版翻日志就靠这行)
# 用 sed 而不是 head:head 读满 3 行就关管道,脚本 set -o pipefail 下会被 SIGPIPE 打断
python3 scripts/what_config.py | sed -n '1,3p' || true
# OA_STRICT_CONFIG=1:生产档体检没过就不起服务,留给上线流程当闸门(默认不拦)
if [ "${OA_STRICT_CONFIG:-0}" = "1" ]; then
# 没过就把体检那几行原样打到 stderr 再退出:容器里日志就只有这些,
# 光说"没过"等于让人再去跑一遍自查才知道改哪
if ! strict=$(python3 scripts/what_config.py --check 2>&1); then
printf '%s\n' "$strict" | awk '/^上线体检/{f=1} f' >&2
echo "上线体检没过(OA_STRICT_CONFIG=1),改好 config.$OA_ENV.toml 再起服务" >&2
exit 1
fi
fi
echo "UniSOA API 启动中:OA_ENV=$OA_ENV"
exec python3 -m uvicorn app.main:app --host "${OA_HOST:-0.0.0.0}" --port "${OA_PORT:-8000}"