This is a Flask-based URL shortener application designed to provide a simple and scalable solution for shortening and managing URLs. The application includes authentication, rate limiting, and a database-backed implementation for handling users and URLs.
- User Management: Create, read, update, and delete users.
- URL Management: Shorten URLs, manage aliases, and track redirect metrics.
- Authentication: Basic authentication with hashed passwords.
- Rate Limiting: Limits requests per minute, hour, and day.
- Database Support: Uses SQLite by default, easily extendable to other databases.
- Production-ready: Compatible with Gunicorn for deployment.
app/
├── dal/ # Data Access Layer
├── database/ # Database setup
├── models/ # Database models
├── resources/ # Flask-RESTful resources
├── tests/ # Test suite
├── utils/ # Utility functions
└── app.py # Main application entry point
- Works with Python 3.13.0+
- pip (Python package manager)
-
Clone the repository:
git clone https://github.com/crossy-l/url-shortener.git cd url-shortener
-
Install dependencies:
pip install -r requirements.txt
-
Initialize the database (optional):
python api.py --recreate-db
Run the application locally:
python api.py
The app will be available at http://127.0.0.1:5000
.
Run the application in production using Gunicorn:
gunicorn -w 4 -b 0.0.0.0:5000 api:gunicorn_app
-w 4
: Specifies 4 worker processes.-b 0.0.0.0:5000
: Binds to all network interfaces on port 5000.
To run the test suite, execute:
pytest app/tests/
To measure test coverage, use:
pytest --cov=app app/tests/
You can configure the application using command-line arguments or environment variables.
--recreate-db
: Recreate the database tables.--cache-dir
: Path to the cache directory (default:cache
).--sql-db-path
: Path to the SQLite database (default:database.db
).--requests-per-day
: Number of requests allowed per day (default:28800
).--requests-per-hour
: Number of requests allowed per hour (default:1200
).--requests-per-minute
: Number of requests allowed per minute (default:20
).--cache-timeout
: Cache timeout in seconds (default:86400
).
Arguments to specify host and port will follow, for now these are the prototype arguments.
python app.py --recreate-db --cache-dir /tmp/cache --sql-db-path mydb.sqlite
This project is licensed under the MIT License. See the LICENSE file for details.