deerflow-code/scripts/start-backend.sh
2026-09-07 18:24:55 +08:00

69 lines
2.3 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/env.sh"
PID_FILE="${PID_DIR}/backend.pid"
LOG_FILE="${LOG_DIR}/backend.log"
BACKEND_VENDOR_DIR="${BACKEND_VENDOR_DIR:-${BACKEND_DIR}/.deer-flow/python-vendor}"
BACKEND_WHEELHOUSE="${BACKEND_WHEELHOUSE:-${BACKEND_DIR}/wheelhouse}"
BACKEND_INSTALL_DEPS="${BACKEND_INSTALL_DEPS:-1}"
BACKEND_PIP_PACKAGES="${BACKEND_PIP_PACKAGES:-croniter==6.2.2 langgraph-checkpoint-postgres==3.0.5 psycopg==3.3.3 psycopg-binary==3.3.3 psycopg-pool==3.3.0 orjson==3.11.9 tzdata==2026.2}"
if is_running "${PID_FILE}"; then
echo "Backend is already running (pid $(cat "${PID_FILE}"))."
echo "URL: http://${HOST_IP}:${BACKEND_PORT}"
exit 0
fi
rm -f "${PID_FILE}"
echo "Starting backend..."
echo "Log: ${LOG_FILE}"
mkdir -p "${BACKEND_VENDOR_DIR}"
if [[ "${BACKEND_INSTALL_DEPS}" != "0" ]]; then
if ! (
cd "${BACKEND_DIR}" && \
PYTHONPATH=".:${BACKEND_VENDOR_DIR}${PYTHONPATH:+:${PYTHONPATH}}" \
uv run --no-sync python -c "import croniter; import psycopg; import psycopg_pool; import langgraph.checkpoint.postgres; import langgraph.store.postgres"
) >/dev/null 2>&1; then
if [[ ! -d "${BACKEND_WHEELHOUSE}" ]]; then
echo "ERROR: wheelhouse not found: ${BACKEND_WHEELHOUSE}" >&2
exit 1
fi
echo "Installing backend runtime deps into ${BACKEND_VENDOR_DIR}"
(
cd "${BACKEND_DIR}" && \
uv pip install \
--target "${BACKEND_VENDOR_DIR}" \
--no-deps \
--no-index \
--find-links "${BACKEND_WHEELHOUSE}" \
${BACKEND_PIP_PACKAGES}
)
fi
fi
# nohup instead of setsid: setsid does not exist on Windows Git Bash.
nohup bash -c '
cd "$0" || exit 1
exec env \
GATEWAY_CORS_ORIGINS="$1" \
PYTHONPATH=".:$4${PYTHONPATH:+:${PYTHONPATH}}" \
uv run uvicorn app.gateway.app:app \
--host "$2" \
--port "$3" \
--reload \
--reload-dir "app" \
--reload-dir "packages" \
--reload-exclude "$0/.deer-flow" \
--reload-exclude "$0/.runtime"
' "${BACKEND_DIR}" "${GATEWAY_CORS_ORIGINS}" "${BACKEND_HOST}" "${BACKEND_PORT}" "${BACKEND_VENDOR_DIR}" \
>"${LOG_FILE}" 2>&1 &
echo "$!" >"${PID_FILE}"
echo "Backend started (pid $(cat "${PID_FILE}"))."
echo "URL: http://${HOST_IP}:${BACKEND_PORT}"