go
49 lines · 8 steps
A maintenance-mode gate in Gin
A toggleable Gin middleware that returns 503 for most routes while a maintenance flag is on, with an allowlist for exceptions.
Explained by
highlit
1package middleware
2
3import (
4 "net/http"
5 "strings"
6 "sync/atomic"
7
8 "github.com/gin-gonic/gin"
9)
10
11type MaintenanceGate struct {
12 enabled atomic.Bool
13 allowlist []string
14 retryAfter string
15}
16
17func NewMaintenanceGate(allowlist []string, retryAfter string) *MaintenanceGate {
18 return &MaintenanceGate{allowlist: allowlist, retryAfter: retryAfter}
19}
20
21func (g *MaintenanceGate) SetEnabled(on bool) {
22 g.enabled.Store(on)
23}
24
25func (g *MaintenanceGate) allowed(path string) bool {
26 for _, prefix := range g.allowlist {
27 if path == prefix || strings.HasPrefix(path, prefix+"/") {
28 return true
29 }
30 }
31 return false
32}
33
34func (g *MaintenanceGate) Handler() gin.HandlerFunc {
35 return func(c *gin.Context) {
36 if !g.enabled.Load() || g.allowed(c.FullPath()) {
37 c.Next()
38 return
39 }
40
41 if g.retryAfter != "" {
42 c.Header("Retry-After", g.retryAfter)
43 }
44 c.AbortWithStatusJSON(http.StatusServiceUnavailable, gin.H{
45 "error": "service_unavailable",
46 "message": "The service is temporarily down for maintenance.",
47 })
48 }
49}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1An atomic.Bool lets you flip a middleware's behavior at runtime without locks or restarts.
- 2Returning a closure from a factory method gives middleware access to shared, mutable state.
- 3Pairing a 503 with a Retry-After header tells clients both that you're down and when to come back.
Related explainers
go
package streaming import ( "bufio"
Streaming NDJSON logs over HTTP in Go
http-streaming
channels
select
Advanced
10 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
go
package api import ( "crypto/sha256"
ETag conditional requests in Gin
http-caching
etag
conditional-requests
Intermediate
6 steps
go
func (w *Watcher) resetDebounce(d time.Duration) { if !w.timer.Stop() { select { case <-w.timer.C:
Debouncing a stream of events in Go
debounce
timers
channels
Advanced
7 steps
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 steps
rust
use std::collections::VecDeque; use std::sync::{Arc, Condvar, Mutex}; use std::time::{Duration, Instant};
Building a counting semaphore in Rust
concurrency
synchronization
condition-variable
Advanced
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-maintenance-mode-gate-in-gin-explained-go-a46f/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.