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
47 lines
847 B
Go
47 lines
847 B
Go
package middleware
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
|
|
"git.trcreatives.at/trcreatives/go-kite"
|
|
)
|
|
|
|
type RequestIDConfig struct {
|
|
Header string
|
|
Generator func() string
|
|
}
|
|
|
|
func RequestID() kite.Middleware {
|
|
return RequestIDWithConfig(RequestIDConfig{})
|
|
}
|
|
|
|
func RequestIDWithConfig(cfg RequestIDConfig) kite.Middleware {
|
|
if cfg.Header == "" {
|
|
cfg.Header = "X-Request-ID"
|
|
}
|
|
|
|
if cfg.Generator == nil {
|
|
cfg.Generator = func() string {
|
|
b := make([]byte, 16)
|
|
rand.Read(b)
|
|
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
|
|
b[0:4], b[4:6], b[6:8], b[8:10], b[10:16])
|
|
}
|
|
}
|
|
|
|
return func(h kite.Handler) kite.Handler {
|
|
return func(ctx *kite.Context) error {
|
|
id := ctx.GetHeader(cfg.Header)
|
|
if id == "" {
|
|
id = cfg.Generator()
|
|
}
|
|
|
|
ctx.SetHeader(cfg.Header, id)
|
|
ctx.SetValue(cfg.Header, id)
|
|
|
|
return h(ctx)
|
|
}
|
|
}
|
|
}
|