Kern

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-api

Goals

  • Stable request lifecycle under load
  • Predictable error and timeout behavior
  • Easy incident debugging with request correlation
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 500 JSON
  • Health and readiness checks are wired to dependencies
  • Timeouts and graceful shutdown are verified during deploy