# Environment Setup — ধাপে ধাপে

এই guide ফলো করলে তুমি **local machine এ ঠিক আমাদের class-এর মতো** প্রতিটা backend project run করতে পারবে।

---

## 1) Tools install (একবারই)

| Tool | Version | কাজ |
|---|---|---|
| Node.js | 20.x বা 22.x | Node/Express backend |
| Python | 3.11+ | FastAPI backend |
| Java (JDK) | 21 (LTS) | Spring Boot backend |
| Maven | 3.9+ | Spring build |
| Docker Desktop | latest | DB/Redis/RabbitMQ container |
| Postman | latest | API test |
| Git | latest | version control |

> Windows-এ `winget install OpenJS.NodeJS.LTS`, macOS-এ `brew install node python@3.11 openjdk@21 maven`।

---

## 2) Repo clone + `.env` তৈরি

```bash
git clone <your-repo> myapp
cd myapp
cp public/env.example.txt .env
# .env খুলে nিজের password/secret বসাও
```

---

## 3) Databases এক command এ (Docker Compose)

প্রজেক্ট root-এ `docker-compose.yml`:

```yaml
services:
  mysql:
    image: mysql:8.4
    ports: ["3306:3306"]
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: appdb
  postgres:
    image: postgres:16
    ports: ["5432:5432"]
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: appdb
  mongo:
    image: mongo:7
    ports: ["27017:27017"]
  redis:
    image: redis:7-alpine
    ports: ["6379:6379"]
```

চালু করো: `docker compose up -d`।  ✅ Verify: `docker ps`।

---

## 4) Backend চালু (framework অনুযায়ী)

### Node/Express
```bash
npm install
npm run dev          # → http://localhost:3000
```

### Python/FastAPI
```bash
python -m venv .venv
source .venv/bin/activate         # Windows: .venv\Scripts\activate
pip install -r requirements.txt
uvicorn main:app --reload         # → http://localhost:8000
```

### Java/Spring Boot
```bash
./mvnw spring-boot:run            # → http://localhost:8080
```

---

## 5) Postman Collection Import

1. Postman খোলো → **Import** button
2. `public/postman-collection.json` file drag-drop করো
3. উপরে right-corner-এ environment dropdown → **Manage Environments** → নতুন `local` environment বানাও:
   - `baseUrl` = `http://localhost:3000`
   - `pyUrl`   = `http://localhost:8000`
   - `javaUrl` = `http://localhost:8080`
   - `gatewayUrl` = `http://localhost:4000`
   - `token`   = (login response থেকে copy)
4. যেকোনো request select করে **Send** — ✅ 200 OK আসলে setup done।

---

## 6) Frontend (React/Vite) সংযোগ

```bash
npm create vite@latest myapp-web -- --template react-ts
cd myapp-web
npm install
```

`.env.local`:
```
VITE_API_URL=http://localhost:3000
```

ব্যবহার: `fetch(`${import.meta.env.VITE_API_URL}/users`)`।

---

## 7) CORS সমস্যা হলে

Backend-এ CORS enable করো (framework অনুযায়ী):

- **Express:** `app.use(cors({ origin: process.env.CORS_ORIGIN }))`
- **FastAPI:** `app.add_middleware(CORSMiddleware, allow_origins=[origin], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])`
- **Spring:** `@CrossOrigin(origins = "${cors.origin}")` অথবা `WebMvcConfigurer.addCorsMappings(...)`।

---

## 8) Troubleshooting Cheatsheet

| Error | কারণ | সমাধান |
|---|---|---|
| `ECONNREFUSED :3306` | DB container চালু নাই | `docker compose up -d mysql` |
| `JWT malformed` | token expired / wrong header | Login আবার → Bearer header set |
| `CORS preflight failed` | server CORS নাই | উপরের section 7 |
| `port already in use` | port occupied | `.env` PORT বদলাও বা kill old process |
| `env undefined` | `.env` load হয়নি | `dotenv/config` বা `import 'dotenv/config'` |

---

সব step done হলে তুমি ১০০% আমাদের class-এর মতো একই output দেখবে ✅