322 lines
7.7 KiB
Bash
Executable File
322 lines
7.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# STUPA PDF API Development Helper Script
|
|
# This script provides convenient commands for development tasks
|
|
|
|
set -e
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Project directories
|
|
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
BACKEND_DIR="${PROJECT_ROOT}/backend"
|
|
FRONTEND_DIR="${PROJECT_ROOT}/frontend"
|
|
|
|
# Function to display usage
|
|
usage() {
|
|
echo -e "${GREEN}STUPA PDF API Development Helper${NC}"
|
|
echo "================================="
|
|
echo ""
|
|
echo "Usage: $0 [command] [options]"
|
|
echo ""
|
|
echo "Commands:"
|
|
echo " start Start all services"
|
|
echo " stop Stop all services"
|
|
echo " restart Restart all services"
|
|
echo " status Show status of all services"
|
|
echo " logs [service] Show logs (optionally for specific service)"
|
|
echo " build [service] Build services (optionally specific service)"
|
|
echo " shell [service] Open shell in service container"
|
|
echo " db Open MySQL client"
|
|
echo " migrate Run database migrations"
|
|
echo " reset Reset everything (WARNING: deletes data)"
|
|
echo " test Run tests"
|
|
echo " lint Run linters"
|
|
echo " format Format code"
|
|
echo " clean Clean up temporary files and caches"
|
|
echo ""
|
|
echo "Services: api, frontend, db, adminer"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " $0 start # Start all services"
|
|
echo " $0 logs api # Show API logs"
|
|
echo " $0 shell frontend # Open shell in frontend container"
|
|
echo " $0 db # Connect to database"
|
|
}
|
|
|
|
# Check if docker compose is available
|
|
check_docker() {
|
|
if ! command -v docker &> /dev/null; then
|
|
echo -e "${RED}Docker is not installed${NC}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Start services
|
|
start_services() {
|
|
echo -e "${GREEN}Starting all services...${NC}"
|
|
cd "${PROJECT_ROOT}"
|
|
docker compose up -d
|
|
echo -e "${GREEN}Services started!${NC}"
|
|
echo ""
|
|
echo "Access points:"
|
|
echo " Frontend: http://localhost:3000"
|
|
echo " API: http://localhost:8000"
|
|
echo " API Docs: http://localhost:8000/docs"
|
|
echo " Adminer: http://localhost:8080"
|
|
}
|
|
|
|
# Stop services
|
|
stop_services() {
|
|
echo -e "${YELLOW}Stopping all services...${NC}"
|
|
cd "${PROJECT_ROOT}"
|
|
docker compose down
|
|
echo -e "${GREEN}Services stopped!${NC}"
|
|
}
|
|
|
|
# Restart services
|
|
restart_services() {
|
|
echo -e "${YELLOW}Restarting all services...${NC}"
|
|
cd "${PROJECT_ROOT}"
|
|
docker compose restart
|
|
echo -e "${GREEN}Services restarted!${NC}"
|
|
}
|
|
|
|
# Show status
|
|
show_status() {
|
|
echo -e "${BLUE}Service Status:${NC}"
|
|
cd "${PROJECT_ROOT}"
|
|
docker compose ps
|
|
}
|
|
|
|
# Show logs
|
|
show_logs() {
|
|
cd "${PROJECT_ROOT}"
|
|
if [ -z "$1" ]; then
|
|
docker compose logs -f
|
|
else
|
|
docker compose logs -f "$1"
|
|
fi
|
|
}
|
|
|
|
# Build services
|
|
build_services() {
|
|
cd "${PROJECT_ROOT}"
|
|
if [ -z "$1" ]; then
|
|
echo -e "${GREEN}Building all services...${NC}"
|
|
docker compose build
|
|
else
|
|
echo -e "${GREEN}Building $1...${NC}"
|
|
docker compose build "$1"
|
|
fi
|
|
echo -e "${GREEN}Build complete!${NC}"
|
|
}
|
|
|
|
# Open shell in container
|
|
open_shell() {
|
|
if [ -z "$1" ]; then
|
|
echo -e "${RED}Please specify a service${NC}"
|
|
echo "Available services: api, frontend, db, adminer"
|
|
exit 1
|
|
fi
|
|
|
|
cd "${PROJECT_ROOT}"
|
|
case "$1" in
|
|
api)
|
|
docker compose exec api /bin/bash
|
|
;;
|
|
frontend)
|
|
docker compose exec frontend /bin/sh
|
|
;;
|
|
db)
|
|
docker compose exec db /bin/bash
|
|
;;
|
|
adminer)
|
|
docker compose exec adminer /bin/sh
|
|
;;
|
|
*)
|
|
echo -e "${RED}Unknown service: $1${NC}"
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# Connect to database
|
|
connect_db() {
|
|
echo -e "${BLUE}Connecting to database...${NC}"
|
|
cd "${PROJECT_ROOT}"
|
|
|
|
# Load .env file
|
|
if [ -f .env ]; then
|
|
export $(cat .env | grep -v '^#' | xargs)
|
|
fi
|
|
|
|
docker compose exec db mysql -u root -p${MYSQL_ROOT_PASSWORD} ${MYSQL_DB}
|
|
}
|
|
|
|
# Run migrations
|
|
run_migrations() {
|
|
echo -e "${GREEN}Running database migrations...${NC}"
|
|
cd "${PROJECT_ROOT}"
|
|
|
|
# Load .env file
|
|
if [ -f .env ]; then
|
|
export $(cat .env | grep -v '^#' | xargs)
|
|
fi
|
|
|
|
# Find all migration files
|
|
for migration in ${BACKEND_DIR}/src/migrations/*.sql; do
|
|
if [ -f "$migration" ]; then
|
|
echo -e "${BLUE}Applying $(basename "$migration")...${NC}"
|
|
docker compose exec -T db mysql -u root -p${MYSQL_ROOT_PASSWORD} ${MYSQL_DB} < "$migration" 2>/dev/null || true
|
|
fi
|
|
done
|
|
|
|
echo -e "${GREEN}Migrations complete!${NC}"
|
|
}
|
|
|
|
# Reset everything
|
|
reset_all() {
|
|
echo -e "${RED}WARNING: This will delete all data!${NC}"
|
|
read -p "Are you sure? (yes/NO): " -r
|
|
echo
|
|
if [[ $REPLY == "yes" ]]; then
|
|
cd "${PROJECT_ROOT}"
|
|
echo -e "${YELLOW}Stopping services...${NC}"
|
|
docker compose down -v
|
|
echo -e "${YELLOW}Removing images...${NC}"
|
|
docker compose down --rmi local
|
|
echo -e "${GREEN}Reset complete!${NC}"
|
|
else
|
|
echo "Aborted."
|
|
fi
|
|
}
|
|
|
|
# Run tests
|
|
run_tests() {
|
|
echo -e "${GREEN}Running tests...${NC}"
|
|
|
|
# Backend tests
|
|
if [ -d "${BACKEND_DIR}/tests" ]; then
|
|
echo -e "${BLUE}Running backend tests...${NC}"
|
|
cd "${PROJECT_ROOT}"
|
|
docker compose exec api pytest
|
|
fi
|
|
|
|
# Frontend tests
|
|
if [ -f "${FRONTEND_DIR}/package.json" ]; then
|
|
echo -e "${BLUE}Running frontend tests...${NC}"
|
|
cd "${PROJECT_ROOT}"
|
|
docker compose exec frontend npm test
|
|
fi
|
|
|
|
echo -e "${GREEN}Tests complete!${NC}"
|
|
}
|
|
|
|
# Run linters
|
|
run_lint() {
|
|
echo -e "${GREEN}Running linters...${NC}"
|
|
|
|
# Backend linting
|
|
echo -e "${BLUE}Linting backend...${NC}"
|
|
cd "${PROJECT_ROOT}"
|
|
docker compose exec api flake8 src/ || true
|
|
docker compose exec api mypy src/ || true
|
|
|
|
# Frontend linting
|
|
echo -e "${BLUE}Linting frontend...${NC}"
|
|
docker compose exec frontend npm run lint || true
|
|
|
|
echo -e "${GREEN}Linting complete!${NC}"
|
|
}
|
|
|
|
# Format code
|
|
format_code() {
|
|
echo -e "${GREEN}Formatting code...${NC}"
|
|
|
|
# Backend formatting
|
|
echo -e "${BLUE}Formatting backend...${NC}"
|
|
cd "${PROJECT_ROOT}"
|
|
docker compose exec api black src/
|
|
docker compose exec api isort src/
|
|
|
|
# Frontend formatting
|
|
echo -e "${BLUE}Formatting frontend...${NC}"
|
|
docker compose exec frontend npm run format || true
|
|
|
|
echo -e "${GREEN}Formatting complete!${NC}"
|
|
}
|
|
|
|
# Clean up
|
|
clean_up() {
|
|
echo -e "${YELLOW}Cleaning up...${NC}"
|
|
|
|
# Python cache
|
|
find "${BACKEND_DIR}" -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
|
|
find "${BACKEND_DIR}" -type f -name "*.pyc" -delete 2>/dev/null || true
|
|
|
|
# Node modules (if you want to clean them)
|
|
# rm -rf "${FRONTEND_DIR}/node_modules"
|
|
|
|
# Docker cleanup
|
|
docker system prune -f
|
|
|
|
echo -e "${GREEN}Cleanup complete!${NC}"
|
|
}
|
|
|
|
# Main script logic
|
|
check_docker
|
|
|
|
case "$1" in
|
|
start)
|
|
start_services
|
|
;;
|
|
stop)
|
|
stop_services
|
|
;;
|
|
restart)
|
|
restart_services
|
|
;;
|
|
status)
|
|
show_status
|
|
;;
|
|
logs)
|
|
show_logs "$2"
|
|
;;
|
|
build)
|
|
build_services "$2"
|
|
;;
|
|
shell)
|
|
open_shell "$2"
|
|
;;
|
|
db)
|
|
connect_db
|
|
;;
|
|
migrate)
|
|
run_migrations
|
|
;;
|
|
reset)
|
|
reset_all
|
|
;;
|
|
test)
|
|
run_tests
|
|
;;
|
|
lint)
|
|
run_lint
|
|
;;
|
|
format)
|
|
format_code
|
|
;;
|
|
clean)
|
|
clean_up
|
|
;;
|
|
*)
|
|
usage
|
|
;;
|
|
esac
|