Files
skipredictor/start.sh
T
dasemu 9d97afe538 feat(api): add FastAPI backend with JWT authentication
REST API serving forecast data with HS256 JWT auth (14-day tokens).
Public: /health, /auth/register, /auth/login.
Protected: /stations, /forecast, /forecast/history, /forecast/{id}, /status.
Includes APScheduler for periodic forecasts and Telegram bot subprocess.
2026-03-13 09:36:18 +01:00

142 lines
4.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Ski Predictor - Setup and start script
# Usage: ./start.sh [--bot] [--cron] [--collect-now]
#
# --bot Start the Telegram bot (requires TELEGRAM_BOT_TOKEN)
# --cron Install the daily cron job (22:00)
# --collect-now Run the data collection pipeline immediately
#
# First run: ./start.sh --cron --collect-now
# With bot: ./start.sh --cron --collect-now --bot
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VENV="$SCRIPT_DIR/.venv"
PYTHON="$VENV/bin/python"
DATA_DIR="$SCRIPT_DIR/data"
ENV_FILE="$SCRIPT_DIR/.env"
# ---------------------------------------------------------------
# Colors
# ---------------------------------------------------------------
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }
# ---------------------------------------------------------------
# 1. Create virtualenv and install dependencies
# ---------------------------------------------------------------
if [ ! -d "$VENV" ]; then
info "Creating virtualenv..."
python3 -m venv "$VENV"
fi
info "Installing dependencies..."
"$VENV/bin/pip" install -q -r "$SCRIPT_DIR/requirements.txt"
"$VENV/bin/pip" install -q pytest
# ---------------------------------------------------------------
# 2. Create data directories
# ---------------------------------------------------------------
mkdir -p "$DATA_DIR/raw" "$DATA_DIR/processed" "$SCRIPT_DIR/ml/models"
# ---------------------------------------------------------------
# 3. Parse arguments
# ---------------------------------------------------------------
DO_BOT=false
DO_CRON=false
DO_COLLECT=false
for arg in "$@"; do
case "$arg" in
--bot) DO_BOT=true ;;
--cron) DO_CRON=true ;;
--collect-now) DO_COLLECT=true ;;
*) error "Unknown argument: $arg"; exit 1 ;;
esac
done
# ---------------------------------------------------------------
# 3.5 Load environment from .env when present
# ---------------------------------------------------------------
if [ -f "$ENV_FILE" ]; then
info "Loading environment from $ENV_FILE"
set -a
# shellcheck disable=SC1090
. "$ENV_FILE"
set +a
fi
# ---------------------------------------------------------------
# 4. Install cron job
# ---------------------------------------------------------------
if [ "$DO_CRON" = true ]; then
CRON_CMD="0 22 * * * \"$PYTHON\" \"$SCRIPT_DIR/scripts/collect_daily_data.py\" >> \"$DATA_DIR/pipeline.log\" 2>&1"
if crontab -l 2>/dev/null | grep -qF "collect_daily_data.py"; then
warn "Cron job already exists, skipping."
else
(crontab -l 2>/dev/null; echo "$CRON_CMD") | crontab -
info "Cron job installed (daily at 22:00)."
fi
fi
# ---------------------------------------------------------------
# 5. Run initial data collection
# ---------------------------------------------------------------
if [ "$DO_COLLECT" = true ]; then
info "Running initial data collection..."
"$PYTHON" "$SCRIPT_DIR/scripts/collect_daily_data.py" 2>&1 | tee -a "$DATA_DIR/pipeline.log"
info "Data collection complete."
fi
# ---------------------------------------------------------------
# 6. Start FastAPI backend (includes scheduler + bot subprocess)
# ---------------------------------------------------------------
if [ "$DO_BOT" = true ]; then
if [ -z "${TELEGRAM_BOT_TOKEN:-}" ]; then
error "TELEGRAM_BOT_TOKEN not set. Export it first:"
echo " export TELEGRAM_BOT_TOKEN='your_token_here'"
exit 1
fi
# Kill existing processes if running
if pgrep -f "uvicorn app.main:app|bot/telegram_bot.py|bot\\.telegram_bot" > /dev/null 2>&1; then
warn "Killing existing processes..."
pkill -f "uvicorn app.main:app|bot/telegram_bot.py|bot\\.telegram_bot" || true
sleep 1
fi
info "Starting FastAPI backend (scheduler + Telegram bot)..."
: > "$DATA_DIR/bot.log"
nohup env PYTHONPATH="$SCRIPT_DIR" "$VENV/bin/uvicorn" app.main:app \
--host 0.0.0.0 --port 8000 \
</dev/null >> "$DATA_DIR/bot.log" 2>&1 &
SERVER_PID=$!
info "Backend started (PID: $SERVER_PID). Logs: $DATA_DIR/bot.log"
fi
# ---------------------------------------------------------------
# Summary
# ---------------------------------------------------------------
echo ""
info "=== Ski Predictor ==="
echo " Project: $SCRIPT_DIR"
echo " Python: $PYTHON"
echo " Data: $DATA_DIR"
if [ "$DO_CRON" = true ]; then
echo " Cron: Daily at 22:00"
fi
if [ "$DO_BOT" = true ]; then
echo " Server PID: $SERVER_PID"
echo " API: http://0.0.0.0:8000/api/v1"
fi
echo ""
info "Done."