Documentation Index
Fetch the complete documentation index at: https://docs.go-mizu.dev/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Theversion middleware provides API versioning support through headers, query parameters, or URL path prefixes. It helps manage multiple API versions and handles deprecation warnings.
Installation
Quick Start
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
DefaultVersion | string | - | Default version when not specified |
Header | string | "Accept-Version" | Header name for version |
QueryParam | string | "version" | Query parameter name |
PathPrefix | bool | false | Extract version from URL path |
Supported | []string | - | List of supported versions |
Deprecated | []string | - | List of deprecated versions |
ErrorHandler | func(*mizu.Ctx, string) error | - | Custom error handler |
Examples
Version from Header
Version from Query Parameter
GET /users?api_version=v2
Version from URL Path
Supported Versions Only
Deprecation Warnings
Using Helper Functions
Version-Specific Routes
Content Negotiation
Feature Flags by Version
API Reference
Version Detection Order
When multiple sources are configured:- Header (e.g.,
Accept-Version: v2) - Query parameter (e.g.,
?version=v2) - Path prefix (e.g.,
/v2/users) - Default version
Response Headers
For deprecated versions:Technical Details
Architecture
The version middleware uses a context-based approach to store and retrieve version information:- Version Detection: The middleware examines multiple sources in a specific priority order
- Context Storage: Detected version is stored in the request context using a private context key
- Validation: Optional validation against supported/deprecated version lists
- Header Injection: Automatic deprecation headers for deprecated versions
Implementation Details
Version Detection Priority:- HTTP Header (configurable, default:
Accept-Version) - Query Parameter (configurable, default:
version) - Path Prefix (optional, pattern:
/v{number}/...) - Default Version (fallback)
- Pattern:
vorVfollowed by digits and optional dots - Examples:
v1,v2,V1,v1.0,v1.2.3 - Invalid:
api, empty string,v,va
- Uses a private
contextKey{}struct to prevent collisions - Version stored as string in request context
- Retrieved via
GetVersion(c)orGet(c)helper functions
- Supported and deprecated versions stored in maps for O(1) lookup
- Single pass through version sources with early exit
- No regex matching for version strings (character-by-character validation)
Response Headers
For deprecated versions, the middleware automatically adds:Deprecation: true- Indicates the version is deprecatedSunset: See documentation for migration guide- Migration information
Best Practices
- Always set a default version
- Document supported versions
- Add deprecation warnings before removal
- Use semantic versioning (v1, v2, v3)
- Maintain backward compatibility within major versions
- Provide migration guides for deprecated versions
Testing
The middleware includes comprehensive test coverage for all features:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew/from header | Version from Accept-Version header | Extracts version “v2” from header |
TestNew/from query | Version from query parameter | Extracts version “v3” from ?version=v3 |
TestNew/default version | No version specified | Uses default version “v1” |
TestNew_PathPrefix/v1 | Version from path prefix /v1 | Extracts version “v1” from URL path |
TestNew_PathPrefix/v2 | Version from path prefix /v2 | Extracts version “v2” from URL path |
TestNew_Supported/supported version | Request with supported version | Returns 200 OK |
TestNew_Supported/unsupported version | Request with unsupported version | Returns 400 Bad Request |
TestNew_Deprecated | Request with deprecated version | Sets Deprecation header to “true” |
TestFromHeader | Custom header name (X-API-Version) | Extracts version “2.0” from custom header |
TestFromPath | Path with semantic version (v1.2) | Extracts version “v1.2” from path |
TestFromQuery | Custom query param (api_version) | Extracts version “3.0” from custom param |
TestGet | GetVersion vs Get function | Both functions return same value |
TestIsVersionString/v1 | Valid version string “v1” | Returns true |
TestIsVersionString/v2 | Valid version string “v2” | Returns true |
TestIsVersionString/V1 | Valid uppercase “V1” | Returns true |
TestIsVersionString/v1.0 | Valid with dot “v1.0” | Returns true |
TestIsVersionString/v1.2.3 | Valid semantic “v1.2.3” | Returns true |
TestIsVersionString/api | Invalid string “api” | Returns false |
TestIsVersionString/empty | Empty string | Returns false |
TestIsVersionString/v | Just “v” character | Returns false |
TestIsVersionString/va | Invalid “va” | Returns false |
Related Middlewares
- maintenance - Maintenance mode
- header - Custom headers