#!/bin/bash # CannaiQ Local Development Setup (Idempotent) # # This script starts the complete local development environment: # - PostgreSQL (cannaiq-postgres) on port 54320 # - Backend API on port 3010 # - CannaiQ Admin UI on port 8080 # - FindADispo Consumer UI on port 3001 # - Findagram Consumer UI on port 3002 # # Usage: ./setup-local.sh # # URLs: # Admin: http://localhost:8080/admin # FindADispo: http://localhost:3001 # Findagram: http://localhost:3002 # Backend: http://localhost:3010 # # Idempotent: Safe to run multiple times. Already-running services are left alone. 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 echo -e "${BLUE}================================${NC}" echo -e "${BLUE} CannaiQ Local Dev Setup${NC}" echo -e "${BLUE}================================${NC}" echo "" # Check for required tools command -v docker >/dev/null 2>&1 || { echo -e "${RED}Error: docker is required but not installed.${NC}" >&2; exit 1; } command -v npm >/dev/null 2>&1 || { echo -e "${RED}Error: npm is required but not installed.${NC}" >&2; exit 1; } # Get the script directory SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" ROOT_DIR="$SCRIPT_DIR/.." cd "$SCRIPT_DIR" # Step 1: PostgreSQL PG_RUNNING=$(docker ps --filter "name=cannaiq-postgres" --filter "status=running" -q) if [ -n "$PG_RUNNING" ]; then echo -e "${GREEN}[1/6] PostgreSQL already running (cannaiq-postgres)${NC}" else echo -e "${YELLOW}[1/6] Starting PostgreSQL (cannaiq-postgres)...${NC}" docker compose -f docker-compose.local.yml up -d cannaiq-postgres # Wait for PostgreSQL to be ready echo -e "${YELLOW} Waiting for PostgreSQL to be ready...${NC}" until docker exec cannaiq-postgres pg_isready -U cannaiq >/dev/null 2>&1; do sleep 1 done echo -e "${GREEN} PostgreSQL ready on port 54320${NC}" fi # Step 2: Create storage directories (always safe to run) mkdir -p storage/images/products mkdir -p storage/images/brands mkdir -p public/images # Step 3: Backend if lsof -i:3010 >/dev/null 2>&1; then echo -e "${GREEN}[2/6] Backend already running on port 3010${NC}" else echo -e "${YELLOW}[2/6] Starting Backend API...${NC}" # Install dependencies if needed if [ ! -d "node_modules" ]; then echo -e "${YELLOW} Installing backend dependencies...${NC}" npm install fi # Set environment for local mode export STORAGE_DRIVER=local export STORAGE_BASE_PATH=./storage export PORT=3010 # Start backend in background npm run dev > /tmp/cannaiq-backend.log 2>&1 & BACKEND_PID=$! echo $BACKEND_PID > /tmp/cannaiq-backend.pid echo -e "${GREEN} Backend starting (PID: $BACKEND_PID)${NC}" # Wait briefly for backend to start sleep 3 fi # Step 4: CannaiQ Admin UI if lsof -i:8080 >/dev/null 2>&1; then echo -e "${GREEN}[3/6] CannaiQ Admin already running on port 8080${NC}" else echo -e "${YELLOW}[3/6] Starting CannaiQ Admin UI...${NC}" cd "$ROOT_DIR/cannaiq" # Install dependencies if needed if [ ! -d "node_modules" ]; then echo -e "${YELLOW} Installing cannaiq dependencies...${NC}" npm install fi # Start frontend in background npm run dev:admin > /tmp/cannaiq-frontend.log 2>&1 & FRONTEND_PID=$! echo $FRONTEND_PID > /tmp/cannaiq-frontend.pid echo -e "${GREEN} CannaiQ Admin starting (PID: $FRONTEND_PID)${NC}" cd "$SCRIPT_DIR" fi # Step 5: FindADispo Consumer UI if lsof -i:3001 >/dev/null 2>&1; then echo -e "${GREEN}[4/6] FindADispo already running on port 3001${NC}" else echo -e "${YELLOW}[4/6] Starting FindADispo Consumer UI...${NC}" cd "$ROOT_DIR/findadispo/frontend" # Install dependencies if needed if [ ! -d "node_modules" ]; then echo -e "${YELLOW} Installing findadispo dependencies...${NC}" npm install fi # Start in background on port 3001 PORT=3001 npm run dev > /tmp/findadispo-frontend.log 2>&1 & FINDADISPO_PID=$! echo $FINDADISPO_PID > /tmp/findadispo-frontend.pid echo -e "${GREEN} FindADispo starting (PID: $FINDADISPO_PID)${NC}" cd "$SCRIPT_DIR" fi # Step 6: Findagram Consumer UI if lsof -i:3002 >/dev/null 2>&1; then echo -e "${GREEN}[5/6] Findagram already running on port 3002${NC}" else echo -e "${YELLOW}[5/6] Starting Findagram Consumer UI...${NC}" cd "$ROOT_DIR/findagram/frontend" # Install dependencies if needed if [ ! -d "node_modules" ]; then echo -e "${YELLOW} Installing findagram dependencies...${NC}" npm install fi # Start in background on port 3002 PORT=3002 npm run dev > /tmp/findagram-frontend.log 2>&1 & FINDAGRAM_PID=$! echo $FINDAGRAM_PID > /tmp/findagram-frontend.pid echo -e "${GREEN} Findagram starting (PID: $FINDAGRAM_PID)${NC}" cd "$SCRIPT_DIR" fi # Step 7: Health checks for newly started services echo "" echo -e "${YELLOW}[6/6] Checking service health...${NC}" # Check backend if it was just started if ! lsof -i:3010 >/dev/null 2>&1; then for i in {1..15}; do if curl -s http://localhost:3010/health > /dev/null 2>&1; then break fi sleep 1 done fi if curl -s http://localhost:3010/health > /dev/null 2>&1; then echo -e "${GREEN} Backend API: OK (port 3010)${NC}" else echo -e "${YELLOW} Backend API: Starting (check: tail -f /tmp/cannaiq-backend.log)${NC}" fi # Check CannaiQ Admin if curl -s http://localhost:8080 > /dev/null 2>&1; then echo -e "${GREEN} CannaiQ Admin: OK (port 8080)${NC}" else echo -e "${YELLOW} CannaiQ Admin: Starting (check: tail -f /tmp/cannaiq-frontend.log)${NC}" fi # Check FindADispo sleep 2 if curl -s http://localhost:3001 > /dev/null 2>&1; then echo -e "${GREEN} FindADispo: OK (port 3001)${NC}" else echo -e "${YELLOW} FindADispo: Starting (check: tail -f /tmp/findadispo-frontend.log)${NC}" fi # Check Findagram if curl -s http://localhost:3002 > /dev/null 2>&1; then echo -e "${GREEN} Findagram: OK (port 3002)${NC}" else echo -e "${YELLOW} Findagram: Starting (check: tail -f /tmp/findagram-frontend.log)${NC}" fi # Print final status echo "" echo -e "${BLUE}================================${NC}" echo -e "${GREEN} Local Environment Ready${NC}" echo -e "${BLUE}================================${NC}" echo "" echo -e " ${BLUE}Services:${NC}" echo -e " Postgres: localhost:54320" echo -e " Backend API: http://localhost:3010" echo "" echo -e " ${BLUE}Frontends:${NC}" echo -e " CannaiQ Admin: http://localhost:8080/admin" echo -e " FindADispo: http://localhost:3001" echo -e " Findagram: http://localhost:3002" echo "" echo -e "${YELLOW}To stop services:${NC} ./stop-local.sh" echo -e "${YELLOW}View logs:${NC}" echo " Backend: tail -f /tmp/cannaiq-backend.log" echo " CannaiQ: tail -f /tmp/cannaiq-frontend.log" echo " FindADispo: tail -f /tmp/findadispo-frontend.log" echo " Findagram: tail -f /tmp/findagram-frontend.log" echo ""