Overview
The Short-URL API requires configuration for database connections, CORS policies, authentication, and performance optimization. This guide covers all configuration aspects for deploying the API in development and production environments.MongoDB Configuration
Database Connection
The API uses MongoDB as its primary database with Motor (async driver). Configure the connection using theMONGO_URI environment variable:
The application connects to MongoDB during the FastAPI lifespan event and automatically creates required database indexes.
Database Structure
The API uses a database namedShortURL with two collections:
Stores URL mappings with short codes, passwords, and target URLs
- Index:
url_code(unique) - Documents: url_code, url_pass (hashed), url, _id
Tracks URL statistics and state
- Index:
url_code(unique) - Documents: url_code, url_hits, url_created_at, url_state
Automatic Index Creation
Indexes are created automatically on application startup (seemain.py:26-27):
CORS Configuration
The API includes CORS middleware to control cross-origin requests.Allowed Origins
Configure allowed origins using theALLOWED_ORIGINS environment variable:
The
ALLOWED_ORIGINS variable accepts a comma-separated list of domains. If not set, it defaults to * (allow all).CORS Settings
The API is configured with the following CORS settings (seemain.py:35-41):
- allow_credentials:
true- Allows cookies and authentication headers - allow_methods:
["*"]- Allows all HTTP methods - allow_headers:
["*"]- Allows all headers
Server Startup
Uvicorn Configuration
Run the API using Uvicorn with recommended production settings:Worker Processes
For production deployments, use multiple workers based on CPU cores:Health Check Endpoint
The API provides a health check endpoint at/health for monitoring database connectivity.
Usage
The health check pings the MongoDB database and returns status:Production Deployment Considerations
Security Checklist
Environment Variables
Environment Variables
- Store all secrets in environment variables, never in code
- Use a secret management service (AWS Secrets Manager, Azure Key Vault, HashiCorp Vault)
- Rotate
SECRET_KEYperiodically - Never commit
.envfiles to version control
CORS Configuration
CORS Configuration
- Set
ALLOWED_ORIGINSto specific domains only - Remove wildcard (
*) origins in production - Use HTTPS for all allowed origins
- Verify CORS settings before deployment
Database Security
Database Security
- Use strong MongoDB authentication credentials
- Enable MongoDB encryption at rest and in transit
- Use connection string with SSL/TLS (
ssl=trueparameter) - Restrict database user permissions (no admin access)
- Enable MongoDB IP whitelist for your application servers
URL Blacklist
URL Blacklist
- Configure
URL_BLACKLISTto prevent malicious domains - Include known phishing domains, malware sites, and spam sources
- Update the blacklist regularly
- Monitor for abuse patterns
Performance Tuning
1. Database Optimization
Motor (AsyncIOMotorClient) uses connection pooling by default. For high-traffic deployments, configure pool size:
Indexes on
url_code are created automatically. Monitor index performance:2. Background Tasks
The API uses FastAPI’sBackgroundTasks for non-blocking hit tracking (see main.py:238):
Hit counting runs in the background to avoid blocking redirect responses. This improves redirect latency significantly.
3. Async Operations
All database operations use async/await for optimal concurrency:Monitoring and Logging
Recommended Monitoring
- Monitor
/healthendpoint status - Track response times for redirect endpoint (
/{url_code}) - Monitor database connection pool usage
- Set up alerts for 4xx/5xx error rates
- Track MongoDB query performance
Logging Configuration
Uvicorn provides access logs by default. For production, configure structured logging:Deployment Platforms
Docker Deployment
Example Dockerfile:Cloud Platforms
The API is compatible with:- AWS: Elastic Beanstalk, ECS, Lambda (with Mangum)
- Google Cloud: Cloud Run, App Engine, GKE
- Azure: App Service, Container Instances, AKS
- Heroku: Direct deployment with Procfile
- Railway: One-click deployment
Base URL Configuration
Set the base URL for generated short links usingSURL_BASE:
short_url returned by the /create endpoint (see main.py:91):