Project 6: Custom HTTP Router with Middleware
A fast HTTP router using a radix trie for path matching, supporting path parameters (
/users/:id), wildcards, middleware chains, and route groups—like a mini Gin or Chi.
Quick Reference
| Attribute | Value |
|---|---|
| Primary Language | Go |
| Alternative Languages | Rust, TypeScript, Python |
| Difficulty | Level 3: Advanced |
| Time Estimate | 2-3 weeks |
| Knowledge Area | HTTP, Data Structures (Trie), API Design |
| Tooling | None (from scratch) |
| Prerequisites | Completed Projects 1-5. Understand HTTP deeply. Familiarity with tree data structures. |
What You Will Build
A fast HTTP router using a radix trie for path matching, supporting path parameters (/users/:id), wildcards, middleware chains, and route groups—like a mini Gin or Chi.
Why It Matters
This project builds core skills that appear repeatedly in real-world systems and tooling.
Core Challenges
- Radix trie for path matching → maps to tree data structures and algorithms
- Extracting path parameters → maps to string parsing and request context
- Middleware chain execution → maps to function composition and closures
- Route grouping → maps to API design and builder pattern
Key Concepts
- Radix/Patricia tries: Algorithm textbooks or online resources
- http.Handler interface: “The Go Programming Language” Ch. 7 - Donovan & Kernighan
- Middleware pattern: “Learning Go” Ch. 10 - Jon Bodner
- Context values: “Learning Go” Ch. 14 - Jon Bodner
Real-World Outcome
r := router.New()
// Middleware
r.Use(router.Logger())
r.Use(router.Recovery())
// Routes with parameters
r.GET("/users/:id", getUser)
r.POST("/users", createUser)
r.PUT("/users/:id", updateUser)
r.DELETE("/users/:id", deleteUser)
// Wildcards
r.GET("/static/*filepath", serveStatic)
// Route groups with group-specific middleware
api := r.Group("/api/v1")
api.Use(authMiddleware)
{
api.GET("/profile", getProfile)
api.GET("/settings", getSettings)
}
// Start server
http.ListenAndServe(":8080", r)
Implementation Guide
- Reproduce the simplest happy-path scenario.
- Build the smallest working version of the core feature.
- Add input validation and error handling.
- Add instrumentation/logging to confirm behavior.
- Refactor into clean modules with tests.
Milestones
- Milestone 1: Minimal working program that runs end-to-end.
- Milestone 2: Correct outputs for typical inputs.
- Milestone 3: Robust handling of edge cases.
- Milestone 4: Clean structure and documented usage.
Validation Checklist
- Output matches the real-world outcome example
- Handles invalid inputs safely
- Provides clear errors and exit codes
- Repeatable results across runs
References
- Main guide:
LEARN_GO_DEEP_DIVE.md - “The Go Programming Language” by Donovan & Kernighan