This repository contains a URL shortener service implemented in Rust, utilizing the Rocket web framework, SQLx for PostgreSQL database interactions, and Shuttle for deployment. The service allows users to shorten URLs, retrieve shortened versions, and redirect them while leveraging caching for performance optimization.
- URL Shortening: Submit a URL and receive a shortened version.
- Redirect Service: Use the shortened URL to be redirected to the original link.
- Caching: Caches both shortened URLs and original URLs for faster lookup.
- Database Integration: Stores URL mappings in a PostgreSQL database with automatic expiration of old links.
- Auto-Cleanup: Periodically removes expired URLs from the cache and database.
- FIFO Cache Management: Ensures the cache stays within size limits by pruning older entries.
- Rust: Install Rust from here.
- PostgreSQL: Set up a PostgreSQL database locally if you want to run the service without Shuttle.
- Shuttle: The project is designed for deployment on Shuttle, which handles the PostgreSQL connection.
If you are running the project locally without Shuttle, you’ll need to set up your environment with a proper database connection.
-
Clone the repository:
git clone https://github.com/kris007iron/url-shortener.git cd url-shortener
-
Set up the PostgreSQL database:
All you need is Docker engine running
-
Db config:
Create a table thru pgAdmin in Record struct well... structure.
-
Build and run the server(loccaly):
cargo-shuttle run
Now the service will be running locally, listening for URL shortening and redirection requests.
If you are deploying with Shuttle, the connection to the PostgreSQL database is handled automatically by Shuttle’s dependency injection. Shuttle will provide a connection pool directly to the main
function, so there is no need to set a connection string.
To deploy the project:
-
Install the Shuttle CLI if you haven't already:
cargo install shuttle-cli
-
Run the following command to deploy:
cargo-shuttle project start
or(locally as mentioned previously)
cargo-shuttle run
Shuttle automatically provisions a PostgreSQL database and injects the connection pool, so no manual database setup is required.
-
GET /: Serves the homepage (HTML).
-
GET /favicon.png: Serves the favicon image.
-
POST /: Accepts a URL and returns its shortened version. If the URL is already shortened, the same ID is returned.
Example:
curl -X POST http://localhost:8000/ -d "https://example.com"
-
GET /: Redirects to the original URL associated with the shortened
id
.
- Cached entries expire after 24 hours and are stored in
DashMap
, which allows for quick concurrent access. - Cache cleanup occurs automatically every hour, ensuring that expired entries are removed.
- The cache employs a FIFO strategy to prune older entries if the cache exceeds its maximum size.
- Expired URLs are automatically removed from the PostgreSQL database every hour via an asynchronous cleanup task.
- Rust: Core programming language.
- Rocket: Web framework for Rust.
- SQLx: Async SQL toolkit for Rust, used here with PostgreSQL.
- DashMap: Concurrent hashmap for efficient caching.
- Chrono: For date and time handling, particularly expiration dates.
- Shuttle: Handles deployment and PostgreSQL provisioning automatically.
This project is licensed under the MIT License. See the LICENSE file for details.