Rust

Link Shortener Service in Rust

Develop a minimalistic link-shortening microservice using the Axum framework and its routing system.

⏱️ 5h 40min
📦 17 modules
🎯 Intermediate

What You'll Build

We'll build a complete URL shortening service from scratch using Rust and the axum web framework. Starting with a bare Cargo project, we'll progressively add async runtime support, HTTP routing, CLI configuration, request handlers, shared state management, and a domain-specific code type with base62 encoding.

Along the way we'll see how axum's extractor system works, how to share mutable state safely across async handlers using Arc<RwLock<T>>, and how to implement custom Serde serialization for domain types. By the end, our service accepts URLs via a POST endpoint, generates compact short codes, stores mappings in memory, and redirects visitors back to the original destination.

Link Shortener Service in Rust cover

Learning Objectives

  • Set up an async Rust web service with axum and tokio

  • Configure runtime options with clap and environment variables

  • Implement REST endpoints for link creation and redirection

  • Manage shared mutable state with Arc<RwLock>

  • Design a domain type with custom Display and Deserialize implementations

  • Build an in-memory storage model with bidirectional HashMap lookups

Prerequisites

  • Basic Rust syntax (structs, enums, traits, modules)

  • Familiarity with async/await in Rust

  • Basic understanding of HTTP methods and status codes

Assembly Steps

1

Project Baseline

2

Async Runtime Foundation

3

Minimal HTTP Server

4

Configuration Parser

5

Health Check Endpoint

6

Link Creation Endpoint

7

Shared Application State

8

Registration Method Skeleton

9

Counter and Code Domain Type

10

Base62 Display Formatting

11

Short Link Construction

12

Handler Response Logic

13

Persistent Link Storage

14

Code Extraction and Redirect Route

15

Reverse Lookup Method

16

Redirect Response

17

URL Scheme Validation

Technologies

Rust axum URL Shortener Microservice REST API tokio Shared State Base62 Serde clap