A simple URL shortener service built with FastAPI and MongoDB. This application allows users to shorten URLs and retrieve the original URL using a short identifier.
- Shorten URLs
- Redirect to original URL using a short identifier
- Health check endpoint
-
Clone the repository:
git clone https://github.com/yourusername/url-shortener.git cd url-shortener
-
Create a virtual environment and activate it:
python3 -m venv venv source venv/bin/activate # On Windows use: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Create a
.env
file in the root directory and add your environment variables:DATABASE_URL=<your-db-url> DATABASE_NAME=url_shortener_db PORT=8000
-
Run the FastAPI application:
python main.py
-
Access the API:
- Health Check:
GET /health-check/
- Check the status of the service. - Shorten URL:
POST /shorten/
- Request body should includeoriginal_url
. - Redirect URL:
GET /{short_url}
- Redirects to the original URL.
- Health Check:
-
Docker:
Build and run the Docker container:
docker build -t url-shortener . docker run -p 8000:8000 --env-file .env url-shortener
Logs are configured to be written to a file named with the current date and rotated daily. Older logs are archived in zip files.
Feel free to open issues and pull requests to contribute to this project.
This project is licensed under the MIT License - see the LICENSE file for details.