Skip to content

Config ​

Configuration is set via functional options passed to kruda.New().

Functional Options ​

WithReadTimeout ​

go
func WithReadTimeout(d time.Duration) Option

Sets the maximum duration for reading the entire request. Default: 30s.

WithWriteTimeout ​

go
func WithWriteTimeout(d time.Duration) Option

Sets the maximum duration for writing the response. Default: 30s.

WithIdleTimeout ​

go
func WithIdleTimeout(d time.Duration) Option

Sets the maximum duration for idle keep-alive connections. Default: 120s.

WithBodyLimit ​

go
func WithBodyLimit(bytes int) Option

Sets the maximum request body size in bytes. Default: 4194304 (4 MB). Bodies exceeding this limit receive HTTP 413.

WithMaxBodySize ​

go
func WithMaxBodySize(size int) Option

Alias for WithBodyLimit.

WithHeaderLimit ​

go
func WithHeaderLimit(limit int) Option

Sets the maximum total request-header size in bytes (default 8 KB). Requests whose headers exceed this limit get HTTP 431. Raise it for clients that send large Authorization/Cookie headers (e.g. big JWTs); 0 disables the limit.

WithDevMode ​

go
func WithDevMode(enabled bool) Option

Enables development mode. When enabled:

  • Rich HTML error pages are rendered instead of JSON
  • X-Frame-Options is relaxed to SAMEORIGIN

Default: false. Also auto-detected via KRUDA_ENV=development.

WithSecureHeaders ​

go
func WithSecureHeaders() Option

Explicitly enables default security headers. Use this to make security enablement explicit in your code.

Security headers are opt-in. Use WithSecureHeaders() to enable, or WithSecurity() to enable all security features.

WithLegacySecurityHeaders ​

go
func WithLegacySecurityHeaders() Option

Restores Phase 1-4 security header defaults for backward compatibility.

WithErrorHandler ​

go
func WithErrorHandler(h func(c *Ctx, err *KrudaError)) Option

Sets a custom error handler. Receives *KrudaError (not error).

WithLogger ​

go
func WithLogger(l *slog.Logger) Option

Sets a custom structured logger.

WithTransport ​

go
func WithTransport(t transport.Transport) Option

Sets a custom transport implementation. With WithTLS, App.Listen requires the transport to implement transport.TLSServer; App.Serve continues to use the caller-supplied listener as-is.

FastHTTP ​

go
func FastHTTP() Option

Selects the fasthttp transport. On Linux the default is Wing (epoll+eventfd) which is faster — use FastHTTP() only if you need fasthttp compatibility. On macOS, fasthttp is already the default.

NetHTTP ​

go
func NetHTTP() Option

Selects the net/http transport for HTTP/2, TLS, and Windows compatibility.

WithTLS ​

go
func WithTLS(certFile, keyFile string) Option

Enables TLS with the given certificate and key files.

WithTrustProxy ​

go
func WithTrustProxy(trust bool) Option

When true, trusts X-Forwarded-For / X-Real-IP headers. Default: false.

WithShutdownTimeout ​

go
func WithShutdownTimeout(d time.Duration) Option

Sets the graceful shutdown timeout. Default: 10s.

WithJSONEncoder / WithJSONDecoder ​

go
func WithJSONEncoder(enc func(v any) ([]byte, error)) Option
func WithJSONDecoder(dec func(data []byte, v any) error) Option

Override the JSON encoder/decoder.

WithEnvPrefix ​

go
func WithEnvPrefix(prefix string) Option

Loads configuration from environment variables with the given prefix.

WithValidator ​

go
func WithValidator(v *Validator) Option

Sets a custom validator for typed handler input validation.

WithoutValidation ​

go
func WithoutValidation() Option

Turns validation off, leaving validate tags inert: input is parsed and bound, and nothing is checked.

Validation became the default in v1.7.1. Before that it ran only when a Validator was configured, so an application written against an earlier version may carry validate tags it never actually enforced. This restores the old behaviour while those tags are reviewed.

WithContainer ​

go
func WithContainer(c *Container) Option

Sets the DI container for the app.

WithOpenAPIInfo ​

go
func WithOpenAPIInfo(title, version, description string) Option

Sets OpenAPI spec metadata.

WithOpenAPIPath ​

go
func WithOpenAPIPath(path string) Option

Sets the path to serve the OpenAPI spec (default: /openapi.json).

WithOpenAPITag ​

go
func WithOpenAPITag(name, description string) Option

Adds an OpenAPI tag definition.

WithOpenAPISecurityScheme ​

go
func WithOpenAPISecurityScheme(name string, scheme OpenAPISecurityScheme) Option

Adds an OpenAPI security scheme under components.securitySchemes.

WithOpenAPIBearerAuth ​

go
func WithOpenAPIBearerAuth(name string) Option

Adds a standard HTTP bearer security scheme. An empty name defaults to bearerAuth.

Defaults Summary ​

OptionDefault
ReadTimeout30s
WriteTimeout30s
IdleTimeout120s
BodyLimit4 MB
ShutdownTimeout10s
DevModefalse
SecurityHeadersfalse
TrustProxyfalse
TransportName"wing" (Linux), "fasthttp" (macOS), "nethttp" (Windows)

Released under the MIT License.