Skip to content

Commit dcebeb4

Browse files
committed
updated caddyfile
1 parent 13d1b26 commit dcebeb4

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Caddyfile

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Caddyfile for StringSight API
2+
# Optimized for Vercel → Digital Ocean connections
3+
# Deploy this to your droplet at /etc/caddy/Caddyfile
4+
5+
api.stringsight.com {
6+
# Reverse proxy to your uvicorn backend
7+
reverse_proxy localhost:8000 {
8+
# Connection pooling - keeps connections alive
9+
transport http {
10+
keepalive 90s
11+
keepalive_idle_conns 10
12+
}
13+
14+
# Health check to verify backend is up
15+
health_uri /health
16+
health_interval 30s
17+
health_timeout 5s
18+
}
19+
20+
# CORS configuration for Vercel frontend
21+
@cors_preflight {
22+
method OPTIONS
23+
}
24+
25+
# Handle CORS preflight quickly (this is KEY for performance!)
26+
handle @cors_preflight {
27+
header Access-Control-Allow-Origin "https://stringsight.vercel.app"
28+
header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
29+
header Access-Control-Allow-Headers "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, Authorization"
30+
header Access-Control-Max-Age "86400" # Cache preflight for 24 hours
31+
respond 204
32+
}
33+
34+
# CORS headers for all responses
35+
header Access-Control-Allow-Origin "https://stringsight.vercel.app"
36+
header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
37+
header Access-Control-Allow-Headers "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, Authorization"
38+
header Access-Control-Expose-Headers "Content-Length, Content-Range"
39+
header Access-Control-Allow-Credentials "true"
40+
41+
# Increase request size limit for file uploads
42+
request_body {
43+
max_size 100MB
44+
}
45+
46+
# Enable compression for API responses
47+
encode gzip
48+
49+
# Logging
50+
log {
51+
output file /var/log/caddy/stringsight.log
52+
format json
53+
level INFO
54+
}
55+
}
56+
57+
# Optional: If you want to serve frontend from the same domain
58+
# www.stringsight.com, stringsight.com {
59+
# root * /var/www/stringsight
60+
# file_server
61+
# try_files {path} /index.html
62+
# }

stringsight/database.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ def _create_engine(database_url: str) -> Engine:
3434
connect_args={"check_same_thread": False},
3535
)
3636

37+
# For PostgreSQL, use connection pooling to reduce cold start time
38+
if database_url.startswith("postgresql://"):
39+
return create_engine(
40+
database_url,
41+
pool_size=5, # Keep 5 connections alive
42+
max_overflow=10, # Allow up to 10 additional connections during spikes
43+
pool_pre_ping=True, # Verify connections before use (prevents stale connections)
44+
pool_recycle=3600, # Recycle connections after 1 hour
45+
)
46+
3747
return create_engine(database_url)
3848

3949

0 commit comments

Comments
 (0)