Rust-URL-Shortening

๐Ÿฆ€ Rust URL Shortener Service

[![Rust Version](https://img.shields.io/badge/Rust-1.56+-orange.svg?logo=rust)](https://www.rust-lang.org) [![CI](https://img.shields.io/github/workflow/status/UNC-GDSC/Rust-URL-Shortening/CI?logo=github)](https://github.com/UNC-GDSC/Rust-URL-Shortening/actions) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![PostgreSQL](https://img.shields.io/badge/Database-PostgreSQL-blue.svg?logo=postgresql&logoColor=white)](#) [![MySQL](https://img.shields.io/badge/Database-MySQL-4479A1.svg?logo=mysql&logoColor=white)](#) [![SQLite](https://img.shields.io/badge/Database-SQLite-lightgrey.svg?logo=sqlite&logoColor=blue)](#) [![Docker](https://img.shields.io/badge/Docker-Ready-2496ED.svg?logo=docker&logoColor=white)](#docker-deployment) **A blazingly fast, production-ready URL shortening service built entirely in Rust** [Features](#features) โ€ข [Quick Start](#quick-start) โ€ข [Documentation](#documentation) โ€ข [Contributing](#contributing)

๐Ÿ“‹ Overview

A full-stack URL shortening service built with Rust, leveraging Actix-web for high performance and Diesel ORM for type-safe database operations. Perfect for learning Rust web development or deploying a production URL shortener.

Key Capabilities


โœจ Features


๐ŸŽฏ Why Rust?

Feature Benefit
โšก Performance Near C/C++ speed with zero-cost abstractions
๐Ÿ”’ Memory Safety No garbage collector, no data races
๐Ÿ”„ Concurrency Fearless concurrency without data races
๐Ÿ› ๏ธ Modern Tooling Cargo, rustfmt, clippy for productive development
๐Ÿ› Reliability Catch bugs at compile time, not runtime

๐Ÿ› ๏ธ Technology Stack

Component Technology Purpose
Web Framework Actix-web 4.x High-performance async HTTP server
ORM Diesel 2.x Type-safe SQL query builder
Database SQLite / PostgreSQL / MySQL Flexible data storage
Async Runtime Tokio Asynchronous runtime
Serialization Serde JSON serialization/deserialization
Environment dotenvy Environment variable management
Utilities rand, chrono, uuid Various utilities

๐Ÿ“ Project Structure

rust-url-shortener/
โ”œโ”€โ”€ .github/
โ”‚   โ””โ”€โ”€ workflows/          # CI/CD pipelines
โ”‚       โ”œโ”€โ”€ ci.yml
โ”‚       โ””โ”€โ”€ docker.yml
โ”œโ”€โ”€ docs/                   # Documentation
โ”‚   โ”œโ”€โ”€ API.md
โ”‚   โ”œโ”€โ”€ ARCHITECTURE.md
โ”‚   โ””โ”€โ”€ DEPLOYMENT.md
โ”œโ”€โ”€ examples/               # Usage examples
โ”‚   โ”œโ”€โ”€ basic_usage.rs
โ”‚   โ””โ”€โ”€ README.md
โ”œโ”€โ”€ migrations/             # Database migrations
โ”‚   โ”œโ”€โ”€ 20230310123456_create_urls/
โ”‚   โ”œโ”€โ”€ 20230310123567_create_usage_logs/
โ”‚   โ”œโ”€โ”€ 20230310123678_create_redirect_stats/
โ”‚   โ””โ”€โ”€ 20230310123789_add_expiration_date_to_urls/
โ”œโ”€โ”€ src/                    # Source code
โ”‚   โ”œโ”€โ”€ main.rs            # Application entry point
โ”‚   โ”œโ”€โ”€ lib.rs             # Library root
โ”‚   โ”œโ”€โ”€ config.rs          # Configuration
โ”‚   โ”œโ”€โ”€ db.rs              # Database connection
โ”‚   โ”œโ”€โ”€ error.rs           # Error handling
โ”‚   โ”œโ”€โ”€ handlers.rs        # Request handlers
โ”‚   โ”œโ”€โ”€ loggers.rs         # Logging setup
โ”‚   โ”œโ”€โ”€ models.rs          # Data models
โ”‚   โ”œโ”€โ”€ routes.rs          # Route definitions
โ”‚   โ”œโ”€โ”€ schema.rs          # Database schema
โ”‚   โ””โ”€โ”€ utils.rs           # Utilities
โ”œโ”€โ”€ scripts/                # Utility scripts
โ”œโ”€โ”€ tests/                  # Integration tests
โ”‚   โ””โ”€โ”€ integrationTests.rs
โ”œโ”€โ”€ .dockerignore
โ”œโ”€โ”€ .env.example           # Example environment file
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ CHANGELOG.md           # Version history
โ”œโ”€โ”€ CONTRIBUTING.md        # Contribution guidelines
โ”œโ”€โ”€ Cargo.toml             # Rust dependencies
โ”œโ”€โ”€ clippy.toml            # Linting configuration
โ”œโ”€โ”€ docker-compose.yml     # Docker Compose setup
โ”œโ”€โ”€ Dockerfile             # Docker image definition
โ”œโ”€โ”€ LICENSE                # MIT License
โ”œโ”€โ”€ Makefile               # Build automation
โ”œโ”€โ”€ README.md              # This file
โ””โ”€โ”€ rustfmt.toml           # Code formatting rules

๐Ÿš€ Quick Start

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/UNC-GDSC/Rust-URL-Shortening.git
    cd Rust-URL-Shortening
    
  2. Install Diesel CLI

    cargo install diesel_cli --no-default-features --features sqlite
    
  3. Set up environment

    cp .env.example .env
    

    Edit .env to configure your settings:

    DATABASE_URL=rust_url_shortener.db
    BASE_URL=http://localhost:8080
    RUST_LOG=info
    
  4. Run database migrations

    diesel migration run
    
  5. Build and run

    cargo run
    

    The server will start at http://localhost:8080 ๐ŸŽ‰


๐Ÿ“– API Usage

Create a Short URL

curl -X POST http://localhost:8080/ \
  -H "Content-Type: application/json" \
  -d '{"original_url": "https://example.com"}'

Response:

{
  "id": 1,
  "original_url": "https://example.com",
  "short_code": "abc123",
  "created_at": "2024-01-15T10:30:00Z",
  "expires_at": null
}

List All URLs

curl http://localhost:8080/

Use Short URL

Simply visit: http://localhost:8080/abc123

For complete API documentation, see docs/API.md


๐Ÿณ Docker Deployment

Using Docker

# Build image
docker build -t rust-url-shortener .

# Run container
docker run -d -p 8080:8080 \
  -e DATABASE_URL=rust_url_shortener.db \
  -e BASE_URL=http://localhost:8080 \
  rust-url-shortener

Using Docker Compose

docker-compose up -d

See docs/DEPLOYMENT.md for production deployment guides.


๐Ÿ“š Documentation


๐Ÿงช Testing

# Run all tests
cargo test

# Run with output
cargo test -- --nocapture

# Run integration tests
cargo test --test integrationTests

๐Ÿ”ง Development

Code Quality

# Format code
cargo fmt

# Run linter
cargo clippy

# Run checks
make lint

Using Makefile

make build      # Build project
make run        # Run application
make test       # Run tests
make fmt        # Format code
make lint       # Run clippy

๐Ÿค Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“‹ Roadmap


๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ‘ฅ Authors

UNC-CH Google Developer Student Club (GDSC)


๐Ÿ™ Acknowledgments


๐Ÿ“ž Support


**[โฌ† back to top](#-rust-url-shortener-service)** Made with โค๏ธ by UNC-GDSC