78 lines
2.3 KiB
Bash
Executable File
78 lines
2.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
OUT="docs/API/openapi.snapshot.yaml"
|
|
PORT="${PORT:-8080}"
|
|
PROFILE="${PROFILE:-test}"
|
|
MAX_WAIT="${MAX_WAIT:-90}"
|
|
|
|
echo "[openapi] export to ${OUT}, port=${PORT}, profile=${PROFILE}"
|
|
|
|
# 1) 编译(如果 jar 不存在)
|
|
JAR="$(find ljwx-platform-app/target -name '*.jar' -not -name '*-sources*' -not -name '*-javadoc*' 2>/dev/null | head -n1 || true)"
|
|
if [[ -z "${JAR}" ]]; then
|
|
echo "[openapi] jar not found, building..."
|
|
./mvnw -q -DskipTests package
|
|
JAR="$(find ljwx-platform-app/target -name '*.jar' -not -name '*-sources*' -not -name '*-javadoc*' | head -n1)"
|
|
fi
|
|
|
|
echo "[openapi] using jar: ${JAR}"
|
|
|
|
# 2) 后台启动应用
|
|
nohup java -jar "${JAR}" \
|
|
--spring.profiles.active="${PROFILE}" \
|
|
--server.port="${PORT}" \
|
|
> /tmp/openapi-app.log 2>&1 &
|
|
APP_PID=$!
|
|
|
|
cleanup() {
|
|
echo "[openapi] stopping app pid=${APP_PID}"
|
|
kill "${APP_PID}" 2>/dev/null || true
|
|
wait "${APP_PID}" 2>/dev/null || true
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
# 3) 等待应用就绪(先试 actuator,再试 OpenAPI 端点自身)
|
|
echo "[openapi] waiting for app to start (max ${MAX_WAIT}s)..."
|
|
READY=0
|
|
for i in $(seq 1 "${MAX_WAIT}"); do
|
|
# 优先 actuator
|
|
if curl -fsS "http://127.0.0.1:${PORT}/actuator/health" >/dev/null 2>&1; then
|
|
READY=1
|
|
break
|
|
fi
|
|
# 回退:直接试 OpenAPI 端点
|
|
if curl -fsS "http://127.0.0.1:${PORT}/v3/api-docs" >/dev/null 2>&1; then
|
|
READY=1
|
|
break
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
if [[ "${READY}" -eq 0 ]]; then
|
|
echo "[openapi] ERROR: app did not start within ${MAX_WAIT}s"
|
|
echo "[openapi] last 50 lines of log:"
|
|
tail -50 /tmp/openapi-app.log
|
|
exit 1
|
|
fi
|
|
|
|
echo "[openapi] app is ready"
|
|
|
|
# 4) 导出 OpenAPI
|
|
mkdir -p "$(dirname "${OUT}")"
|
|
|
|
if curl -fsS "http://127.0.0.1:${PORT}/v3/api-docs.yaml" -o "${OUT}" 2>/dev/null; then
|
|
echo "[openapi] exported yaml to ${OUT}"
|
|
elif curl -fsS "http://127.0.0.1:${PORT}/v3/api-docs" -o "${OUT%.yaml}.json" 2>/dev/null; then
|
|
echo "[openapi] yaml not available, exported json to ${OUT%.yaml}.json"
|
|
else
|
|
echo "[openapi] WARNING: could not export OpenAPI. Endpoint may not be configured."
|
|
echo "[openapi] This is non-fatal. Skipping snapshot update."
|
|
# 创建一个占位文件,避免后续 git diff 报错
|
|
if [[ ! -f "${OUT}" ]]; then
|
|
echo "# OpenAPI snapshot not yet available" > "${OUT}"
|
|
fi
|
|
fi
|
|
|
|
echo "[openapi] done"
|