From 29c40ea6813b0795e91f5f263cc5e4756b39b8a7 Mon Sep 17 00:00:00 2001 From: Ho Ngoc Hai Date: Sat, 27 Dec 2025 10:12:46 +0700 Subject: [PATCH] Update project documentation and scripts for improved setup and bilingual support - Enhanced `README.md` with a quick start guide and detailed project structure. - Updated `SETUP_GUIDE.md` by removing it as it was redundant. - Improved `local-development.md` and `development.md` with clearer instructions for database migrations. - Added bilingual comments in various scripts for better understanding and usability. - Updated `.gitignore` to include specific build scripts while ignoring others. - Enhanced `scripts` for database management, including backup and seeding functionalities with bilingual support. --- .gitignore | 1 + README.md | 212 +++++++++++++--------- README.vi.md | 212 ++++++++++++++++++++++ SETUP_GUIDE.md | 265 ---------------------------- docs/en/guides/development.md | 9 +- docs/en/guides/local-development.md | 17 +- docs/vi/guides/development.md | 9 +- docs/vi/guides/local-development.md | 17 +- infra/secrets/.gitignore | 4 - scripts/build/build-all.sh | 11 ++ scripts/build/build-service.sh | 28 +++ scripts/db/backup.sh | 31 +++- scripts/db/seed.sh | 7 +- scripts/db/setup-neon.sh | 6 +- scripts/deploy/deploy-prod.sh | 8 + scripts/deploy/deploy-staging.sh | 4 + scripts/dev/logs.sh | 12 +- scripts/dev/setup-env.sh | 20 ++- scripts/dev/start-service.sh | 5 +- scripts/setup/init-project.sh | 12 +- scripts/setup/install-deps.sh | 2 + scripts/utils/cleanup.sh | 12 +- scripts/utils/create-service.sh | 16 +- 23 files changed, 528 insertions(+), 392 deletions(-) create mode 100644 README.vi.md delete mode 100644 SETUP_GUIDE.md delete mode 100644 infra/secrets/.gitignore create mode 100755 scripts/build/build-all.sh create mode 100755 scripts/build/build-service.sh diff --git a/.gitignore b/.gitignore index 7e8f3865..2cff97a1 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ coverage/ # Production dist/ build/ +!scripts/build/ .next/ out/ diff --git a/README.md b/README.md index 501077b8..c32f695f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # GoodGo Microservices Platform +[![English](https://img.shields.io/badge/Language-English-blue.svg)](README.md) [![Tiếng Việt](https://img.shields.io/badge/Ngôn%20ngữ-Tiếng%20Việt-red.svg)](README.vi.md) + Enterprise-grade microservices monorepo built with modern technologies and best practices. ## 🏗️ Architecture @@ -7,7 +9,7 @@ Enterprise-grade microservices monorepo built with modern technologies and best This monorepo follows a microservices architecture pattern with: - **Apps**: Frontend applications (Web + Mobile) -- **Services**: Backend microservices (starting with Auth Service) +- **Services**: Backend microservices (Node.js/TypeScript) - **Packages**: Shared libraries and utilities - **Infrastructure**: Traefik, Observability, Databases - **Deployments**: Environment-specific configurations @@ -21,26 +23,32 @@ This monorepo follows a microservices architecture pattern with: │ ├── app-admin/ # Flutter admin mobile application │ └── app-client/ # Flutter client mobile application ├── services/ # Backend microservices -│ ├── auth-service/ # Authentication service -│ └── _template/ # Service template +│ ├── auth-service/ # Authentication service (Node.js) +│ └── _template/ # Service template for new services ├── packages/ # Shared libraries -│ ├── logger/ # Centralized logging -│ ├── types/ # TypeScript types -│ ├── config/ # Shared configs -│ ├── http-client/ # API client wrapper -│ ├── auth-sdk/ # Auth utilities -│ └── tracing/ # OpenTelemetry setup +│ ├── auth-sdk/ # Auth utilities and guards +│ ├── config/ # Shared configurations +│ ├── http-client/ # Standardized API client +│ ├── logger/ # Centralized logging (Winston) +│ ├── tracing/ # OpenTelemetry setup +│ └── types/ # Shared TypeScript types ├── infra/ # Infrastructure as Code -│ ├── traefik/ # Traefik configuration -│ ├── docker/ # Docker configs -│ ├── observability/ # Monitoring stack -│ └── databases/ # Database configs +│ ├── databases/ # Database configs (PostgreSQL/Neon, Redis) +│ ├── docker/ # Docker compose files +│ ├── observability/ # Monitoring stack (Prometheus, Grafana, Loki) +│ ├── secrets/ # Secret management +│ └── traefik/ # API Gateway configuration ├── deployments/ # Environment configs -│ ├── local/ # Local development -│ ├── staging/ # Staging environment -│ └── production/ # Production environment -├── .github/ # CI/CD workflows -├── scripts/ # Automation scripts +│ ├── local/ # Local development setup +│ ├── staging/ # Staging environment (Kubernetes) +│ └── production/ # Production environment (Kubernetes) +├── scripts/ # Automation scripts (Bilingual EN/VI) +│ ├── build/ # Build scripts +│ ├── db/ # Database management (backup, seed, migrate) +│ ├── deploy/ # Deployment scripts +│ ├── dev/ # Development helpers +│ ├── setup/ # Project initialization +│ └── utils/ # Utility scripts (cleanup, generators) └── docs/ # Documentation ``` @@ -53,100 +61,144 @@ This monorepo follows a microservices architecture pattern with: - Docker & Docker Compose - Neon account (https://neon.tech) - for PostgreSQL database -### Installation +### Quick Start (One-Command Setup) + +We provide a comprehensive initialization script to get you up and running quickly: + +```bash +./scripts/setup/init-project.sh +``` + +This script will: +1. Check prerequisites +2. Install dependencies +3. Generate Prisma clients +4. Create necessary `.env` files from examples + +### Manual Installation + +If you prefer to set up manually: ```bash # Install dependencies pnpm install +# Generate Prisma clients +pnpm prisma:generate + # Start all services (development) -pnpm dev - -# Start specific service -pnpm --filter @goodgo/auth-service dev - -# Build all -pnpm build - -# Run tests -pnpm test - -# Run linter -pnpm lint +./scripts/dev/start-all.sh ``` -### Local Development +### Local Development Flow -```bash -# Setup Neon database (first time only) -./scripts/db/setup-neon.sh -# Or manually: Create .env.local with Neon DATABASE_URL +1. **Database Setup**: + ```bash + ./scripts/db/setup-neon.sh + # Follow the interactive prompts to configure your Neon URL + ``` -# Start infrastructure (Redis, Traefik - no PostgreSQL needed) -cd deployments/local -docker-compose up -d +2. **Start Infrastructure**: + ```bash + # Starts Redis, Traefik, and Observability stack + cd deployments/local + docker-compose up -d + ``` -# Run migrations -./scripts/db/migrate.sh auth-service dev +3. **Run Migrations**: + ```bash + ./scripts/db/migrate.sh auth-service dev + ``` -# Start services -pnpm dev -``` +4. **Start Services**: + ```bash + ./scripts/dev/start-all.sh + # Or to start a specific service: + # ./scripts/dev/start-service.sh auth-service + ``` -**Note**: This project uses Neon PostgreSQL. No local PostgreSQL setup needed! +## 🛠️ Helper Scripts + +The `scripts/` directory contains bilingual (English/Vietnamese) automation scripts to streamline daily tasks: + +| Category | Script | Description | +|----------|--------|-------------| +| **Setup** | `init-project.sh` | Full project initialization | +| | `install-deps.sh` | Install dependencies | +| **Dev** | `start-all.sh` | Start infrastructure and all services | +| | `start-service.sh` | Start a specific service | +| | `logs.sh` | View logs for services or docker containers | +| | `setup-env.sh` | Helper to setup environment variables | +| **DB** | `migrate.sh` | Run Prisma migrations (dev/deploy) | +| | `seed.sh` | Seed database with initial data | +| | `backup.sh` | Backup database (supports Neon) | +| | `setup-neon.sh` | Wizard to configure Neon DB connection | +| **Utils** | `create-service.sh` | Scaffold a new microservice from template | +| | `cleanup.sh` | Clean build artifacts and caches | +| **Build** | `build-all.sh` | Build entire monorepo | +| | `build-service.sh` | Build specific service | +| **Deploy** | `deploy-staging.sh` | Deploy to staging cluster | +| | `deploy-prod.sh` | Deploy to production cluster | ## 🛠️ Tech Stack **Frontend:** -- Web: Next.js 14+ (App Router) - Admin & Client portals -- Mobile: Flutter 3.x - Admin & Client apps -- TypeScript / Dart -- Tailwind CSS -- Zustand / Provider +- Web: Next.js 14+ (App Router) +- Mobile: Flutter 3.x +- Styling: Tailwind CSS +- State: Zustand / Provider **Backend:** -- Node.js + TypeScript -- Express/NestJS -- Prisma ORM -- PostgreSQL (Neon - serverless) -- Redis +- Runtime: Node.js + TypeScript +- Framework: Express/NestJS (via service template) +- ORM: Prisma +- Database: PostgreSQL (Neon Serverless), Redis (Caching/Queues) **Infrastructure:** -- Traefik (API Gateway) -- Docker & Docker Compose -- Kubernetes (production) -- Prometheus + Grafana + Loki -- OpenTelemetry +- Gateway: Traefik +- Containerization: Docker +- Orchestration: Kubernetes +- Observability: Prometheus, Grafana, Loki, OpenTelemetry **DevOps:** -- GitHub Actions -- PNPM Workspaces -- Turborepo +- Build System: Turborepo +- Package Manager: PNPM Workspaces +- CI/CD: GitHub Actions + +## 🌐 Coding Standards + +### Bilingual Comments +To support our diverse team, we follow a bilingual commenting standard (English & Vietnamese) for: +- Scripts +- Complex logic +- Public APIs +- Configuration files + +Example: +```bash +# EN: Verify Docker daemon is running +# VI: Xác minh Docker daemon đang chạy +``` + +### Monorepo Management +- We use **Turborepo** for build pipelines and caching. +- **PNPM Workspaces** manage dependencies across apps and services. ## 📚 Documentation -- [Architecture Overview](docs/architecture/system-design.md) -- [API Documentation](docs/api/openapi/) -- [Development Guide](docs/guides/development.md) -- [Deployment Guide](docs/guides/deployment.md) +- [Architecture Overview](docs/en/architecture/system-design.md) +- [API Documentation](docs/en/api/openapi/) +- [Development Guide](docs/en/guides/development.md) +- [Deployment Guide](docs/en/guides/deployment.md) - [Contributing Guide](CONTRIBUTING.md) ## 🔐 Environment Variables -### Database (Neon) - -All environments use Neon PostgreSQL: -- **Dev**: Set `DATABASE_URL` in `deployments/local/.env.local` -- **Staging/Prod**: Set in Kubernetes secrets or GitHub Secrets - -See `infra/databases/neon/README.md` for setup instructions. - -### Other Variables - -Copy `.env.example` files in each service/app and configure accordingly. - **Important**: Never commit `.env` files. Use `.env.example` as templates. +- **Dev**: `deployments/local/.env.local` (Shared), `services/*/.env.local` (Service specific) +- **Staging/Prod**: Managed via Kubernetes Secrets + ## 🧪 Testing ```bash diff --git a/README.vi.md b/README.vi.md new file mode 100644 index 00000000..60bc0ed5 --- /dev/null +++ b/README.vi.md @@ -0,0 +1,212 @@ +# Nền tảng Microservices GoodGo + +[![English](https://img.shields.io/badge/Language-English-blue.svg)](README.md) [![Tiếng Việt](https://img.shields.io/badge/Ngôn%20ngữ-Tiếng%20Việt-red.svg)](README.vi.md) + +Nền tảng microservices cấp doanh nghiệp được xây dựng với các công nghệ hiện đại và thực tiễn tốt nhất. + +## 🏗️ Kiến trúc + +Monorepo này tuân theo mô hình kiến trúc microservices với: + +- **Apps**: Các ứng dụng Frontend (Web + Mobile) +- **Services**: Các Backend microservices (Node.js/TypeScript) +- **Packages**: Các thư viện và tiện ích dùng chung +- **Infrastructure**: Traefik, Observability, Databases +- **Deployments**: Cấu hình riêng cho từng môi trường + +## 📁 Cấu trúc Dự án + +``` +├── apps/ # Ứng dụng Frontend +│ ├── web-admin/ # Web admin (Next.js) +│ ├── web-client/ # Web client (Next.js) +│ ├── app-admin/ # App admin (Flutter) +│ └── app-client/ # App client (Flutter) +├── services/ # Backend microservices +│ ├── auth-service/ # Dịch vụ xác thực (Node.js) +│ └── _template/ # Template mẫu cho service mới +├── packages/ # Thư viện dùng chung +│ ├── auth-sdk/ # Tiện ích xác thực và guards +│ ├── config/ # Cấu hình chung +│ ├── http-client/ # API client chuẩn hóa +│ ├── logger/ # Logging tập trung (Winston) +│ ├── tracing/ # Cấu hình OpenTelemetry +│ └── types/ # Các kiểu dữ liệu TypeScript dùng chung +├── infra/ # Infrastructure as Code +│ ├── databases/ # Cấu hình Database (PostgreSQL/Neon, Redis) +│ ├── docker/ # File Docker compose +│ ├── observability/ # Stack giám sát (Prometheus, Grafana, Loki) +│ ├── secrets/ # Quản lý bảo mật +│ └── traefik/ # Cấu hình API Gateway +├── deployments/ # Cấu hình môi trường +│ ├── local/ # Thiết lập dev local +│ ├── staging/ # Môi trường Staging (Kubernetes) +│ └── production/ # Môi trường Production (Kubernetes) +├── scripts/ # Script tự động hóa (Song ngữ EN/VI) +│ ├── build/ # Script build +│ ├── db/ # Quản lý Database (backup, seed, migrate) +│ ├── deploy/ # Script deploy +│ ├── dev/ # Hỗ trợ Development +│ ├── setup/ # Khởi tạo dự án +│ └── utils/ # Script tiện ích +└── docs/ # Tài liệu +``` + +## 🚀 Bắt đầu + +### Yêu cầu tiên quyết + +- Node.js >= 20.0.0 +- pnpm >= 8.0.0 +- Docker & Docker Compose +- Tài khoản Neon (https://neon.tech) - cho PostgreSQL database + +### Khởi tạo nhanh (1 Lệnh duy nhất) + +Chúng tôi cung cấp script khởi tạo toàn diện để bạn bắt đầu nhanh chóng: + +```bash +./scripts/setup/init-project.sh +``` + +Script này sẽ: +1. Kiểm tra các yêu cầu tiên quyết +2. Cài đặt dependencies +3. Tạo Prisma clients +4. Tạo các file `.env` cần thiết từ file mẫu + +### Cài đặt thủ công + +Nếu bạn muốn thiết lập thủ công: + +```bash +# Cài đặt dependencies +pnpm install + +# Tạo Prisma clients +pnpm prisma:generate + +# Khởi động tất cả services (chế độ development) +./scripts/dev/start-all.sh +``` + +### Quy trình Development Local + +1. **Thiết lập Database**: + ```bash + ./scripts/db/setup-neon.sh + # Làm theo hướng dẫn để cấu hình Neon URL của bạn + ``` + +2. **Khởi động Infrastructure**: + ```bash + # Khởi động Redis, Traefik, và Observability stack + cd deployments/local + docker-compose up -d + ``` + +3. **Chạy Migrations**: + ```bash + ./scripts/db/migrate.sh auth-service dev + ``` + +4. **Khởi động Services**: + ```bash + ./scripts/dev/start-all.sh + # Hoặc khởi động service cụ thể: + # ./scripts/dev/start-service.sh auth-service + ``` + +## 🛠️ Các Script Hỗ trợ + +Thư mục `scripts/` chứa các script tự động hóa song ngữ (Anh/Việt) giúp đơn giản hóa các tác vụ hàng ngày: + +| Danh mục | Script | Mô tả | +|---|---|---| +| **Setup** | `init-project.sh` | Khởi tạo toàn bộ dự án | +| | `install-deps.sh` | Cài đặt dependencies | +| **Dev** | `start-all.sh` | Khởi động hạ tầng và tất cả services | +| | `start-service.sh` | Khởi động một service cụ thể | +| | `logs.sh` | Xem log của services hoặc docker containers | +| | `setup-env.sh` | Hỗ trợ thiết lập biến môi trường | +| **DB** | `migrate.sh` | Chạy Prisma migrations (dev/deploy) | +| | `seed.sh` | Seed data ban đầu cho database | +| | `backup.sh` | Backup database (hỗ trợ Neon) | +| | `setup-neon.sh` | Wizard cấu hình kết nối Neon DB | +| **Utils** | `create-service.sh` | Tạo microservice mới từ template | +| | `cleanup.sh` | Dọn dẹp file build và cache | +| **Build** | `build-all.sh` | Build toàn bộ monorepo | +| | `build-service.sh` | Build service cụ thể | +| **Deploy** | `deploy-staging.sh` | Deploy lên cluster staging | +| | `deploy-prod.sh` | Deploy lên cluster production | + +## 🛠️ Công nghệ sử dụng + +**Frontend:** +- Web: Next.js 14+ (App Router) +- Mobile: Flutter 3.x +- Styling: Tailwind CSS +- State: Zustand / Provider + +**Backend:** +- Runtime: Node.js + TypeScript +- Framework: Express/NestJS (thông qua service template) +- ORM: Prisma +- Database: PostgreSQL (Neon Serverless), Redis (Caching/Queues) + +**Infrastructure:** +- Gateway: Traefik +- Containerization: Docker +- Orchestration: Kubernetes +- Observability: Prometheus, Grafana, Loki, OpenTelemetry + +**DevOps:** +- Build System: Turborepo +- Package Manager: PNPM Workspaces +- CI/CD: GitHub Actions + +## 🌐 Tiêu chuẩn Code + +### Comment Song ngữ +Để hỗ trợ đội ngũ đa dạng, chúng tôi tuân thủ tiêu chuẩn comment song ngữ (Anh & Việt) cho: +- Scripts +- Các logic phức tạp +- Public APIs +- Các file cấu hình + +Ví dụ: +```bash +# EN: Verify Docker daemon is running +# VI: Xác minh Docker daemon đang chạy +``` + +### Quản lý Monorepo +- Sử dụng **Turborepo** cho quy trình build và caching. +- **PNPM Workspaces** quản lý dependencies giữa các apps và services. + +## 📚 Tài liệu + +- [Tổng quan Kiến trúc](docs/vi/architecture/system-design.md) +- [Tài liệu API](docs/vi/api/openapi/) +- [Hướng dẫn Phát triển](docs/vi/guides/development.md) +- [Hướng dẫn Deployment](docs/vi/guides/deployment.md) +- [Hướng dẫn Đóng góp](CONTRIBUTING.md) + +## 🔐 Biến môi trường + +**Quan trọng**: Không bao giờ commit file `.env`. Hãy sử dụng `.env.example` làm mẫu. + +- **Dev**: `deployments/local/.env.local` (Dùng chung), `services/*/.env.local` (Riêng cho Service) +- **Staging/Prod**: Được quản lý qua Kubernetes Secrets + +## 🤝 Đóng góp + +Vui lòng đọc file [CONTRIBUTING.md](CONTRIBUTING.md) để biết chi tiết về quy tắc ứng xử và quy trình gửi pull requests của chúng tôi. + +## 📄 Giấy phép + +Dự án này được cấp phép theo Giấy phép MIT - xem file [LICENSE](LICENSE) để biết chi tiết. + +## 👥 Nhóm phát triển + +Được xây dựng với ❤️ bởi đội ngũ GoodGo diff --git a/SETUP_GUIDE.md b/SETUP_GUIDE.md deleted file mode 100644 index aae3a439..00000000 --- a/SETUP_GUIDE.md +++ /dev/null @@ -1,265 +0,0 @@ -# Environment Setup Guide - Hướng Dẫn Thiết Lập Môi Trường - -## 🎯 Tổng Quan - -Dự án sử dụng **Hybrid Environment Configuration** với 2 levels: - -1. **Shared Environment** (`deployments/local/.env.local`) - Configs dùng chung -2. **Service-Specific Environment** (`services//.env.local`) - Configs riêng - -### Lợi Ích - -- ✅ **JWT secrets giống nhau** → Services có thể verify tokens của nhau -- ✅ **Mỗi service có database riêng** → Đúng microservices pattern -- ✅ **Override configs dễ dàng** → REDIS_HOST khác nhau cho Docker/Native -- ✅ **Không duplicate** → Maintain dễ hơn - -## 🚀 Quick Start - -### Option 1: Sử Dụng Script (Khuyến nghị) - -```bash -# Tự động tạo tất cả env files -./scripts/dev/setup-env.sh -``` - -### Option 2: Setup Thủ Công - -**Step 1: Shared Environment** -```bash -cp deployments/local/env.local.example deployments/local/.env.local -``` - -**Step 2: Service Environment** -```bash -cp services/auth-service/env.local.example services/auth-service/.env.local -``` - -## 📝 Cấu Hình Chi Tiết - -### 1. Shared Environment (`deployments/local/.env.local`) - -Chứa configs dùng chung cho TẤT CẢ services: - -```bash -# JWT Secrets - PHẢI GIỐNG NHAU cho tất cả services -JWT_SECRET=dev-jwt-secret-change-in-production-min-32-chars -JWT_REFRESH_SECRET=dev-refresh-secret-change-in-production-min-32-chars - -# Redis (Docker hostname) -REDIS_HOST=redis -REDIS_PORT=6379 - -# Common -NODE_ENV=development -LOG_LEVEL=debug -CORS_ORIGIN=http://localhost:3000,http://localhost:3001 -``` - -### 2. Service Environment (`services/auth-service/.env.local`) - -Chứa configs RIÊNG cho từng service: - -```bash -# Database riêng cho service này -DATABASE_URL=postgresql://user:pass@ep-xxx.neon.tech/goodgo_auth_dev?sslmode=require&pgbouncer=true - -# Service configs -PORT=5001 -SERVICE_NAME=auth-service - -# Override cho native dev (Redis trong Docker) -REDIS_HOST=localhost -``` - -## 🗄️ Database Setup - -### Tạo Databases Trong Neon - -Mỗi service cần database riêng: - -1. Truy cập: https://console.neon.tech -2. Tạo databases: - - `goodgo_auth_dev` → auth-service - - `goodgo_user_dev` → user-service - - `goodgo_product_dev` → product-service - - etc. - -3. Copy connection string vào `services//.env.local`: -```bash -DATABASE_URL=postgresql://user:pass@ep-xxx.neon.tech/goodgo_auth_dev?sslmode=require&pgbouncer=true -``` - -### Run Migrations - -```bash -# Auth service -./scripts/db/migrate.sh auth-service dev - -# Seed data (optional) -./scripts/db/seed.sh auth-service -``` - -## 🔧 Cách Hoạt Động - -### Loading Order - -Services load environment theo thứ tự: - -1. **Shared env** (`deployments/local/.env.local`) - - JWT_SECRET, JWT_REFRESH_SECRET - - REDIS_HOST=redis, REDIS_PORT - - NODE_ENV, LOG_LEVEL, CORS_ORIGIN - -2. **Service env** (`.env.local`) - - DATABASE_URL (riêng cho service) - - PORT (riêng cho service) - - REDIS_HOST=localhost (override cho native dev) - -### Package.json Configuration - -```json -{ - "scripts": { - "dev": "dotenv -e ../../deployments/local/.env.local -e .env.local -- tsx watch src/main.ts" - } -} -``` - -Sử dụng `dotenv-cli` để load multiple env files. - -## 📂 File Structure - -``` -Base/ -├── deployments/local/ -│ ├── env.local.example # Template cho shared env -│ └── .env.local # Shared env (gitignored) -│ -├── services/ -│ ├── auth-service/ -│ │ ├── env.example # Old template (deprecated) -│ │ ├── env.local.example # New template cho service env -│ │ └── .env.local # Service env (gitignored) -│ │ -│ └── user-service/ -│ ├── env.local.example -│ └── .env.local -│ -└── scripts/dev/ - └── setup-env.sh # Auto setup script -``` - -## ⚠️ Important Notes - -### 1. JWT Secrets - -**MUST BE IDENTICAL** across all services: -- Services cần verify JWT tokens của nhau -- Nếu khác nhau → authentication sẽ fail - -### 2. Database URLs - -**MUST BE DIFFERENT** for each service: -- Mỗi service có database riêng (microservices pattern) -- Ví dụ: - - auth-service → `goodgo_auth_dev` - - user-service → `goodgo_user_dev` - -### 3. Redis Host - -**Khác nhau tùy môi trường:** -- **Shared env**: `REDIS_HOST=redis` (Docker hostname) -- **Service env**: `REDIS_HOST=localhost` (override cho native dev) - -Khi chạy native, Redis trong Docker → dùng `localhost` - -### 4. Git Ignore - -Tất cả `.env.local` files đã được gitignored: -- `deployments/local/.env.local` -- `services/*/.env.local` - -**NEVER commit** actual env files! - -## 🎓 Examples - -### Example 1: Auth Service Native Dev - -```bash -# 1. Setup env -cp services/auth-service/env.local.example services/auth-service/.env.local - -# 2. Edit .env.local -DATABASE_URL=postgresql://user:pass@ep-xxx.neon.tech/goodgo_auth_dev?sslmode=require -PORT=5001 -REDIS_HOST=localhost # Override cho native dev - -# 3. Start Redis in Docker -cd deployments/local && docker-compose up -d redis - -# 4. Run service -pnpm --filter @goodgo/auth-service dev -``` - -### Example 2: Multiple Services - -```bash -# 1. Setup all envs -./scripts/dev/setup-env.sh - -# 2. Edit each service's .env.local with different DATABASE_URL - -# 3. Start infrastructure -cd deployments/local && docker-compose up -d redis traefik - -# 4. Run all services -pnpm dev -``` - -## 🔍 Troubleshooting - -### Database Connection Failed - -```bash -# Check if .env.local exists -ls -la services/auth-service/.env.local - -# Check DATABASE_URL -cat services/auth-service/.env.local | grep DATABASE_URL - -# Test connection -cd services/auth-service && pnpm prisma db pull -``` - -### JWT Secret Warning - -Nếu thấy warning "Using default JWT_SECRET": -- Check `deployments/local/.env.local` có JWT_SECRET chưa -- Check service có load đúng env files chưa - -### Redis Connection Failed - -Khi chạy native: -- Check Redis container đang chạy: `docker ps | grep redis` -- Check REDIS_HOST=localhost trong service `.env.local` - -## 📚 Tài Liệu Thêm - -- [Local Development Guide](docs/vi/guides/local-development.md) -- [Neon Database Guide](docs/vi/guides/neon-database.md) -- [Service README](services/auth-service/README.md) - -## 🎉 Ready to Go! - -Sau khi setup xong: - -```bash -# Start everything -./scripts/dev/start-all.sh - -# Or selective -pnpm --filter @goodgo/auth-service dev -``` - -Happy coding! 🚀 diff --git a/docs/en/guides/development.md b/docs/en/guides/development.md index 0e0625eb..5c66e3ee 100644 --- a/docs/en/guides/development.md +++ b/docs/en/guides/development.md @@ -71,13 +71,14 @@ pnpm format ## Database Migrations +## Database Migrations + ```bash -# Create migration -cd services/auth-service -pnpm prisma migrate dev --name add_new_field +# Create migration (dev) +./scripts/db/migrate.sh auth-service dev # Apply migrations (production) -pnpm prisma migrate deploy +./scripts/db/migrate.sh auth-service deploy ``` ## Debugging diff --git a/docs/en/guides/local-development.md b/docs/en/guides/local-development.md index 69d15b2b..b82eb033 100644 --- a/docs/en/guides/local-development.md +++ b/docs/en/guides/local-development.md @@ -10,7 +10,22 @@ Comprehensive guide for running and developing the project locally with real-tim - **Git**: For cloning repository - **Neon Account**: https://neon.tech (for database) -## Initial Setup +## Quick Start (Recommended) + +1. **Clone Repository**: + ```bash + git clone + cd Base + ``` + +2. **Run Initialization Script**: + ```bash + ./scripts/setup/init-project.sh + ``` + This script will install dependencies, generate clients, and setup environment files. + +## Manual Setup + ### 1. Clone Repository diff --git a/docs/vi/guides/development.md b/docs/vi/guides/development.md index 3bd5b56c..e718e20a 100644 --- a/docs/vi/guides/development.md +++ b/docs/vi/guides/development.md @@ -71,13 +71,14 @@ pnpm format ## Database Migrations +## Database Migrations + ```bash -# Tạo migration -cd services/auth-service -pnpm prisma migrate dev --name add_new_field +# Tạo migration (dev) +./scripts/db/migrate.sh auth-service dev # Áp dụng migrations (production) -pnpm prisma migrate deploy +./scripts/db/migrate.sh auth-service deploy ``` ## Debugging diff --git a/docs/vi/guides/local-development.md b/docs/vi/guides/local-development.md index 5a597dbe..f3aefa06 100644 --- a/docs/vi/guides/local-development.md +++ b/docs/vi/guides/local-development.md @@ -10,7 +10,22 @@ Hướng dẫn chi tiết cách chạy và phát triển dự án trên máy loc - **Git**: Để clone repository - **Tài khoản Neon**: https://neon.tech (cho database) -## Thiết Lập Ban Đầu +## Bắt Đầu Nhanh (Khuyến Nghị) + +1. **Clone Repository**: + ```bash + git clone + cd Base + ``` + +2. **Chạy Script Khởi Tạo**: + ```bash + ./scripts/setup/init-project.sh + ``` + Script này sẽ cài đặt dependencies, tạo clients, và thiết lập các file môi trường. + +## Thiết Lập Thủ Công + ### 1. Clone Repository diff --git a/infra/secrets/.gitignore b/infra/secrets/.gitignore deleted file mode 100644 index 794096a9..00000000 --- a/infra/secrets/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -# Ignore all secrets -* -!.gitignore -!.env.example diff --git a/scripts/build/build-all.sh b/scripts/build/build-all.sh new file mode 100755 index 00000000..80d0768c --- /dev/null +++ b/scripts/build/build-all.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +echo "🔨 Building all services and apps..." + +# EN: Build all packages using pnpm workspace +# VI: Build tất cả các package sử dụng pnpm workspace +pnpm build + +echo "✅ Build completed!" diff --git a/scripts/build/build-service.sh b/scripts/build/build-service.sh new file mode 100755 index 00000000..bd60c7be --- /dev/null +++ b/scripts/build/build-service.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +set -e + +SERVICE=$1 + +# EN: Validate arguments +# VI: Xác thực tham số +if [ -z "$SERVICE" ]; then + echo "Usage: $0 " + echo "Example: $0 auth-service" + exit 1 +fi + +# EN: Check if service exists +# VI: Kiểm tra xem service có tồn tại không +if [ ! -d "services/$SERVICE" ]; then + echo "❌ Service $SERVICE not found" + exit 1 +fi + +echo "🔨 Building $SERVICE..." + +# EN: Build specific service using pnpm filter +# VI: Build service cụ thể sử dụng pnpm filter +pnpm --filter "@goodgo/$SERVICE" build + +echo "✅ Build completed!" diff --git a/scripts/db/backup.sh b/scripts/db/backup.sh index d091f6dc..4a98a638 100755 --- a/scripts/db/backup.sh +++ b/scripts/db/backup.sh @@ -5,30 +5,43 @@ set -e SERVICE=$1 BACKUP_DIR="${2:-./backups}" +# EN: Validate arguments +# VI: Xác thực tham số if [ -z "$SERVICE" ]; then echo "Usage: $0 [backup-dir]" echo "Example: $0 auth-service" exit 1 fi +# EN: Create backup directory if not exists +# VI: Tạo thư mục backup nếu chưa tồn tại mkdir -p "$BACKUP_DIR" +# EN: Generate timestamped filename +# VI: Tạo tên file với dấu thời gian TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="$BACKUP_DIR/${SERVICE}_${TIMESTAMP}.sql" echo "💾 Backing up database for $SERVICE..." -# Extract database URL from .env or environment +# EN: Extract database URL from .env or environment +# VI: Trích xuất database URL từ .env hoặc biến môi trường cd "services/$SERVICE" if [ -z "$DATABASE_URL" ]; then + # EN: Check local .env file + # VI: Kiểm tra file .env cục bộ if [ -f ".env" ]; then DATABASE_URL=$(grep DATABASE_URL .env | cut -d '=' -f2- | tr -d '"' | tr -d "'") + # EN: Check shared env file + # VI: Kiểm tra file env chia sẻ elif [ -f "../../deployments/local/.env.local" ]; then DATABASE_URL=$(grep DATABASE_URL ../../deployments/local/.env.local | cut -d '=' -f2- | tr -d '"' | tr -d "'") fi fi +# EN: Validate DATABASE_URL is set +# VI: Xác thực DATABASE_URL đã được thiết lập if [ -z "$DATABASE_URL" ]; then echo "❌ DATABASE_URL not found. Please set it in:" echo " - services/$SERVICE/.env" @@ -37,17 +50,23 @@ if [ -z "$DATABASE_URL" ]; then exit 1 fi -# Use pg_dump directly with DATABASE_URL (works with Neon and standard PostgreSQL) +# EN: Use pg_dump directly with DATABASE_URL (works with Neon and standard PostgreSQL) +# VI: Sử dụng pg_dump trực tiếp với DATABASE_URL (hoạt động với Neon và PostgreSQL chuẩn) +# # Neon URLs format: postgresql://user:pass@ep-xxx.region.neon.tech/dbname?sslmode=require # Standard format: postgresql://user:pass@host:port/dbname -# Check if pg_dump supports connection string (PostgreSQL 11+) +# EN: Check if pg_dump supports connection string (PostgreSQL 11+) +# VI: Kiểm tra xem pg_dump có hỗ trợ connection string không (PostgreSQL 11+) if pg_dump --version | grep -qE "1[1-9]|2[0-9]"; then - # Use connection string directly (supports Neon) + # EN: Use connection string directly (supports Neon) + # VI: Sử dụng connection string trực tiếp (hỗ trợ Neon) pg_dump "$DATABASE_URL" > "../$BACKUP_FILE" else - # Fallback: Parse connection string for older pg_dump - # Remove query parameters for parsing + # EN: Fallback: Parse connection string for older pg_dump + # VI: Dự phòng: Phân tích connection string cho bản pg_dump cũ hơn + # EN: Remove query parameters for parsing + # VI: Loại bỏ các tham số query để phân tích CLEAN_URL=$(echo $DATABASE_URL | sed 's/?.*//') DB_USER=$(echo $CLEAN_URL | sed -n 's/.*:\/\/\([^:]*\):.*/\1/p') DB_PASS=$(echo $CLEAN_URL | sed -n 's/.*:\/\/[^:]*:\([^@]*\)@.*/\1/p') diff --git a/scripts/db/seed.sh b/scripts/db/seed.sh index 352f96c5..92a81dd4 100755 --- a/scripts/db/seed.sh +++ b/scripts/db/seed.sh @@ -4,12 +4,16 @@ set -e SERVICE=$1 +# EN: Validate argument +# VI: Xác thực tham số if [ -z "$SERVICE" ]; then echo "Usage: $0 " echo "Example: $0 auth-service" exit 1 fi +# EN: Check if service exists +# VI: Kiểm tra xem service có tồn tại không if [ ! -d "services/$SERVICE" ]; then echo "❌ Service $SERVICE not found" exit 1 @@ -19,7 +23,8 @@ echo "🌱 Seeding database for $SERVICE..." cd "services/$SERVICE" -# Check if DATABASE_URL is set +# EN: Check if DATABASE_URL is set +# VI: Kiểm tra DATABASE_URL đã được thiết lập chưa if [ -z "$DATABASE_URL" ]; then if [ -f ".env" ]; then export $(grep -v '^#' .env | xargs) diff --git a/scripts/db/setup-neon.sh b/scripts/db/setup-neon.sh index a646f97d..d45245cb 100755 --- a/scripts/db/setup-neon.sh +++ b/scripts/db/setup-neon.sh @@ -13,7 +13,8 @@ NC='\033[0m' # No Color echo -e "${YELLOW}This script helps you set up Neon database for all environments.${NC}" echo "" -# Check if .env.local exists +# EN: Check if .env.local exists +# VI: Kiểm tra xem .env.local có tồn tại không ENV_LOCAL="deployments/local/.env.local" if [ ! -f "$ENV_LOCAL" ]; then echo "📝 Creating $ENV_LOCAL from example..." @@ -36,7 +37,8 @@ echo "" read -p "Enter development DATABASE_URL (main branch): " dev_url if [ -n "$dev_url" ]; then - # Update .env.local + # 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" diff --git a/scripts/deploy/deploy-prod.sh b/scripts/deploy/deploy-prod.sh index 123d8c5a..408c5391 100755 --- a/scripts/deploy/deploy-prod.sh +++ b/scripts/deploy/deploy-prod.sh @@ -2,9 +2,13 @@ set -e +# EN: Warn user about production deployment +# VI: Cảnh báo người dùng về việc deploy lên production echo "⚠️ WARNING: You are about to deploy to PRODUCTION!" read -p "Are you sure? (yes/no): " confirm +# EN: Check user confirmation +# VI: Kiểm tra xác nhận của người dùng if [ "$confirm" != "yes" ]; then echo "Deployment cancelled" exit 1 @@ -12,11 +16,15 @@ fi echo "🚀 Deploying to production..." +# EN: Verify KUBECONFIG environment variable is set +# VI: Xác minh biến môi trường KUBECONFIG đã được thiết lập if [ -z "$KUBECONFIG" ]; then echo "❌ KUBECONFIG environment variable not set" exit 1 fi +# EN: Apply Kubernetes configurations and wait for rollout +# VI: Áp dụng cấu hình Kubernetes và đợi quá trình rollout hoàn tất kubectl apply -f deployments/production/kubernetes/ kubectl rollout status deployment/auth-service -n production diff --git a/scripts/deploy/deploy-staging.sh b/scripts/deploy/deploy-staging.sh index 5022a375..de91ebff 100755 --- a/scripts/deploy/deploy-staging.sh +++ b/scripts/deploy/deploy-staging.sh @@ -4,11 +4,15 @@ set -e echo "🚀 Deploying to staging..." +# EN: Verify KUBECONFIG environment variable is set +# VI: Xác minh biến môi trường KUBECONFIG đã được thiết lập if [ -z "$KUBECONFIG" ]; then echo "❌ KUBECONFIG environment variable not set" exit 1 fi +# EN: Apply Kubernetes configurations and wait for rollout +# VI: Áp dụng cấu hình Kubernetes và đợi quá trình rollout hoàn tất kubectl apply -f deployments/staging/kubernetes/ kubectl rollout status deployment/auth-service -n staging diff --git a/scripts/dev/logs.sh b/scripts/dev/logs.sh index 6326e107..da7a7ad6 100755 --- a/scripts/dev/logs.sh +++ b/scripts/dev/logs.sh @@ -2,11 +2,13 @@ SERVICE=$1 +# EN: Check usage +# VI: Kiểm tra cách sử dụng if [ -z "$SERVICE" ]; then - echo "Usage: $0 " + echo "Usage: $0 / Cách dùng: $0 " echo "Example: $0 auth-service" echo "" - echo "Or use 'docker' to view Docker logs:" + echo "Or use 'docker' to view Docker logs: / Hoặc dùng 'docker' để xem log Docker:" echo " $0 docker " exit 1 fi @@ -15,12 +17,16 @@ if [ "$SERVICE" = "docker" ]; then CONTAINER=$2 if [ -z "$CONTAINER" ]; then echo "Usage: $0 docker " - echo "Available containers:" + echo "Available containers: / Các container khả dụng:" docker ps --format "{{.Names}}" exit 1 fi + # EN: Follow docker logs + # VI: Theo dõi log docker docker logs -f "$CONTAINER" else + # EN: Start development logs using pnpm + # VI: Bắt đầu log phát triển sử dụng pnpm cd "services/$SERVICE" pnpm dev fi diff --git a/scripts/dev/setup-env.sh b/scripts/dev/setup-env.sh index f1bd9bfd..3d203667 100755 --- a/scripts/dev/setup-env.sh +++ b/scripts/dev/setup-env.sh @@ -13,20 +13,22 @@ GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color -# Check if we're in the project root +# EN: Check if we're in the project root +# VI: Kiểm tra xem có đang ở root project không if [ ! -f "package.json" ]; then - echo -e "${RED}❌ Error: Must run from project root${NC}" + echo -e "${RED}❌ Error: Must run from project root / Lỗi: Phải chạy từ root project${NC}" exit 1 fi -# Setup shared environment +# EN: Setup shared environment +# VI: Thiết lập môi trường chia sẻ echo "" echo "📦 Step 1: Setup shared environment (deployments/local/.env.local)" if [ -f "deployments/local/.env.local" ]; then - echo -e "${YELLOW}⚠️ deployments/local/.env.local already exists${NC}" - read -p "Overwrite? (y/n): " overwrite + echo -e "${YELLOW}⚠️ deployments/local/.env.local already exists / đã tồn tại${NC}" + read -p "Overwrite? (y/n): / Ghi đè? (y/n): " overwrite if [ "$overwrite" != "y" ]; then - echo "Skipping shared environment setup" + echo "Skipping shared environment setup / Bỏ qua thiết lập môi trường chia sẻ" else cp deployments/local/env.local.example deployments/local/.env.local echo -e "${GREEN}✅ Created deployments/local/.env.local${NC}" @@ -36,11 +38,13 @@ else echo -e "${GREEN}✅ Created deployments/local/.env.local${NC}" fi -# Setup service-specific environments +# EN: Setup service-specific environments +# VI: Thiết lập môi trường cụ thể cho từng service echo "" echo "📦 Step 2: Setup service-specific environments" -# Function to setup service env +# EN: Function to setup service env +# VI: Hàm để setup env cho service setup_service_env() { local service=$1 local service_path="services/$service" diff --git a/scripts/dev/start-service.sh b/scripts/dev/start-service.sh index d13c90f9..c293be66 100755 --- a/scripts/dev/start-service.sh +++ b/scripts/dev/start-service.sh @@ -4,6 +4,8 @@ set -e SERVICE=$1 +# EN: Validate arguments +# VI: Xác thực tham số if [ -z "$SERVICE" ]; then echo "Usage: $0 " echo "Example: $0 auth-service" @@ -12,7 +14,8 @@ fi echo "🚀 Starting $SERVICE..." -# Check if service exists +# EN: Check if service exists +# VI: Kiểm tra xem service có tồn tại không if [ ! -d "services/$SERVICE" ]; then echo "❌ Service $SERVICE not found" exit 1 diff --git a/scripts/setup/init-project.sh b/scripts/setup/init-project.sh index 03fb7a75..2056b9d5 100755 --- a/scripts/setup/init-project.sh +++ b/scripts/setup/init-project.sh @@ -4,7 +4,8 @@ set -e echo "🚀 Initializing GoodGo Microservices Project..." -# Check prerequisites +# EN: Check prerequisites +# VI: Kiểm tra các điều kiện tiên quyết if ! command -v pnpm &> /dev/null; then echo "❌ PNPM is not installed. Please install it first: npm install -g pnpm" exit 1 @@ -14,17 +15,20 @@ if ! command -v docker &> /dev/null; then echo "⚠️ Docker is not installed. Some features may not work." fi -# Install dependencies +# EN: Install dependencies +# VI: Cài đặt các gói phụ thuộc echo "📦 Installing dependencies..." pnpm install -# Generate Prisma clients +# EN: Generate Prisma clients +# VI: Tạo Prisma generic clients echo "🔧 Generating Prisma clients..." cd services/auth-service pnpm prisma:generate || echo "⚠️ Prisma generation skipped (database not available)" cd ../.. -# Setup environment files +# EN: Setup environment files +# VI: Thiết lập các file biến môi trường echo "📝 Setting up environment files..." if [ ! -f "services/auth-service/.env" ]; then cp services/auth-service/env.example services/auth-service/.env diff --git a/scripts/setup/install-deps.sh b/scripts/setup/install-deps.sh index 777e12ff..4d2bdaae 100755 --- a/scripts/setup/install-deps.sh +++ b/scripts/setup/install-deps.sh @@ -4,6 +4,8 @@ set -e echo "📦 Installing all dependencies..." +# EN: Install dependencies using pnpm +# VI: Cài đặt dependencies sử dụng pnpm pnpm install echo "✅ Dependencies installed!" diff --git a/scripts/utils/cleanup.sh b/scripts/utils/cleanup.sh index eec9e2a6..fbccf056 100755 --- a/scripts/utils/cleanup.sh +++ b/scripts/utils/cleanup.sh @@ -4,19 +4,23 @@ set -e echo "🧹 Cleaning up temporary files..." -# Remove node_modules (optional, comment out if you want to keep them) +# EN: Remove node_modules (optional, comment out if you want to keep them) +# VI: Xóa node_modules (tùy chọn, comment lại nếu muốn giữ) # find . -name "node_modules" -type d -prune -exec rm -rf {} \; -# Remove build artifacts +# EN: Remove build artifacts +# VI: Xóa các file build find . -name "dist" -type d -prune -exec rm -rf {} \; find . -name ".next" -type d -prune -exec rm -rf {} \; find . -name "build" -type d -prune -exec rm -rf {} \; find . -name "*.tsbuildinfo" -type f -delete -# Remove logs +# EN: Remove logs +# VI: Xóa file logs find . -name "*.log" -type f -delete -# Remove cache +# EN: Remove cache +# VI: Xóa cache find . -name ".turbo" -type d -prune -exec rm -rf {} \; find . -name ".cache" -type d -prune -exec rm -rf {} \; diff --git a/scripts/utils/create-service.sh b/scripts/utils/create-service.sh index f03a3ab8..89515160 100755 --- a/scripts/utils/create-service.sh +++ b/scripts/utils/create-service.sh @@ -4,6 +4,8 @@ set -e SERVICE_NAME=$1 +# EN: Validate arguments +# VI: Xác thực tham số if [ -z "$SERVICE_NAME" ]; then echo "Usage: $0 " echo "Example: $0 payment-service" @@ -12,6 +14,8 @@ fi SERVICE_DIR="services/$SERVICE_NAME" +# EN: Check if service directory already exists +# VI: Kiểm tra xem thư mục service đã tồn tại chưa if [ -d "$SERVICE_DIR" ]; then echo "❌ Service $SERVICE_NAME already exists" exit 1 @@ -19,19 +23,23 @@ fi echo "📦 Creating new service: $SERVICE_NAME..." -# Copy template +# EN: Copy template +# VI: Copy template cp -r services/_template "$SERVICE_DIR" -# Update package.json +# 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 -# Update .env.example +# 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 -# Update Dockerfile port if needed +# EN: Update Dockerfile port if needed +# VI: Cập nhật port Dockerfile nếu cần # (Keep default 5000, user can change later) cd ../..