ddddddd / entrypoint.sh
wsj1995's picture
Upload 3 files
7566883 verified
#!/bin/sh
set -e
if [ -z "$MIDDLEWARE_ZIP_URL" ]; then
echo "error: MIDDLEWARE_ZIP_URL is required" >&2
exit 1
fi
EXTRACT_ROOT="${MIDDLEWARE_EXTRACT_DIR:-/opt/middleware}"
TMP_ZIP="/tmp/middleware-bundle.zip"
rm -rf "${EXTRACT_ROOT:?}"
mkdir -p "$EXTRACT_ROOT"
echo "downloading middleware zip..."
curl -fsSL "$MIDDLEWARE_ZIP_URL" -o "$TMP_ZIP"
echo "extracting..."
unzip -oq "$TMP_ZIP" -d "$EXTRACT_ROOT"
rm -f "$TMP_ZIP"
find_app_root() {
base="$1"
if [ -f "$base/requirements.txt" ] && [ -d "$base/app" ]; then
echo "$base"
return 0
fi
# 常见:zip 根目录为 middleware/(与 MIDDLEWARE_EXTRACT_DIR 名无关)
mw="$base/middleware"
if [ -d "$mw" ] && [ -f "$mw/requirements.txt" ] && [ -d "$mw/app" ]; then
echo "$mw"
return 0
fi
count=0
child=""
for d in "$base"/*; do
[ -d "$d" ] || continue
count=$((count + 1))
child="$d"
done
if [ "$count" -eq 1 ] && [ -f "$child/requirements.txt" ] && [ -d "$child/app" ]; then
echo "$child"
return 0
fi
echo "error: could not locate app/ and requirements.txt under $base" >&2
exit 1
}
APP_ROOT=$(find_app_root "$EXTRACT_ROOT")
export PYTHONPATH="$APP_ROOT"
cd "$APP_ROOT"
echo "installing dependencies..."
pip install --no-cache-dir -r requirements.txt
echo "starting uvicorn..."
exec uvicorn app.main:app --host 0.0.0.0 --port "${PORT}"