#!/usr/bin/env bash # Development services manager for local development # Uses Nix to run PostgreSQL and MinIO set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' RED='\033[0;31m' NC='\033[0m' # Data directories POSTGRES_DATA="$PROJECT_ROOT/.dev-data/postgres" MINIO_DATA="$PROJECT_ROOT/.dev-data/minio" # Create data directories mkdir -p "$POSTGRES_DATA" "$MINIO_DATA" function start_postgres() { echo -e "${BLUE}🐘 Starting PostgreSQL...${NC}" if [ ! -d "$POSTGRES_DATA/PG_VERSION" ]; then echo "Initializing PostgreSQL database..." initdb -D "$POSTGRES_DATA" -U webref --encoding=UTF8 --locale=C fi # Start PostgreSQL pg_ctl -D "$POSTGRES_DATA" -l "$POSTGRES_DATA/logfile" start # Wait for PostgreSQL to be ready until pg_isready -q -h localhost -p 5432; do echo "Waiting for PostgreSQL..." sleep 1 done # Create database if it doesn't exist createdb -h localhost -U webref webref 2>/dev/null || true echo -e "${GREEN}✓ PostgreSQL running on localhost:5432${NC}" echo -e " Database: webref" echo -e " User: webref (no password)" } function stop_postgres() { echo -e "${BLUE}🐘 Stopping PostgreSQL...${NC}" pg_ctl -D "$POSTGRES_DATA" stop -m fast || true echo -e "${GREEN}✓ PostgreSQL stopped${NC}" } function start_minio() { echo -e "${BLUE}📦 Starting MinIO...${NC}" # Start MinIO in background MINIO_ROOT_USER=minioadmin \ MINIO_ROOT_PASSWORD=minioadmin \ minio server "$MINIO_DATA" \ --address :9000 \ --console-address :9001 \ > "$MINIO_DATA/minio.log" 2>&1 & MINIO_PID=$! echo $MINIO_PID > "$MINIO_DATA/minio.pid" # Wait for MinIO to be ready for i in {1..10}; do if curl -s http://localhost:9000/minio/health/live > /dev/null 2>&1; then break fi echo "Waiting for MinIO..." sleep 1 done # Create bucket if it doesn't exist mc alias set local http://localhost:9000 minioadmin minioadmin 2>/dev/null || true mc mb local/webref 2>/dev/null || true echo -e "${GREEN}✓ MinIO running${NC}" echo -e " API: http://localhost:9000" echo -e " Console: http://localhost:9001" echo -e " Credentials: minioadmin / minioadmin" } function stop_minio() { echo -e "${BLUE}📦 Stopping MinIO...${NC}" if [ -f "$MINIO_DATA/minio.pid" ]; then PID=$(cat "$MINIO_DATA/minio.pid") kill $PID 2>/dev/null || true rm "$MINIO_DATA/minio.pid" else # Try to find and kill MinIO process pkill -f "minio server" || true fi echo -e "${GREEN}✓ MinIO stopped${NC}" } function status() { echo -e "${BLUE}📊 Service Status${NC}" echo "" # PostgreSQL if pg_isready -q -h localhost -p 5432 2>/dev/null; then echo -e "${GREEN}✓ PostgreSQL${NC} - running on localhost:5432" else echo -e "${RED}✗ PostgreSQL${NC} - not running" fi # MinIO if curl -s http://localhost:9000/minio/health/live > /dev/null 2>&1; then echo -e "${GREEN}✓ MinIO${NC} - running on localhost:9000" else echo -e "${RED}✗ MinIO${NC} - not running" fi } function logs() { echo -e "${BLUE}📜 Showing service logs${NC}" echo "" if [ -f "$POSTGRES_DATA/logfile" ]; then echo -e "${YELLOW}=== PostgreSQL ===${NC}" tail -n 20 "$POSTGRES_DATA/logfile" echo "" fi if [ -f "$MINIO_DATA/minio.log" ]; then echo -e "${YELLOW}=== MinIO ===${NC}" tail -n 20 "$MINIO_DATA/minio.log" fi } function reset() { echo -e "${RED}⚠️ Resetting all data (this will delete everything)${NC}" read -p "Are you sure? (yes/no): " -r if [ "$REPLY" = "yes" ]; then stop_postgres stop_minio rm -rf "$POSTGRES_DATA" "$MINIO_DATA" echo -e "${GREEN}✓ All data deleted${NC}" else echo "Aborted" fi } # Main command handler case "${1:-}" in start) echo -e "${BLUE}🚀 Starting development services${NC}" echo "" start_postgres start_minio echo "" echo -e "${GREEN}✅ All services started!${NC}" echo "" echo "Environment variables:" echo " export DATABASE_URL='postgresql://webref@localhost:5432/webref'" echo " export MINIO_ENDPOINT='localhost:9000'" ;; stop) echo -e "${BLUE}🛑 Stopping development services${NC}" echo "" stop_postgres stop_minio echo "" echo -e "${GREEN}✅ All services stopped${NC}" ;; restart) $0 stop sleep 2 $0 start ;; status) status ;; logs) logs ;; reset) reset ;; *) echo "Development Services Manager" echo "" echo "Usage: $0 {start|stop|restart|status|logs|reset}" echo "" echo "Commands:" echo " start - Start PostgreSQL and MinIO" echo " stop - Stop all services" echo " restart - Restart all services" echo " status - Show service status" echo " logs - Show recent logs" echo " reset - Delete all data and reset services" exit 1 ;; esac