Skip to content

Repository files navigation

πŸš€ URL Shortener v2.0 - Professional Python Package

Python Flask SQLite License Version Package

Professional URL shortening service with analytics, bulk operations, and enterprise features Organized as a Python package with CLI management tools

πŸ—οΈ Architecture β€’ πŸš€ Quick Start β€’ πŸ› οΈ CLI Commands β€’ πŸ“– API Documentation β€’ πŸ§ͺ Development


πŸ—οΈ Architecture

Package Structure

The codebase has been reorganized into a professional Python package structure following Flask best practices:

Url_Shortner_python/
β”œβ”€β”€ πŸ“ url_shortener/              # Main Python package
β”‚   β”œβ”€β”€ πŸ“„ __init__.py            # Package initialization
β”‚   β”œβ”€β”€ πŸ“„ app.py                 # Flask application factory
β”‚   β”œβ”€β”€ πŸ“„ config.py              # Configuration management
β”‚   β”œβ”€β”€ πŸ“„ models.py              # Database models & operations
β”‚   β”œβ”€β”€ πŸ“„ services.py            # Business logic & services
β”‚   β”œβ”€β”€ πŸ“„ routes.py              # API routes & endpoints
β”‚   β”œβ”€β”€ πŸ“„ utils.py               # Utility functions
β”‚   β”œβ”€β”€ πŸ“ templates/             # Jinja2 templates
β”‚   └── πŸ“ static/                # Static files (CSS, JS)
β”œβ”€β”€ πŸ“ tests/                     # Test suite
β”‚   β”œβ”€β”€ πŸ“„ __init__.py
β”‚   └── πŸ“ unit/                  # Unit tests
β”‚       └── πŸ“„ test_url_shortener.py
β”œβ”€β”€ πŸ“„ run.py                     # CLI entry point
β”œβ”€β”€ πŸ“„ setup.py                   # Package setup configuration
β”œβ”€β”€ πŸ“„ requirements.txt           # Python dependencies
└── πŸ“„ README.md                  # This file

Key Components

🎯 Models (models.py)

  • URLModel: Database operations for URL shortening
  • RateLimitModel: Rate limiting management
  • SQLite database with optimized indexes
  • Comprehensive analytics tracking

βš™οΈ Services (services.py)

  • URLService: Core URL shortening business logic
  • AnalyticsService: Advanced analytics processing
  • Validation, rate limiting, and error handling
  • Separation of business logic from Flask

πŸ›£οΈ Routes (routes.py)

  • API Routes: RESTful API endpoints under /api/
  • Web Routes: HTML templates and web interface
  • Blueprint-based organization
  • Comprehensive error handling

πŸ”§ Configuration (config.py)

  • DevelopmentConfig: Development environment settings
  • ProductionConfig: Production environment settings
  • TestingConfig: Testing environment settings
  • Environment-based configuration management

🏭 App Factory (app.py)

  • create_app(): Flask application factory pattern
  • Blueprint registration
  • Logging configuration
  • Service initialization

πŸš€ Quick Start

Installation

  1. Clone and setup

    git clone https://github.com/GrandmaEJ/api.git
    cd Url_Shortner_python
  2. Install dependencies

    # Using pip
    pip install -r requirements.txt
    
    # Or using uv (recommended)
    uv add flask validators click
  3. Initialize database

    python run.py init-db
  4. Start the server

    python run.py run
  5. Access the application

    • 🌐 Web Interface: http://localhost:8398
    • πŸ”— API Base: http://localhost:8398/api
    • πŸ“Š Health Check: http://localhost:8398/health

πŸ› οΈ CLI Commands

The application provides comprehensive CLI management commands:

Server Management

# Start development server
python run.py run

# Start with custom options
python run.py run --host 0.0.0.0 --port 5000 --debug

# Use specific configuration
python run.py run --config production

Database Management

# Initialize database
python run.py init-db

# Clean up expired URLs
python run.py cleanup-expired

Testing

# Run test suite
python run.py test

Help

# View all available commands
python run.py --help

# View specific command help
python run.py run --help

πŸ“– API Documentation

Base Configuration

  • Base URL: http://localhost:8398
  • API Version: v2.0
  • Content-Type: application/json

πŸ”— Core Endpoints

1. Create Short URL (POST)

POST /api/short
Content-Type: application/json

{
    "url": "https://example.com/very/long/url",
    "custom_id": "mycustom",        # Optional
    "title": "My Website",          # Optional
    "description": "Main website",  # Optional
    "expiry_days": 30               # Optional (default: 30)
}

2. Bulk URL Creation

POST /api/urls/bulk
Content-Type: application/json

{
    "urls": [
        "https://example.com",
        {"url": "https://google.com", "custom_id": "google"},
        {"url": "https://github.com", "title": "GitHub"}
    ]
}

3. URL Analytics

GET /api/urls/<short_id>/analytics

4. List All URLs

GET /api/urls?limit=50&offset=0

5. Redirect

GET /<short_id>  # Redirects to original URL

πŸ” Health & Status Endpoints

Health Check

GET /health
Response: {"status": "healthy", "version": "2.0.0"}

API Version

GET /api/version
Response: {"version": "2.0.0", "status": "active"}

🎯 Examples

Using the Web Interface

  1. Navigate to http://localhost:8398
  2. Enter your long URL
  3. Optionally add custom ID, title, and description
  4. Click "Shorten URL"
  5. View result with copy and preview options

API Examples

cURL

# Create short URL
curl -X POST http://localhost:8398/api/short \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com", "custom_id": "example"}'

# Bulk creation
curl -X POST http://localhost:8398/api/urls/bulk \
  -H "Content-Type: application/json" \
  -d '{"urls": ["https://site1.com", "https://site2.com"]}'

Python

import requests

# Create short URL
response = requests.post('http://localhost:8398/api/short', json={
    'url': 'https://www.python.org/',
    'custom_id': 'python',
    'title': 'Python Official'
})
data = response.json()
print(f"Short link: {data['short_link']}")

πŸ§ͺ Development

Test Suite

The application includes a comprehensive test suite:

# Run all tests
python run.py test

# Run specific test file
python -m pytest tests/unit/test_url_shortener.py -v

Development Setup

  1. Clone repository

    git clone <repository-url>
    cd Url_Shortner_python
  2. Setup virtual environment

    # Using venv
    python -m venv venv
    source venv/bin/activate  # Linux/Mac
    # or venv\Scripts\activate  # Windows
    
    # Using uv (recommended)
    uv venv
    source .venv/bin/activate
  3. Install dependencies

    pip install -r requirements.txt
    # or
    uv add flask validators click
  4. Run tests

    python run.py test

Package Installation

Development Installation

# Install in development mode
pip install -e .

# Install with development dependencies
pip install -e ".[dev]"

Production Installation

# Install package
pip install url-shortener-v2

# Use CLI commands
url-shortener run --port 5000

🏭 Configuration

Environment Variables

# Database
DATABASE_PATH=url_shortener_v2.db

# Security
SECRET_KEY=your-secret-key
ADMIN_KEY=your-admin-key

# URLs
BASE_URL=https://your-domain.com
DEFAULT_EXPIRY_DAYS=30

# Rate Limiting
RATE_LIMIT_ENABLED=true
RATE_LIMIT_DEFAULT_REQUESTS=30
RATE_LIMIT_BULK_REQUESTS=10

# Logging
LOG_LEVEL=INFO
LOG_FILE=url_shortener_v2.log

Configuration Files

Development

# Default configuration
DATABASE_PATH=url_shortener_v2.db
BASE_URL=http://localhost:8398
DEBUG=True
RATE_LIMIT_ENABLED=True

Production

# Production configuration
SECRET_KEY=os.environ.get('SECRET_KEY')
BASE_URL=https://your-domain.com
DEBUG=False
RATE_LIMIT_ENABLED=True
RATE_LIMIT_DEFAULT_REQUESTS=20

πŸ“ˆ Features

✨ Core Features

  • πŸ”— Smart URL Shortening - Auto-generate or custom short URLs
  • πŸ“Š Real-time Analytics - Click tracking, browser analysis, referrer tracking
  • ⚑ Bulk Operations - Shorten multiple URLs simultaneously
  • πŸ›‘οΈ Rate Limiting - Built-in request throttling
  • 🎯 Custom Metadata - Support for titles, descriptions, and expiry
  • πŸ”„ Auto-cleanup - Automatic expired URL management

πŸ—οΈ Technical Features

  • 🏭 Application Factory - Flask factory pattern for scalability
  • πŸ“¦ Python Package - Professional package structure
  • πŸ”§ CLI Tools - Comprehensive management commands
  • πŸ§ͺ Test Suite - Unit tests with pytest
  • πŸ“‹ Configuration - Environment-based configuration
  • πŸ“ Documentation - Comprehensive API documentation
  • 🎨 Modern UI - Responsive web interface

πŸ”’ Security Features

  • URL Validation - Strict URL format and safety checking
  • Rate Limiting - Per-IP request throttling
  • Input Sanitization - XSS and injection prevention
  • Secure Headers - Security-focused HTTP headers
  • Admin Protection - Protected admin endpoints

πŸš€ Deployment

Production Deployment

Using Docker

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8398
CMD ["python", "run.py", "run", "--config", "production"]

Using SystemD

[Unit]
Description=URL Shortener v2.0
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/opt/url-shortener
ExecStart=/opt/url-shortener/venv/bin/python run.py run --config production
Restart=always

[Install]
WantedBy=multi-user.target

Environment Setup

# Production environment
export FLASK_ENV=production
export DATABASE_PATH=/var/lib/url-shortener/url_shortener_v2.db
export BASE_URL=https://your-domain.com
export SECRET_KEY=your-production-secret-key

# Run with production config
python run.py run --config production

πŸ› οΈ Troubleshooting

Common Issues

Database Issues

# Reset database
rm url_shortener_v2.db
python run.py init-db

# Check database
python run.py cleanup-expired

Permission Issues

# Fix permissions
chmod +x run.py
chmod 664 *.db *.log

Port Conflicts

# Use different port
python run.py run --port 8399

Debug Mode

# Enable debug logging
export LOG_LEVEL=DEBUG
python run.py run --debug

Health Checks

# Check application health
curl http://localhost:8398/health

# Check API version
curl http://localhost:8398/api/version

# Test database
python run.py init-db

πŸ“¦ Package Information

Installation Methods

From PyPI (when published)

pip install url-shortener-v2

From Source

git clone <repository>
cd Url_Shortner_python
pip install -e .

CLI Commands (when installed)

# Available commands
url-shortener run
url-shortener test
url-shortener init-db
url-shortener cleanup-expired

🀝 Contributing

Development Workflow

  1. Fork the repository
  2. Create feature branch: git checkout -b feature-name
  3. Make changes and add tests
  4. Run test suite: python run.py test
  5. Submit pull request

Code Standards

  • Python: Follow PEP 8
  • Testing: Add tests for new features
  • Documentation: Update README and docstrings
  • Architecture: Maintain separation of concerns

Pull Request Guidelines

  • Include comprehensive tests
  • Update documentation
  • Follow existing code style
  • Add changelog entry

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ†˜ Support

Getting Help

Community

  • ⭐ Star this repository
  • πŸ› Report bugs via GitHub Issues
  • πŸ’‘ Suggest features via GitHub Discussions
  • 🀝 Contribute by submitting pull requests

πŸ—οΈ Professional Python Package β€’ πŸš€ Enterprise Features β€’ πŸ› οΈ CLI Management

Made with ❀️ using Python, Flask, and SQLite

⬆ Back to Top

🎯 URL Shortener v2.0 - Now as a Professional Python Package!

About

url shortner website via python.

Resources

Stars

6 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages