Advanced URL shortener service with comprehensive analytics and click tracking capabilities.
- URL Shortening: Convert long URLs into short, manageable links
- Custom Short Codes: Option to create custom short codes for branded links
- Advanced Analytics: Detailed click tracking with metrics including:
- Total clicks and unique visitors
- Geographic data (country/city)
- Browser and device statistics
- Referrer tracking
- Click history over time
- Rate Limiting: Built-in protection against abuse
- RESTful API: Clean, well-documented API endpoints
- Redis Support: Optional Redis integration for improved performance
- Docker Ready: Easy deployment with Docker and Docker Compose
- Backend: FastAPI (Python)
- Database: SQLAlchemy with SQLite (easily configurable for PostgreSQL/MySQL)
- Caching: Redis (optional)
- Testing: Pytest
- Deployment: Docker & Docker Compose
-
Clone the repository
git clone https://github.com/0xReLogic/Nexus.git cd Nexus -
Install dependencies
pip install -r requirements.txt
-
Set up environment variables
cp .env.example .env # Edit .env with your configuration -
Run the application
uvicorn app.main:app --reload
The API will be available at http://localhost:8000
-
Using Docker Compose (Recommended)
docker-compose up -d
-
Using Docker only
docker build -t nexus . docker run -p 8000:8000 nexus
POST /shorten
Content-Type: application/json
{
"original_url": "https://example.com",
"custom_code": "optional-custom-code"
}
GET /{short_code}
GET /api/urls/{short_code}
GET /api/analytics/{short_code}
GET /api/urls?skip=0&limit=100
Once the application is running, visit:
- Interactive API Docs:
http://localhost:8000/docs - ReDoc Documentation:
http://localhost:8000/redoc
curl -X POST "http://localhost:8000/shorten" \
-H "Content-Type: application/json" \
-d '{"original_url": "https://github.com/username/repo"}'Response:
{
"id": 1,
"original_url": "https://github.com/username/repo",
"short_code": "abc123",
"short_url": "http://localhost:8000/abc123",
"created_at": "2024-01-01T12:00:00Z",
"click_count": 0,
"is_active": true
}curl "http://localhost:8000/api/analytics/abc123"Response:
{
"total_clicks": 42,
"unique_ips": 28,
"top_countries": [
{"country": "United States", "count": 15},
{"country": "Germany", "count": 8}
],
"top_referrers": [
{"referrer": "https://twitter.com", "count": 12},
{"referrer": "https://reddit.com", "count": 8}
],
"click_history": [
{"date": "2024-01-01", "clicks": 5},
{"date": "2024-01-02", "clicks": 12}
],
"browser_stats": [
{"browser": "Chrome 91.0", "count": 25},
{"browser": "Firefox 89.0", "count": 10}
]
}| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
Database connection string | sqlite:///./nexus.db |
REDIS_URL |
Redis connection string | redis://localhost:6379 |
SECRET_KEY |
Secret key for security | your-secret-key-here |
ALLOWED_ORIGINS |
CORS allowed origins | * |
BASE_URL |
Base URL for short links | http://localhost:8000 |
The API includes built-in rate limiting:
- Default: 10 requests per minute per IP
- Configurable: Modify in
app/middleware.py - Fallback: In-memory storage if Redis is unavailable
Run the test suite:
# Install test dependencies
pip install pytest httpx
# Run tests
pytest tests/ -vid: Primary keyoriginal_url: The original long URLshort_code: Generated short codecreated_at: Creation timestampclick_count: Number of clicksis_active: URL statuscreator_ip: IP address of creator
id: Primary keyshort_code: Reference to URLclicked_at: Click timestampip_address: Visitor IPuser_agent: Browser informationreferer: Referring websitecountry: Visitor countrycity: Visitor city
- Input validation: URL format validation
- Rate limiting: Prevents abuse
- IP tracking: For analytics and security
- CORS protection: Configurable origins
- SQL injection protection: SQLAlchemy ORM
- Redis caching: Optional Redis integration for improved performance
- Database indexing: Optimized queries with proper indexes
- Connection pooling: Efficient database connections
- Async support: FastAPI's async capabilities
-
Set up environment variables
export DATABASE_URL="postgresql://user:pass@localhost/nexus" export REDIS_URL="redis://localhost:6379" export SECRET_KEY="your-strong-secret-key" export BASE_URL="https://yourdomain.com"
-
Use production database
- PostgreSQL recommended for production
- Update
DATABASE_URLaccordingly
-
Deploy with Docker
docker-compose -f docker-compose.prod.yml up -d
The API provides a health check endpoint:
GET /
Returns service status and version information.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
This project is open source and available under the MIT License.
For questions, issues, or contributions, please open an issue on GitHub.