#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BACKEND_DIR="${ROOT_DIR}/backend" FRONTEND_DIR="${ROOT_DIR}/frontend" BACKEND_VENV="${BACKEND_DIR}/.venv" PYTHON_BIN="${PYTHON_BIN:-python3}" ENV_FILE="${BACKEND_DIR}/.env" ENV_EXAMPLE_FILE="${BACKEND_DIR}/.env.dev.example" BACKEND_LOG="/tmp/routeros-next-backend.log" cleanup() { if [[ -n "${BACKEND_PID:-}" ]] && kill -0 "${BACKEND_PID}" 2>/dev/null; then kill "${BACKEND_PID}" 2>/dev/null || true fi } trap cleanup EXIT INT TERM read_env_value() { local key="$1" local file="$2" if [[ -f "$file" ]]; then local line line=$(grep -E "^${key}=" "$file" | tail -n 1 || true) if [[ -n "$line" ]]; then echo "${line#*=}" return 0 fi fi return 1 } if [[ ! -d "${BACKEND_VENV}" ]]; then "${PYTHON_BIN}" -m venv "${BACKEND_VENV}" fi # shellcheck disable=SC1091 source "${BACKEND_VENV}/bin/activate" python -m pip install --upgrade pip python -m pip install -r "${BACKEND_DIR}/requirements.txt" if [[ ! -f "${ENV_FILE}" ]] && [[ -f "${ENV_EXAMPLE_FILE}" ]]; then cp "${ENV_EXAMPLE_FILE}" "${ENV_FILE}" fi DEFAULT_ADMIN_USERNAME="$(read_env_value DEFAULT_ADMIN_USERNAME "${ENV_FILE}" || read_env_value DEFAULT_ADMIN_USERNAME "${ENV_EXAMPLE_FILE}" || echo admin)" DEFAULT_ADMIN_PASSWORD="$(read_env_value DEFAULT_ADMIN_PASSWORD "${ENV_FILE}" || read_env_value DEFAULT_ADMIN_PASSWORD "${ENV_EXAMPLE_FILE}" || echo admin)" cd "${BACKEND_DIR}" PYTHONPATH="${BACKEND_DIR}" uvicorn app.main:app --reload --reload-dir app --host 127.0.0.1 --port 8000 > "${BACKEND_LOG}" 2>&1 & BACKEND_PID=$! for _ in $(seq 1 30); do if python - <<'PY' import sys from urllib.request import urlopen try: with urlopen('http://127.0.0.1:8000/api/health', timeout=1) as response: sys.exit(0 if response.status == 200 else 1) except Exception: sys.exit(1) PY then break fi sleep 1 done if ! python - <<'PY' import sys from urllib.request import urlopen try: with urlopen('http://127.0.0.1:8000/api/health', timeout=1) as response: sys.exit(0 if response.status == 200 else 1) except Exception: sys.exit(1) PY then echo "Backend failed to become ready. Log: ${BACKEND_LOG}" tail -n 100 "${BACKEND_LOG}" || true exit 1 fi echo "Backend: http://127.0.0.1:8000" echo "API docs: http://127.0.0.1:8000/docs" echo "Frontend: http://127.0.0.1:4200" echo "Default login: ${DEFAULT_ADMIN_USERNAME} / ${DEFAULT_ADMIN_PASSWORD}" cd "${FRONTEND_DIR}" if [[ ! -d node_modules ]]; then npm install fi npm run start