Skip to content

Commit c90ac2d

Browse files
committed
Skip DB init in TESTING; add reset_db
Avoid initializing the SQLite DB during tests by checking the TESTING env var in app.lifespan and logging accordingly. Add scripts/reset_db.py to remove and re-create the DB file for fresh state during development/benchmarks. Update tests: conftest sets TESTING=true and test_api uses a shared client fixture instead of creating TestClient per test. Update results.json sample data (IDs and durations) as part of test/sample data refresh.
1 parent c1972ef commit c90ac2d

9 files changed

Lines changed: 199 additions & 56 deletions

File tree

DEPLOYMENT.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# CodeLens. Deployment Guide (Production)
2+
3+
Follow this guide to deploy **CodeLens. v1.0.0** to the professional cloud. This configuration uses **Vercel** for the frontend, **Render** for the backend, and **Supabase/Neon** for the PostgreSQL database.
4+
5+
---
6+
7+
## 1. 🗄️ Setup the Database (PostgreSQL)
8+
9+
Since SQLite is disk-based and will be deleted at every restart on Render/Vercel, you **must** use a managed PostgreSQL service.
10+
11+
1. **Go to [Supabase](https://supabase.com)** or [Neon](https://neon.tech).
12+
2. **Create a new Project** called "CodeLens".
13+
3. **Copy your Connection String** (it should look like `postgres://user:pass@host:5432/dbname`).
14+
4. **Important**: Keep this URL safe—it is your `DATABASE_URL`.
15+
16+
---
17+
18+
## 2. 🚀 Setup the Backend (Render)
19+
20+
Render will host your FastAPI API and your Dockerized environment.
21+
22+
1. **Go to [Render Dashboard](https://dashboard.render.com)**.
23+
2. **New -> Web Service** and connect your GitHub repository.
24+
3. **Configure**:
25+
- **Runtime**: `Docker`.
26+
- **Environment Variables**:
27+
- `DATABASE_URL`: (Paste your Supabase/Neon URL here).
28+
- `API_KEY_ENABLED`: `true` (highly recommended for production).
29+
- `API_KEY`: A strong secret password.
30+
- `APP_ENV`: `production`.
31+
4. **Deploy**: Render will automatically build the `Dockerfile` in the root and start the service.
32+
5. **Identify**: Copy your Render URL (e.g., `https://codelens-api.onrender.com`).
33+
34+
---
35+
36+
## 3. 🎨 Setup the Frontend (Vercel)
37+
38+
Vercel will host your React/Vite dashboard.
39+
40+
1. **Go to [Vercel](https://vercel.com)**.
41+
2. **Import** your `dashboard` folder (or the whole repository and set the root directory to `dashboard`).
42+
3. **Update `vercel.json`**:
43+
- Open [`dashboard/vercel.json`](file:///Users/arshverma/GitHub/open-ev-code-handler/dashboard/vercel.json).
44+
- Replace `https://YOUR_BACKEND_URL.render.com` with your **real** Render URL.
45+
4. **Deploy**: Vercel will build the React application and provide a global dashboard link.
46+
47+
---
48+
49+
## 4. 🤖 Running Remote Evaluations
50+
51+
Once deployed, you can run the benchmark script from your local machine (or any CI) against your **production** instance:
52+
53+
```bash
54+
python scripts/evaluate.py --url https://your-render-url.com --api-key YOUR_SECRET_KEY
55+
```
56+
57+
---
58+
59+
> [!CAUTION]
60+
> **Database Migrations**: When you first deploy to a new PostgreSQL instance, the tables will be empty. The first request to the API will automatically trigger `create_db_and_tables()` via the lifespan hook—no manual SQL is required.
61+
62+
> [!TIP]
63+
> **Vercel Rewrites**: The `vercel.json` rewrite rule is what allows the frontend to talk to the backend without CORS issues. Ensure the URL is exactly correct.

app.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@
4242
@asynccontextmanager
4343
async def lifespan(app: FastAPI):
4444
# Startup
45-
create_db_and_tables()
45+
if not os.getenv("TESTING"):
46+
create_db_and_tables()
47+
logger.info(f"CodeLens API started — DB at {settings.db_path}")
48+
else:
49+
logger.info("CodeLens API running in TESTING mode — DB initialization skipped")
50+
4651
cleanup_task = asyncio.create_task(cleanup_expired_episodes())
47-
logger.info(f"CodeLens API started — DB at {settings.db_path}")
4852

4953
yield
5054

codelens_env/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from functools import lru_cache
2+
from typing import Optional
23
from pydantic_settings import BaseSettings, SettingsConfigDict
34

45
class Settings(BaseSettings):
@@ -19,6 +20,7 @@ class Settings(BaseSettings):
1920
rate_limit_per_minute: int = 60 # requests per minute per IP
2021

2122
# Persistence
23+
database_url: Optional[str] = None
2224
db_path: str = "./data/codelens.db"
2325
db_echo: bool = False # Set True to log all SQL queries
2426

codelens_env/database.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77

88
def get_engine():
99
settings = get_settings()
10+
11+
if settings.database_url:
12+
# Support Render/Heroku 'postgres://' URLs by converting to 'postgresql://'
13+
url = settings.database_url
14+
if url.startswith("postgres://"):
15+
url = url.replace("postgres://", "postgresql://", 1)
16+
17+
return create_engine(
18+
url,
19+
echo=settings.db_echo,
20+
pool_pre_ping=True, # Ensure connections are alive
21+
)
22+
23+
# Fallback to local SQLite
1024
Path(settings.db_path).parent.mkdir(parents=True, exist_ok=True)
1125
return create_engine(
1226
f"sqlite:///{settings.db_path}",

dashboard/vercel.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"rewrites": [
3+
{
4+
"source": "/api/(.*)",
5+
"destination": "https://YOUR_BACKEND_URL.render.com/$1"
6+
},
7+
{
8+
"source": "/ws/(.*)",
9+
"destination": "wss://YOUR_BACKEND_URL.render.com/ws/$1"
10+
}
11+
],
12+
"framework": "vite",
13+
"buildCommand": "npm run build",
14+
"outputDirectory": "dist"
15+
}

0 commit comments

Comments
 (0)