Core framework: - Kite router with full HTTP method support (GET, POST, PUT, DELETE, HEAD, OPTIONS, CONNECT, TRACE) - Nestable route groups with prefix and middleware inheritance - Global, group, and route-level middleware with onion ordering - Centralized error, not-found, and method-not-allowed handlers - Graceful shutdown on SIGTERM/SIGINT with configurable timeout - Configurable server read, write, and idle timeouts - Context with typed request/response helpers, value store, cookie, form, and body binding support - Response writer wrapper for status code tracking Middleware: - Logger with configurable output, format, and skip function - Recovery with configurable panic handler - RequestID with configurable header and generator, forwards incoming IDs - CORS with configurable origins, methods, headers, credentials, and max age - MaxBodySize with configurable byte limit Docs: - README with quickstart, routing, middleware, context API reference, and TLS guide
38 lines
836 B
Go
38 lines
836 B
Go
package middleware
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"runtime/debug"
|
|
|
|
"git.trcreatives.at/trcreatives/go-kite"
|
|
)
|
|
|
|
type RecoveryConfig struct {
|
|
OnPanic func(ctx *kite.Context, recovered any, stack []byte) error
|
|
}
|
|
|
|
func Recovery() kite.Middleware {
|
|
return RecoveryWithConfig(RecoveryConfig{})
|
|
}
|
|
|
|
func RecoveryWithConfig(cfg RecoveryConfig) kite.Middleware {
|
|
if cfg.OnPanic == nil {
|
|
cfg.OnPanic = func(ctx *kite.Context, recovered any, stack []byte) error {
|
|
log.Printf("[Kite] Panic: %v\n%s", recovered, stack)
|
|
return ctx.WriteString(http.StatusInternalServerError, "500 internal server error")
|
|
}
|
|
}
|
|
|
|
return func(h kite.Handler) kite.Handler {
|
|
return func(ctx *kite.Context) (err error) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
err = cfg.OnPanic(ctx, r, debug.Stack())
|
|
}
|
|
}()
|
|
return h(ctx)
|
|
}
|
|
}
|
|
}
|