139 lines
2.8 KiB
Bash
Executable File
139 lines
2.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m'
|
|
|
|
info() { echo -e "${GREEN}[+]${NC} $*"; }
|
|
die() { echo -e "${RED}[x] FATAL:${NC} $*" >&2; exit 1; }
|
|
|
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
BACKEND_DIR="$ROOT_DIR/backend"
|
|
ENV_FILE="$BACKEND_DIR/.env"
|
|
|
|
require_command() {
|
|
command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
|
|
}
|
|
|
|
require_env() {
|
|
[[ -f "$ENV_FILE" ]] || die "Missing $ENV_FILE. Run ./setup.sh first."
|
|
}
|
|
|
|
env_value() {
|
|
local key="$1"
|
|
local default_value="${2:-}"
|
|
local value
|
|
value=$(grep -E "^${key}=" "$ENV_FILE" | cut -d '=' -f2- || true)
|
|
if [[ -n "$value" ]]; then
|
|
printf '%s\n' "$value"
|
|
return
|
|
fi
|
|
printf '%s\n' "$default_value"
|
|
}
|
|
|
|
compose() {
|
|
docker compose --env-file "$ENV_FILE" -f "$ROOT_DIR/docker-compose.yml" "$@"
|
|
}
|
|
|
|
run_backend() {
|
|
require_command uv
|
|
require_env
|
|
local api_port
|
|
api_port=$(env_value API_PORT 3671)
|
|
info "Starting backend on http://0.0.0.0:$api_port"
|
|
(
|
|
cd "$BACKEND_DIR"
|
|
uv run fastapi run app/main.py --host 0.0.0.0 --port "$api_port"
|
|
)
|
|
}
|
|
|
|
run_frontend() {
|
|
require_command docker
|
|
require_env
|
|
local action="${1:-up}"
|
|
case "$action" in
|
|
up)
|
|
compose up -d frontend
|
|
info "Frontend container running on http://localhost:$(env_value FRONTEND_PORT 5173)"
|
|
;;
|
|
down)
|
|
compose stop frontend
|
|
;;
|
|
status)
|
|
compose ps frontend
|
|
;;
|
|
logs)
|
|
compose logs -f frontend
|
|
;;
|
|
*)
|
|
die "Unknown frontend action: $action"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
run_mongo() {
|
|
require_command docker
|
|
require_env
|
|
local action="${1:-status}"
|
|
case "$action" in
|
|
up)
|
|
compose up -d mongodb
|
|
info "MongoDB running on mongodb://localhost:27017"
|
|
;;
|
|
down)
|
|
compose stop mongodb
|
|
;;
|
|
status)
|
|
compose ps mongodb
|
|
;;
|
|
logs)
|
|
compose logs -f mongodb
|
|
;;
|
|
*)
|
|
die "Unknown mongo action: $action"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
show_help() {
|
|
cat <<'EOF'
|
|
Usage:
|
|
./run.sh backend
|
|
./run.sh frontend [up|down|status|logs]
|
|
./run.sh mongo [up|down|status|logs]
|
|
|
|
Examples:
|
|
./run.sh mongo up
|
|
./run.sh backend
|
|
./run.sh frontend up
|
|
./run.sh frontend logs
|
|
EOF
|
|
}
|
|
|
|
main() {
|
|
local command="${1:-help}"
|
|
shift || true
|
|
|
|
case "$command" in
|
|
backend)
|
|
run_backend "$@"
|
|
;;
|
|
frontend)
|
|
run_frontend "$@"
|
|
;;
|
|
mongo)
|
|
run_mongo "$@"
|
|
;;
|
|
help|-h|--help)
|
|
show_help
|
|
;;
|
|
*)
|
|
die "Unknown command: $command"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
main "$@"
|