Skip to content

var4yn/url-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL shortener powered by Rust.

🌐 README: English | Русский

This is a pet project implementing a URL shortener service using Rust. It demonstrates the CQRS pattern.

Functionality

  • URL Shortening: Takes a long URL and generates a shorter, unique identifier.
  • URL Redirection: Redirects users from a shortened URL to the original URL.

Architecture

The project follows a CQRS (Command Query Responsibility Segregation) architectural pattern, separating the handling of write operations (commands) from read operations (queries).

  • src/main.rs: Entry point of the application.
  • src/adapters/: Contains implementations for different data storage mechanisms. Currently, only an in-memory adapter is implemented.
    • src/adapters/inmemory/mod.rs: In-memory repository for storing URL mappings (for testing and development).
  • src/app/: Contains the application logic, separated into commands and queries.
    • src/app/commands/: Defines command handlers for write operations.
      • src/app/commands/create_short_url.rs: Implements the command to create a shortened URL.
    • src/app/query/: Defines query handlers for read operations.
      • src/app/query/get_real_url.rs: Implements the query to retrieve the original URL from a shortened URL.
  • src/depend/mod.rs: Defines the application state, holding the command and query handlers.
  • src/generator/: Contains components responsible for generating unique identifiers for shortened URLs.
    • src/generator/nanoid.rs: Implements an ID generator using the nanoid library.
  • src/ports/: Defines how the application interacts with the outside world.
    • src/ports/restapi/: Implements a REST API for the URL shortener service.
      • src/ports/restapi/router.rs: Defines the API routes using axum.
  • target/: Cargo's output directory (executables, compiled code, etc.).

Endpoints

  • POST /: Create a shortened URL. Takes a JSON body with the original URL. Returns a JSON response containing the generated short ID.

    Example Request:

    POST localhost:8080/
    
    Body:
    {
        "url": "https://google.com"
    }
    
    Result:
    {
        "id": "niLao688u9"
    }
    
  • GET /{id}: Redirect to the original URL. {id} is the shortened URL identifier. Returns a JSON response containing the original URL.

    Example Request:

    GET localhost:8080/niLao688u9
    
    Result:
    {
        "url": "https://google.com/"
    }
    

Storage Adapters

  • In-Memory
  • PostgreSQL (Planned)

Dependencies

  • axum: For building the REST API.
  • url: For parsing and validating URLs.
  • tokio: For asynchronous runtime.
  • nanoid: For generating unique IDs.

Running the Project

cargo run

About

A typical backend pet project: a URL shortener service built using the CQRS pattern.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages