Recipe - Production API Blueprint
End-to-end Kern production API setup with middleware order, timeouts, health endpoints, and graceful shutdown.
Production API Blueprint
Use this blueprint as a baseline for production services.
You can scaffold a project matching this pattern with:
kern-cli new myapi -template rest-apiGoals
- Stable request lifecycle under load
- Predictable error and timeout behavior
- Easy incident debugging with request correlation
Recommended middleware order
app := kern.New()
app.Use(middleware.RequestID())
app.Use(kern.Logger(kern.LoggerConfig{Format: "json"}))
app.Use(kern.Recovery())
app.Use(kern.CORS([]string{"https://app.example.com"}))
app.Use(middleware.SecurityHeaders())
app.Use(middleware.Timeout(middleware.TimeoutConfig{Duration: 2 * time.Second}))Route layering
api := app.Group("/api")
api.Use(middleware.RateLimiter())
api.GET("/health", healthHandler)
api.GET("/ready", readyHandler)Run options
app.Run(":8080",
kern.WithReadTimeout(5*time.Second),
kern.WithWriteTimeout(10*time.Second),
kern.WithGracefulShutdown(10*time.Second),
)Checklist
- Request ID appears in response and logs
- Panic path returns consistent
500JSON - Health and readiness checks are wired to dependencies
- Timeouts and graceful shutdown are verified during deploy
Recipes
Production-focused Kern recipes for Go API servers, including health checks, graceful shutdown, SPA serving, JSON errors, observability, and middleware patterns.
Recipe - CRUD REST API with Validation
Full CRUD REST API using xvalidator.BodyValidator[T] middleware for route-level decode + validate, path constraints, and an in-memory store.