Skip to content

A FastAPI-based URL shortener with MongoDB, Redis caching, and JWT authentication.

Notifications You must be signed in to change notification settings

Charleskojomark/fastapi-url-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”— FastAPI URL Shortener

A modern, scalable, and secure URL shortener built with FastAPI, MongoDB, Redis, and Docker. This service allows you to shorten URLs with optional user authentication, custom aliases, usage analytics, and caching for optimal performance.

πŸš€ Features

  • βœ… Shorten any valid URL
  • πŸ” Optional user authentication (OAuth2/JWT)
  • ✏️ Custom aliases for your short links
  • ⚑ Redis caching for fast redirects
  • 🐳 Dockerized for easy deployment

πŸ“¦ Tech Stack

  • FastAPI – Web framework
  • MongoDB – Database for storing URLs
  • Redis – Caching layer for performance
  • Pydantic – Data validation
  • JWT / OAuth2 – Authentication
  • Docker – Containerized app

πŸ“ Project Structure

fastapi-url-shortener/
β”‚
β”œβ”€β”€ deploy/
β”‚   β”œβ”€β”€ app/                   # Application source code
β”‚   β”‚   β”œβ”€β”€ controllers/       # Route handlers
β”‚   β”‚   β”œβ”€β”€ core/              # Configuration & DB setup
β”‚   β”‚   β”œβ”€β”€ models/            # Pydantic and MongoDB models
β”‚   β”‚   β”œβ”€β”€ routes/            # FastAPI routers
β”‚   β”‚   β”œβ”€β”€ schemas/           # Request/Response schemas
β”‚   β”‚   β”œβ”€β”€ services/          # Business logic
β”‚   β”‚   β”œβ”€β”€ utils/             # Utility functions
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── main.py            # Entry point
β”‚   β”‚
β”‚   β”œβ”€β”€ Dockerfile             # Docker build instructions
β”‚   β”œβ”€β”€ docker-compose.yml     # Multi-container setup
β”‚   └── requirements.txt       # Python dependencies
β”‚
└── env/                       # Environment variables

🏁 Getting Started

1. Clone the repository

git clone https://github.com/yourusername/fastapi-url-shortener.git
cd fastapi-url-shortener

2. Set up environment variables

Create a .env file in the env/ directory and configure the following:

SECRET_KEY=your-secret-key
ALGORITHM=HS256
MONGO_URI=mongodb://mongo:27017
REDIS_URL=redis://redis:6379
REDIS_PASSWORD=your-redis-password
ACCESS_TOKEN_EXPIRE_MINUTES=30

For Redis Cloud, use the provided URL with username and password. The REDIS_PASSWORD is required when using Redis Cloud as a service.

3. Run with Docker

Navigate to the deploy/ directory and run:

cd deploy
docker-compose up --build

The application will be available at: http://localhost:8000/docs

🐳 Docker Setup

This project uses Docker Compose to run:

  • FastAPI app on port 8000
  • MongoDB on port 27017
  • Redis on port 6379

You can inspect the full setup in deploy/docker-compose.yml.

πŸ“Œ API Endpoints

Method Endpoint Description Auth Required
POST /v1/shorten Shorten URL (unauthenticated) ❌
POST /v2/shorten Shorten URL (authenticated) βœ…
GET /{short_hash} Redirect to original URL ❌
POST /api/auth/register Register a new user ❌
POST /api/auth/login Login and get access token ❌

πŸ› οΈ Development

Running with Docker (Recommended)

The recommended way to run this application is using Docker, which takes care of all dependencies and services:

  1. Make sure Docker and Docker Compose are installed on your system
  2. Navigate to the deploy directory:
cd deploy
  1. Build and start the containers:
docker-compose up --build

This will start the FastAPI application, MongoDB, and Redis all configured to work together.

πŸ“„ License

This project is licensed under the MIT License.

πŸ‘¨β€πŸ’» Author

Charles

Built with ❀️ using FastAPI

πŸ”— Demo

https://fastapi-url-shortener-i8ag.onrender.com/redoc

About

A FastAPI-based URL shortener with MongoDB, Redis caching, and JWT authentication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published