# 08 - Application Entry Point (`app/main.py`)

Is document me hum `app/main.py` file ko line-by-line samjhenge aur dekhenge ki FastAPI application lifecycle aur automatic API documentation (`/docs`) kaise setup hoti hai.

---

## 1. What is `main.py`? (The "WHY")

`app/main.py` application ki central assembly shop hai:
- Instantiates the `FastAPI()` web app.
- Manages startup and shutdown lifespans (e.g. database disconnect).
- Mounts routers (`tenant.router`).
- Serves automatic Swagger UI documentation at `/docs`.

---

## 2. Line-by-Line Code Breakdown of `main.py`

Here is the exact code in `app/main.py`:

```python
 1: from contextlib import asynccontextmanager
 2: from fastapi import FastAPI
 3: from app.routers import tenant
 4: from app.database import close_database
 5: 
 6: @asynccontextmanager
 7: async def lifespan(app: FastAPI):
 8:     # Startup logic
 9:     yield
10:     # Shutdown logic
11:     close_database()
12: 
13: app = FastAPI(
14:     title="Universal Chatbot Widget API",
15:     version="1.0.0",
16:     description="Multi-tenant chatbot backend service",
17:     lifespan=lifespan
18: )
19: 
20: # Include routers
21: app.include_router(tenant.router)
22: 
23: @app.get("/")
24: async def root():
25:     return {"message": "Universal Chatbot Widget API running"}
```

---

### Line 6-11: Lifespan Manager (`@asynccontextmanager`)
```python
@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup logic
    yield
    # Shutdown logic
    close_database()
```
- **What is Lifespan?** Lifespan FastAPI ka async context manager feature hai jo application start aur shutdown moments handle karta hai.
- **Before `yield`:** Executes when FastAPI application boots up (e.g. warming up caches or pre-connecting DB).
- **`yield`:** Application turns active and starts serving requests.
- **After `yield`:** Executes when server shuts down. Here it calls `close_database()` to close open MongoDB connection sockets safely!

---

### Line 13-18: FastAPI Application Instance
```python
app = FastAPI(
    title="Universal Chatbot Widget API",
    version="1.0.0",
    description="Multi-tenant chatbot backend service",
    lifespan=lifespan
)
```
- Initializes the primary application instance `app`.
- `title`, `version`, `description`: Metadata displayed at top of Swagger UI (`/docs`).
- `lifespan=lifespan`: Attaches lifespan cleanup context manager.

---

### Line 21: Router Registration
```python
app.include_router(tenant.router)
```
- Mounts all endpoints registered on `tenant.router` onto the main application.
- Now `POST /api/v1/tenant/register` becomes active and reachable on the server!

---

### Line 23-25: Health Check Endpoint
```python
@app.get("/")
async def root():
    return {"message": "Universal Chatbot Widget API running"}
```
- Basic root health-check route returning simple JSON confirmation that backend server is alive and responding.

---

## 3. How Automatic Swagger Documentation (`/docs`) Works

FastAPI automatically parses Pydantic models, endpoint decorators, and docstrings to generate OpenAPI specifications.

When you start FastAPI and visit `http://localhost:8000/docs`:
- Interactive UI opens in browser.
- Displays `POST /api/v1/tenant/register`.
- Allows clicking "Try it out" to test request directly from the browser!
