Skip to content

Commit a8039d2

Browse files
committed
FIxes
1 parent c6624d5 commit a8039d2

11 files changed

Lines changed: 285 additions & 223 deletions

File tree

‎.python-version‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

‎README.md‎

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,41 @@ uvicorn app.main:app --reload
5555

5656
That's it. The app defaults to SQLite and gracefully handles Redis being unavailable.
5757

58-
### Docker Compose (full-stack production)
58+
## Deployment
59+
60+
**Zly is built for self-hosting.** A $5–10/month VPS with Docker Compose gives you full control, persistent storage, Redis caching, and auto-HTTPS via Caddy — no platform lock-in.
61+
62+
### Recommended: VPS with Docker Compose
5963

6064
```bash
61-
# Create production env
65+
# SSH into your VPS (Ubuntu 24+)
66+
ssh root@your-server
67+
68+
# Clone and deploy
69+
git clone https://github.com/pythonplumber/zly.git
70+
cd zly
6271
cp infrastructure/.env.example .env
63-
# Edit .env with secure secrets and postgres URL
72+
# Edit .env with secure secrets
6473

65-
# Start all services
74+
# Start everything
6675
docker compose -f infrastructure/docker-compose.yml up -d
6776

6877
# Run migrations
6978
docker compose exec fastapi alembic upgrade head
7079
```
7180

72-
### Verify
81+
Full guide in **[docs/DEPLOYMENT.md](docs/DEPLOYMENT.md)**.
7382

74-
```bash
75-
curl http://localhost:8000/health
76-
# → {"status": "ok"}
83+
### Platform Guides
7784

78-
# Open http://localhost:8000/docs for interactive API docs
79-
```
85+
| Platform | Type | Database | Redis | Effort | Notes |
86+
|---|---|---|---|---|---|
87+
| **VPS / Dedicated** <br> (🥇 recommended) | Full control | PostgreSQL | ✅ Yes | Medium | Docker Compose + Caddy auto-TLS |
88+
| **Railway** | PaaS | PostgreSQL add-on | ✅ Yes | Low | Native Docker support |
89+
| **Render** | PaaS | PostgreSQL (Neon) | ❌ No | Low | Web Service + Cron Job |
90+
| **Vercel** | Serverless | Neon / Supabase | ❌ No | Medium | `api/index.py` entrypoint, free tier |
91+
92+
See the platform-specific section in **[docs/DEPLOYMENT.md](docs/DEPLOYMENT.md)** for step-by-step instructions.
8093

8194
## Configuration
8295

@@ -152,35 +165,26 @@ Interactive OpenAPI docs at `/docs` (auto-generated by FastAPI). All routes pref
152165

153166
```
154167
zly/
155-
├── app/
156-
│ ├── api/ # Route handlers (auth, links, analytics, bio, domains, ab_testing, etc.)
157-
│ ├── core/ # Config, security, dependencies, user-agent parser
168+
├── app/ # FastAPI application
169+
│ ├── api/ # Route handlers
170+
│ ├── core/ # Config, security, dependencies
158171
│ ├── models/ # SQLAlchemy ORM models
159-
│ ├── schemas/ # Pydantic request/response schemas
160-
│ ├── services/ # Business logic layer
161-
│ ├── templates/ # Jinja2 HTML templates (toon/editorial style)
162-
│ ├── db.py # SQLAlchemy engine + session
163-
│ ├── config.py # Pydantic Settings
164-
│ └── main.py # FastAPI application factory
165-
├── infrastructure/ # Docker Compose, Caddy, Dockerfile, .env.example
166-
├── migrations/ # Alembic database migrations
167-
├── tests/ # Pytest suite (112+ tests)
172+
│ ├── schemas/ # Pydantic schemas
173+
│ ├── services/ # Business logic
174+
│ ├── templates/ # Jinja2 HTML templates
175+
│ ├── db.py # Database engine
176+
│ ├── config.py # Settings
177+
│ └── main.py # App factory
178+
├── api/index.py # Vercel serverless entrypoint
179+
├── infrastructure/ # Docker, Caddy, env config
180+
├── migrations/ # Alembic migrations
181+
├── tests/ # Pytest suite (112+)
168182
├── worker/ # arq background worker
169183
├── docs/ # Deployment & architecture guides
170184
├── .github/ # CI workflow
171-
└── pyproject.toml # Project configuration & dependencies
185+
└── pyproject.toml # Project config
172186
```
173187

174-
## Deployment
175-
176-
See **[docs/DEPLOYMENT.md](docs/DEPLOYMENT.md)** for the full deployment guide covering:
177-
178-
- Production Docker Compose setup with Caddy reverse proxy
179-
- PostgreSQL and Redis configuration
180-
- SSL/TLS with auto-Let's Encrypt
181-
- Security hardening (secrets, CORS, rate limiting)
182-
- Monitoring and backup strategies
183-
184188
## Architecture
185189

186190
See **[docs/ARCHITECTURE.md](docs/ARCHITECTURE.md)** for detailed architecture documentation covering:

‎api/index.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from app.main import app

‎app/api/redirect.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async def redirect(
2929
status_code=status.HTTP_307_TEMPORARY_REDIRECT,
3030
headers={"location": cached_url},
3131
)
32-
except ConnectionError:
32+
except Exception:
3333
pass
3434

3535
link = await get_link_by_code(db, short_code)
@@ -64,7 +64,7 @@ async def redirect(
6464

6565
try:
6666
await redis.set(f"link:{short_code}", target_url)
67-
except ConnectionError:
67+
except Exception:
6868
pass
6969

7070
ip = request.client.host if request.client else "unknown"

‎app/core/dependencies.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from sqlalchemy.ext.asyncio import AsyncSession
55

66
from app.core.redis import get_redis
7-
from app.db import async_session_factory
7+
from app.db import get_session_factory
88

99

1010
async def get_db() -> AsyncGenerator[AsyncSession, None]:
11-
async with async_session_factory() as session:
11+
async with get_session_factory()() as session:
1212
try:
1313
yield session
1414
await session.commit()

‎app/db.py‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1+
from collections.abc import AsyncGenerator
2+
from typing import Optional
3+
14
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
25
from sqlalchemy.orm import DeclarativeBase
36

47
from app.config import settings
58

6-
engine = create_async_engine(settings.database_url, echo=False)
7-
async_session_factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
9+
_engine = None
10+
_session_factory = None
11+
12+
13+
def get_engine():
14+
global _engine
15+
if _engine is None:
16+
_engine = create_async_engine(settings.database_url, echo=False)
17+
return _engine
18+
19+
20+
def get_session_factory():
21+
global _session_factory
22+
if _session_factory is None:
23+
_session_factory = async_sessionmaker(get_engine(), class_=AsyncSession, expire_on_commit=False)
24+
return _session_factory
825

926

1027
class Base(DeclarativeBase):
1128
pass
1229

1330

14-
async def get_db() -> AsyncSession:
15-
async with async_session_factory() as session:
31+
async def get_db() -> AsyncGenerator[AsyncSession, None]:
32+
factory = get_session_factory()
33+
async with factory() as session:
1634
try:
1735
yield session
1836
await session.commit()

‎app/main.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ async def lifespan(app: FastAPI):
2626
)
2727

2828
app.include_router(dashboard_router)
29-
app.include_router(redirect_router)
3029
app.include_router(api_router, prefix="/api/v1")
3130

3231

3332
@app.get("/health")
3433
async def health():
3534
return {"status": "ok"}
35+
36+
37+
app.include_router(redirect_router)

0 commit comments

Comments
 (0)