Overview
Themaintenance middleware enables maintenance mode for your application, returning a 503 Service Unavailable response with optional whitelist support for IPs and paths.
Installation
Quick Start
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
Enabled | bool | false | Enable maintenance mode |
Message | string | "Service is under maintenance" | Response message |
RetryAfter | int | 3600 | Retry-After header in seconds |
StatusCode | int | 503 | HTTP status code |
Handler | mizu.Handler | - | Custom maintenance handler |
Whitelist | []string | - | Allowed IP addresses |
WhitelistPaths | []string | - | Paths that bypass maintenance |
Check | func() bool | - | Dynamic maintenance check |
Examples
Basic Maintenance Mode
Custom Message
Custom Handler
Whitelist IPs
Whitelist Paths
Dynamic Control
Toggle Maintenance
Scheduled Maintenance
Check Function
Environment-Based
With Retry-After
JSON Response
API Reference
Response Headers
Technical Details
Implementation Architecture
The maintenance middleware is implemented with a layered approach for flexibility and performance:- Static Configuration - Uses
Optionsstruct to configure enabled state, messages, status codes, and retry headers - Dynamic Mode Control -
Modetype provides thread-safe atomic operations for runtime enable/disable toggling - Bypass Mechanisms - Implements two bypass layers:
- IP-based whitelisting via
Whitelistoption (checks X-Forwarded-For, X-Real-IP, and RemoteAddr) - Path-based whitelisting via
WhitelistPathsoption (exact path matching)
- IP-based whitelisting via
- Custom Check Function - Supports dynamic maintenance state via
Checkfunction, which takes precedence over staticEnabledflag - Scheduled Maintenance - Uses time-based checks to automatically enable/disable maintenance during specific windows
Key Components
- Options: Configuration struct with fields for enabled state, messages, status codes, handlers, and whitelists
- Mode: Thread-safe maintenance mode controller using
atomic.Int32for concurrent read/write operations - WithOptions: Core middleware factory that processes configuration and returns the middleware function
- ScheduledMaintenance: Helper function that creates time-windowed maintenance middleware
- getClientIP: Helper function to extract client IP from various headers (X-Forwarded-For, X-Real-IP, RemoteAddr)
- itoa: Custom integer-to-string conversion for Retry-After header (avoids fmt package overhead)
Execution Flow
- Check if maintenance is enabled (via
Checkfunction orEnabledflag) - If disabled, pass request to next handler
- If enabled, check IP whitelist (bypass if matched)
- Check path whitelist (bypass if matched)
- Execute custom handler if provided, or return default 503 response with Retry-After header
Thread Safety
TheMode type uses atomic operations for thread-safe state management:
Enable()andDisable()useatomic.StoreInt32IsEnabled()usesatomic.LoadInt32Toggle()usesatomic.CompareAndSwapInt32for lock-free toggling
Best Practices
- Whitelist health check endpoints for monitoring
- Use scheduled maintenance for planned downtime
- Whitelist admin IPs for debugging
- Set appropriate Retry-After values
- Provide helpful maintenance messages
- Use dynamic control for emergency maintenance
Testing
The maintenance middleware includes comprehensive test coverage for all scenarios:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew | Basic maintenance mode enabled | Returns 503 with default message “Service is under maintenance” |
TestNew_Disabled | Maintenance mode disabled | Returns 200 and passes through to handler |
TestWithOptions_CustomMessage | Custom maintenance message | Returns 503 with custom message “We’ll be back soon!” |
TestWithOptions_RetryAfter | Custom retry-after value | Sets Retry-After header to 7200 seconds |
TestWithOptions_Whitelist (whitelisted IP) | IP in whitelist accesses endpoint | Returns 200 and bypasses maintenance mode |
TestWithOptions_Whitelist (non-whitelisted IP) | IP not in whitelist accesses endpoint | Returns 503 in maintenance mode |
TestWithOptions_WhitelistPaths (whitelisted path) | Access to /health whitelisted path | Returns 200 and bypasses maintenance mode |
TestWithOptions_WhitelistPaths (non-whitelisted path) | Access to /api non-whitelisted path | Returns 503 in maintenance mode |
TestWithOptions_CustomHandler | Custom handler for maintenance response | Returns JSON response with custom handler |
TestMode | Mode creation, enable, disable, toggle | Correctly changes state: disabled by default, enabled after Enable(), disabled after Disable(), enabled after Toggle() |
TestMode_Middleware (disabled) | Middleware with Mode disabled | Returns 200 and passes through |
TestMode_Middleware (enabled) | Middleware with Mode enabled | Returns 503 in maintenance mode |
TestScheduledMaintenance | Scheduled maintenance in the past | Returns 200 as maintenance period has ended |