A full‑stack URL shortening service built entirely in Rust using Actix‑web and Diesel with SQLite. This service allows users to:
rust-url-shortener/
├── Cargo.toml
├── .env
├── migrations/
│ └── 20230310123456_create_urls/
│ ├── up.sql
│ └── down.sql
├── src/
│ ├── main.rs
│ ├── db.rs
│ ├── models.rs
│ ├── schema.rs
│ └── routes.rs
└── README.md
cargo install diesel_cli --no-default-features --features sqlite
Clone the Repository or Create the Project Folder
You can create the file structure using the following terminal commands:
# Create project directory and initialize a new binary project
mkdir rust-url-shortener
cd rust-url-shortener
cargo init --bin
# Create directories for Diesel migrations
mkdir -p migrations/20230310123456_create_urls
# Create migration SQL files
touch migrations/20230310123456_create_urls/up.sql
touch migrations/20230310123456_create_urls/down.sql
# Create additional source files
cd src
touch db.rs models.rs schema.rs routes.rs
cd ..
Create a .env
File
In the project root, create a file named .env
with the following content (adjust values as needed):
DATABASE_URL=rust_url_shortener.db
BASE_URL=http://localhost:8080
Run Diesel Migrations
Set up the database schema by running:
diesel migration run
Build and Run the Server
cargo run
The server will start at http://localhost:8080.
API Endpoints
POST /
with JSON body:
{
"original_url": "https://example.com"
}
Returns the created URL entry with a generated short code.
List URLs:
GET /
returns a list of shortened URLs.
GET /{short_code}
redirects to the original URL if found.Changing the BASE_URL:
Update the BASE_URL
variable in your .env
file to reflect your domain (e.g., https://yourdomain.com
).
Extending Functionality:
Modify or add additional endpoints in src/routes.rs
and update models in src/models.rs
as needed.
Integrating a Frontend:
This example provides a robust backend API. You can build a frontend in your preferred technology to consume this API.
Feel free to fork this repository and submit pull requests with improvements or customizations. For issues or feature requests, please open an issue.
This project is licensed under the MIT License.
The UNC-CH Google Developer Student Club (GDSC) team.