diff --git a/deployments/local/README.md b/deployments/local/README.md deleted file mode 100644 index 524e6aa1..00000000 --- a/deployments/local/README.md +++ /dev/null @@ -1,81 +0,0 @@ -# Local Development Setup - -Docker Compose configuration for local development. - -## Prerequisites - -- Docker & Docker Compose installed -- Neon account (https://neon.tech) - for database -- Ports available: 80, 6379, 5001, 3000, 3001, 8080 - -## Initial Setup - -### 1. Setup Neon Database - -```bash -# Run setup script -./scripts/db/setup-neon.sh - -# Or manually: -# 1. Create Neon project at https://neon.tech -# 2. Get connection string from main branch -# 3. Create deployments/local/.env.local: -# DATABASE_URL=postgresql://user:pass@ep-xxx.region.neon.tech/dbname?sslmode=require&pgbouncer=true -``` - -See [Neon Setup Guide](../../infra/databases/neon/README.md) for details. - -### 2. Start Services - -```bash -# Start infrastructure (Redis, Traefik - no PostgreSQL needed) -docker-compose up -d - -# Run migrations -./scripts/db/migrate.sh auth-service dev - -# Seed database (optional) -./scripts/db/seed.sh auth-service -``` - -## Usage - -```bash -# Start all services -docker-compose up -d - -# View logs -docker-compose logs -f - -# Stop all services -docker-compose down - -# Stop and remove volumes (clean slate) -docker-compose down -v -``` - -## Services - -- **Neon Database**: Cloud-hosted (no local container) -- **Redis**: `localhost:6379` -- **Auth Service**: `localhost:5001` -- **Web Admin**: `http://localhost:3000` or `http://admin.localhost` -- **Web Client**: `http://localhost:3001` or `http://localhost` -- **Traefik Dashboard**: `http://localhost:8080` - -## Access - -- API Gateway: `http://localhost/api/v1` -- Web Admin: `http://admin.localhost` (via Traefik) or `http://localhost:3000` (direct) -- Web Client: `http://localhost` (via Traefik) or `http://localhost:3001` (direct) -- Traefik Dashboard: `http://localhost:8080` - -## Environment Variables - -Copy `env.local.example` to `.env.local` and add your Neon DATABASE_URL: - -```bash -DATABASE_URL=postgresql://user:pass@ep-xxx.region.neon.tech/dbname?sslmode=require&pgbouncer=true -``` - -**Note**: PostgreSQL is not included in Docker Compose. All environments use Neon database. diff --git a/scripts/db/setup-neon.sh b/scripts/db/setup-neon.sh index d45245cb..c19d4c50 100755 --- a/scripts/db/setup-neon.sh +++ b/scripts/db/setup-neon.sh @@ -5,6 +5,9 @@ set -e echo "🚀 Neon Database Setup Script" echo "" +# Source OS helper +source "$(dirname "$0")/../utils/os-helper.sh" + # Colors for output GREEN='\033[0;32m' YELLOW='\033[1;33m' @@ -40,8 +43,7 @@ if [ -n "$dev_url" ]; then # EN: Update .env.local with new URL # VI: Cập nhật .env.local với URL mới if grep -q "DATABASE_URL=" "$ENV_LOCAL"; then - sed -i.bak "s|DATABASE_URL=.*|DATABASE_URL=$dev_url|" "$ENV_LOCAL" - rm -f "${ENV_LOCAL}.bak" + run_sed "s|DATABASE_URL=.*|DATABASE_URL=$dev_url|" "$ENV_LOCAL" else echo "DATABASE_URL=$dev_url" >> "$ENV_LOCAL" fi diff --git a/scripts/dev/start-all.sh b/scripts/dev/start-all.sh index 4ffbd20f..c952e4f0 100755 --- a/scripts/dev/start-all.sh +++ b/scripts/dev/start-all.sh @@ -4,6 +4,9 @@ set -e +# Source OS helper for cross-platform commands +source "$(dirname "$0")/../utils/os-helper.sh" + echo "🚀 Starting all services... / Khởi động tất cả services..." # EN: Verify Docker daemon is running @@ -38,7 +41,7 @@ fi # VI: Khởi động infrastructure services (Redis cho caching, Traefik cho routing) echo "📦 Starting infrastructure (Redis, Traefik)... / Khởi động infrastructure (Redis, Traefik)..." cd deployments/local -docker-compose up -d +run_compose up -d cd ../.. # EN: Give Redis time to fully start before starting services diff --git a/scripts/setup/init-project.sh b/scripts/setup/init-project.sh index 2056b9d5..be6dcff9 100755 --- a/scripts/setup/init-project.sh +++ b/scripts/setup/init-project.sh @@ -54,7 +54,7 @@ echo "" echo "Next steps:" echo "1. Setup Neon database: ./scripts/db/setup-neon.sh" echo "2. Update .env files with your Neon DATABASE_URL" -echo "3. Start infrastructure: cd deployments/local && docker-compose up -d" +echo "3. Start all services: ./scripts/dev/start-all.sh" echo "4. Run migrations: ./scripts/db/migrate.sh auth-service dev" echo "5. Seed database: ./scripts/db/seed.sh auth-service" echo "6. Start services: pnpm dev" diff --git a/scripts/utils/create-service.sh b/scripts/utils/create-service.sh index 89515160..afabadd2 100755 --- a/scripts/utils/create-service.sh +++ b/scripts/utils/create-service.sh @@ -4,6 +4,9 @@ set -e SERVICE_NAME=$1 +# Source OS helper for cross-platform commands +source "$(dirname "$0")/os-helper.sh" + # EN: Validate arguments # VI: Xác thực tham số if [ -z "$SERVICE_NAME" ]; then @@ -30,13 +33,11 @@ cp -r services/_template "$SERVICE_DIR" # EN: Update package.json # VI: Cập nhật package.json cd "$SERVICE_DIR" -sed -i.bak "s/@goodgo\/service-template/@goodgo\/$SERVICE_NAME/g" package.json -rm package.json.bak +run_sed "s/@goodgo\/service-template/@goodgo\/$SERVICE_NAME/g" package.json # EN: Update .env.example # VI: Cập nhật .env.example -sed -i.bak "s/SERVICE_NAME=service-name/SERVICE_NAME=$SERVICE_NAME/g" .env.example 2>/dev/null || true -rm .env.example.bak 2>/dev/null || true +run_sed "s/SERVICE_NAME=service-name/SERVICE_NAME=$SERVICE_NAME/g" .env.example # EN: Update Dockerfile port if needed # VI: Cập nhật port Dockerfile nếu cần diff --git a/scripts/utils/os-helper.sh b/scripts/utils/os-helper.sh new file mode 100755 index 00000000..a17f7d63 --- /dev/null +++ b/scripts/utils/os-helper.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +# EN: OS Detection and Compatibility Helper +# VI: Helper phát hiện Hệ điều hành và Tương thích + +OS="$(uname -s)" +case "${OS}" in + Linux*) OS_TYPE=Linux;; + Darwin*) OS_TYPE=Mac;; + CYGWIN*) OS_TYPE=Cygwin;; + MINGW*) OS_TYPE=MinGw;; + *) OS_TYPE="UNKNOWN:${OS}" +esac + +# EN: Logging function +# VI: Hàm logging +log_info() { + echo -e "\033[0;32m[INFO]\033[0m $1" +} + +log_warn() { + echo -e "\033[1;33m[WARN]\033[0m $1" +} + +# EN: Cross-platform SED command +# VI: Lệnh SED đa nền tảng +# Usage: run_sed "s/find/replace/g" filename +run_sed() { + if [ "$OS_TYPE" = "Mac" ]; then + sed -i '' "$1" "$2" + else + sed -i "$1" "$2" + fi +} + +# EN: Cross-platform OPEN command (open URL or file) +# VI: Lệnh OPEN đa nền tảng (mở URL hoặc file) +run_open() { + if [ "$OS_TYPE" = "Mac" ]; then + open "$1" + elif [ "$OS_TYPE" = "Linux" ]; then + xdg-open "$1" + else + echo "Cannot open $1 on $OS_TYPE" + fi +} + +export OS_TYPE +export -f run_sed +export -f run_open +export -f log_info +export -f log_warn + +# EN: Cross-platform Docker Compose command +# VI: Lệnh Docker Compose đa nền tảng +run_compose() { + if command -v docker-compose &> /dev/null; then + docker-compose "$@" + elif docker compose version &> /dev/null; then + docker compose "$@" + else + echo -e "\033[0;31m[ERROR]\033[0m Docker Compose not found / Không tìm thấy Docker Compose" + return 1 + fi +} + +export -f run_compose