A simple URL shortener service built with Rust, Actix-web, and Redis. This API allows you to shorten long URLs and redirect users to the original URLs using short, user-friendly links.
- Shorten URLs: Convert long URLs into short, user-friendly URLs.
- Redirect to Original URL: When a short URL is accessed, users are redirected to the original URL.
- Redis Backend: URL mappings are stored in Redis for fast access.
- Random Short URL Generation: Short URLs are created using a 6-character random string.
- Rate Limiting: Protect the API from abuse with basic rate limiting.
- Rust (v1.68.0 or later)
- Actix-web: A powerful web framework for Rust.
- Redis: An in-memory key-value store used for fast URL storage and retrieval.
- Serde: For serializing and deserializing JSON data.
- Rand: A crate used for generating random short URL strings.
- Install Rust (version 1.68.0 or later).
- Install Redis and ensure it is running locally on port
6379
. - Ensure your project is set up with a Redis connection.
-
Clone the repository:
git clone https://github.com/yourusername/url-shortener.git cd url-shortener
-
Install dependencies:
cargo build
-
Start Redis Server: Make sure Redis is installed and running locally:
redis-server
-
Run the application:
cargo run
The server will be running on
http://localhost:1025
.
- Description: Shortens a long URL.
- Request Body:
{ "original_url": "https://www.example.com" }
- Response:
{ "short_url": "http://localhost:1025/api/qzpxlt" }
- Description: Redirects the user to the original URL associated with the short URL.
- Path Parameter:
short_url
: The short URL code (e.g.,qzpxlt
).
- Response: A 302 Redirect to the original URL.
- Example: Redirects to
https://www.example.com
.
- Example: Redirects to
-
Redis Client Initialization (
database/mod.rs
):
The Redis client is initialized and wrapped in aMutex
andArc
for safe concurrent access. -
URL Models (
models/url.rs
):
Defines the request and response models for handling URLs. TheUrlRequest
model takes the original URL, while theUrlResponse
model returns the shortened URL. -
Shorten URL Route (
routes/shorten.rs
):
Defines the logic for shortening a URL. When a POST request is made to/shorten
, it generates a short URL, stores it in Redis, and returns the shortened URL. -
Redirect URL Route (
routes/redirect.rs
):
Handles the redirection logic. When a GET request is made to a short URL, the system looks up the corresponding original URL in Redis and redirects the user. -
Utility for URL Generation (
utils.rs
):
Generates a 6-character random short URL string. -
Main Server Setup (
main.rs
):
Initializes and runs the Actix-web server, binds the routes, and connects to Redis.
This project is open-source and available under the MIT License.