This project is a URL shortener application built with Rust. It provides URL shortening functionality, redirect handling, data persistence, and error handling through a simple web form interface.
The application allows users to shorten long URLs. This is handled by the shorten_url function in the handlers/api.rs file. The function takes a URL from a web form, generates a short ID using the nanoid crate, and stores the original URL and short ID in the database.
When a user accesses a shortened URL, the application redirects them to the original URL. This is managed by the redirect_handler function in the handlers/redirect.rs file. The function looks up the original URL based on the short ID, increments the click count, and performs the redirection.
The application uses a PostgreSQL database to store URLs and their corresponding short IDs. The database connection is managed by the AppState struct in the lib.rs file, and the database operations are performed using the sqlx crate.
The application includes comprehensive error handling to manage various error scenarios, such as invalid URL formats and database errors. Custom error types are defined in the errors/mod.rs file, and these errors are converted to appropriate HTTP responses.
The application provides a simple web form interface for users to input URLs and receive shortened URLs. The form is defined in the templates/index.html file, and the form submission is handled by the index_handler function in the handlers/frontend.rs file.
The application includes a rate-limiting feature to prevent abuse and ensure fair usage. This is implemented using Redis and the bb8 crate for connection pooling.
- Request Tracking: Each incoming request is tracked using a unique identifier (e.g., IP address).
- Redis Storage: The request count is stored in Redis with an expiration time.
- Rate Limiting Logic: The application checks the request count against a predefined limit. If the limit is exceeded, the request is rejected with an appropriate error message.
- Redis Connection Pool: The
bb8crate is used to manage a pool of Redis connections, ensuring efficient and concurrent access to Redis. - Rate Limiting Middleware: A middleware component intercepts incoming requests, updates the request count in Redis, and enforces the rate limit.
The rate-limiting logic is implemented in the handlers/api.rs file: This feature helps maintain the application's performance and reliability by preventing excessive usage from any single user.
The application includes an additional feature to generate QR codes for shortened URLs, enhancing the ease of sharing and accessibility. This functionality is implemented using the to_svg_to_string function within the qrcode-generator crate located in the handlers/api.rs file. When a URL is shortened, a corresponding QR code is generated. The generated QR code is then displayed alongside the shortened URL in the web form interface.
- URL Shortening: When a user submits a URL to be shortened, the application generates a short ID for the URL.
- QR Code Generation: Simultaneously, the
to_svg_to_stringfunction creates a QR code for the shortened URL using theqrcode-generatorcrate. - Storage: The QR code image is added to the Database corresponding to the short ID.
- Display: The web form interface is updated to display the QR code image alongside the shortened URL, allowing users to scan the code with their mobile devices to access the original URL.
This feature provides a convenient way for users to share shortened URLs, especially in scenarios where typing a URL is impractical. By scanning the QR code, users can quickly and easily access the original URL on their mobile devices.
- Rust
- PostgreSQL
- Redis
-
Clone the repository:
git clone https://github.com/anomitroid/URL-Shortener-BlazinglyFast.git cd URL-Shortener-BlazinglyFast/url_app -
Install Redis server:
For Windows:
- Follow the instructions provided by Microsoft to install WSL (Windows Subsystem for Linux). The default Linux distribution installed is typically Ubuntu.
- Click on the windows button and type Ubuntu to open the Ubuntu terminal. You might need to download the Ubuntu Application from Windows Store.
- Once Ubuntu is running on Windows, add the Redis repository to the apt index, update it, and install Redis:
sudo apt-add-repository ppa:redislabs/redis sudo apt-get update sudo apt-get upgrade sudo apt-get install redis-server
- Start the Redis server:
or
sudo service redis-server start
redis-server
For Linux:
- Install Redis using your package manager:
sudo apt update sudo apt install redis-server
- Start the Redis server:
or
sudo service redis-server start
redis-server
-
Set up the PostgreSQL database:
- Install PostgreSQL from here.
- Create a new database:
psql -h localhost -p 5432 -U postgres CREATE DATABASE url_app;
-
Configure the database connection:
- Create a
.envfile in the url_app directory with the following content:DATABASE_URL=postgresql://postgres:password@localhost/url_app BASE_URL=http://localhost:3000 REDIS_URL=redis://localhost/
- Create a
-
Run database migrations:
cargo install sqlx-cli sqlx migrate run
-
Run the application:
cargo run
- Open your web browser and navigate to
http://localhost:3000. - Enter a URL in the input field and click "Shorten URL".
- The shortened URL will be displayed, and you can use it to redirect to the original URL.
By default, the application runs on port 3000. You can change this to any port you prefer by setting the PORT environment variable in the .env file.
- Open the
.envfile located in theurl_appdirectory. - Add or modify the
PORTvariable with your desired port number:PORT=8080
- Save the
.envfile and restart the application:cargo run
The application will now run on the port specified in the .env file.
url-shortener/
├── url_app/
│ ├── src/
│ │ ├── errors/
│ │ │ └── mod.rs
│ │ ├── handlers/
│ │ │ ├── api.rs
│ │ │ ├── frontend.rs
│ │ │ ├── mod.rs
│ │ │ ├── static_files.rs
│ │ │ └── redirect.rs
│ │ ├── models/
│ │ │ └── mod.rs
│ │ ├── routes/
│ │ │ └── mod.rs
│ │ ├── templates/
│ │ │ ├── mod.rs
│ │ │ └── index.html
│ │ ├── db.rs
│ │ ├── lib.rs
│ │ └── main.rs
│ ├── static/
│ │ ├── css/
│ │ │ └── styles.css
│ │ └── js/
│ │ └── app.js
│ ├── .env
│ ├── .gitignore
│ ├── Cargo.toml
│ ├── Cargo.lock
│ ├── migrations/
│ │ └── 20250201162815_create_urls_table.sql
│ ├── .cargo/
│ │ └── config.toml
├── LICENSE
└── README.md
We welcome contributions! Here are some ways you can help:
- Report bugs and request features by opening issues.
- Submit pull requests to fix bugs or add features.
- Improve documentation.
- URL shortening functionality: The
shorten_urlfunction generates a short ID for a given URL and stores it in the database. - Redirect handling: The
redirect_handlerfunction retrieves the original URL using the short ID and redirects the user. - Data persistence: URLs and their short IDs are stored in a PostgreSQL database, managed by the
AppStatestruct andsqlxcrate. - Error handling: Custom error types are defined and converted to HTTP responses to handle various error scenarios.
- Simple web form: Users can input URLs and receive shortened URLs through a web form interface.
- Used Rust idioms and patterns.
- Implemented robust error handling.
- Applied concurrency where it makes sense.
- Created clean, modular architecture.
- Managed resources efficiently.
- Kept the design scalable.