This is a pet project implementing a URL shortener service using Rust. It demonstrates the CQRS pattern.
- URL Shortening: Takes a long URL and generates a shorter, unique identifier.
- URL Redirection: Redirects users from a shortened URL to the original URL.
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 thenanoidlibrary.
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 usingaxum.
target/: Cargo's output directory (executables, compiled code, etc.).
-
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/" }
- In-Memory
- PostgreSQL (Planned)
axum: For building the REST API.url: For parsing and validating URLs.tokio: For asynchronous runtime.nanoid: For generating unique IDs.
cargo run