Core APIThe aether-core module provides the fundamental building blocks for Aether applications, including the Exchange interface for handling HTTP interactions and the Pipeline for middleware orchestration.

Core API

The aether-core module provides the fundamental building blocks for Aether applications, including the Exchange interface for handling HTTP interactions and the Pipeline for middleware orchestration.

Exchange

The Exchange interface represents a single HTTP request-response cycle. It provides access to the request data, response manipulation, and helper methods for sending various types of responses.

Properties

PropertyTypeDescription
requestRequestThe incoming HTTP request.
responseResponseThe outgoing HTTP response.
attributesAttributesA map for storing request-scoped data (e.g., extracted path parameters, user sessions).

Response Methods

respond(statusCode: Int = 200, body: String)

Sends a plain text response.

  • Sets Content-Type to text/plain; charset=utf-8.

respondHtml(statusCode: Int = 200, html: String)

Sends an HTML response.

  • Sets Content-Type to text/html; charset=utf-8.

respondJson<T>(statusCode: Int = 200, data: T, json: Json = Json, serializer: KSerializer<T>)

Sends a JSON response.

  • Sets Content-Type to application/json; charset=utf-8.
  • Uses kotlinx.serialization to encode the data.

respondCbor(statusCode: Int = 200, data: ByteArray)

Sends a CBOR (Concise Binary Object Representation) response.

  • Sets Content-Type to application/cbor.

respondBytes(statusCode: Int = 200, contentType: String, bytes: ByteArray)

Sends raw binary data with a specified content type.

redirect(url: String, permanent: Boolean = false)

Sends a redirect response.

  • Status code: 302 Found (default) or 301 Moved Permanently.
  • Sets the Location header.

Bounded request and response streaming (JVM)

Set streamRequestBodies = true on VertxServerConfig or AetherServerConfig to opt into the single-consumer Request.openBodyStream() capability. The default remains the compatible bounded bodyBytes() path. Server limits always take precedence over route limits:

val stream = exchange.request.openBodyStream(
    RequestBodyStreamLimits(
        maximumTotalBytes = 64L * 1024 * 1024,
        maximumChunkBytes = 64 * 1024,
        deadline = 30.seconds
    )
) ?: error("Enable streamRequestBodies")

stream.consume { ciphertextChunk ->
    objectStoreMultipartPart(ciphertextChunk) // consume before returning
}
exchange.response.write("accepted")

Each body can be opened once. Oversize chunks or totals, deadlines, disconnects, and repeated consumption produce typed RequestBodyStreamException failures. The adapter pauses the Vert.x source until downstream demand is available; highWaterBytes reports the largest retained body window. Cancellation closes only that request connection. Calling bodyBytes() on a streaming request uses the same bounded source and should remain limited to ordinary JSON/form payloads.

Response.write(ByteArray) is incremental and awaits every Vert.x write. The adapter also waits for a full responseWriteQueueBytes queue to drain before accepting another chunk, so a slow reader applies backpressure. A response must still end exactly once.

For large multipart/form-data, use Exchange.streamMultipart(...) with a StreamingMultipartSink. It recognizes boundaries split across transport chunks, bounds headers, file count, file size, total bytes, chunk size, and deadline, and reports transport high-water in StreamingMultipartResult. Sink calls are sequential. onPartData reuses its transfer buffer, so upload or encrypt each range before returning; copy only when the destination API requires retention. This supports direct ciphertext object-store multipart writes without materializing the plaintext file in framework memory.

StreamingProxyClient similarly bounds request chunks and response demand with ProxyConfig.streamBufferSize and streamBufferChunks. ProxyResult.metrics.highWaterBytes reports retained response bytes. Stopping collection closes the upstream connection; closing the client is idempotent.

Both JVM servers reject new work during stop(), drain owned request and WebSocket children for shutdownGraceMillis, then cancel remaining children and close transport resources once. Use the bounded grace period for rolling replacement; existing WebSockets remain active during the drain, while the replacement listener receives new connections.

Error Helpers

  • notFound(message: String = "Not Found"): Sends a 404 response.
  • internalError(message: String = "Internal Server Error"): Sends a 500 response.
  • badRequest(message: String = "Bad Request"): Sends a 400 response.
  • unauthorized(message: String = "Unauthorized"): Sends a 401 response.

Pipeline

The Pipeline class implements the middleware chain pattern (often called the "Russian Doll" model). Middleware functions are executed sequentially, and each middleware can decide whether to pass control to the next one or short-circuit the request.

Middleware Definition

Middleware is defined as a suspend function:

typealias Middleware = suspend (exchange: Exchange, next: suspend () -> Unit) -> Unit

Usage

val pipeline = Pipeline()

pipeline.use { exchange, next ->
    println("Before request")
    next() // Pass control to the next middleware
    println("After request")
}

Methods

  • use(middleware: Middleware): Adds a middleware to the end of the pipeline.
  • execute(exchange: Exchange, handler: suspend (Exchange) -> Unit): Executes the pipeline. If all middleware call next(), the final handler is executed.
  • copy(): Creates a shallow copy of the pipeline.
  • clear(): Removes all middleware.

Safe request diagnostics and errors

Both JVM server adapters establish a bounded X-Request-Id before pipeline execution. A supplied ID is retained only when it is 8–64 ASCII letters, digits, ., _, or -; malformed values are replaced. installRecovery() emits the canonical JSON envelope {"error":{"code":"…","message":"…","retryable":false},"request_id":"…"}. Throw ApiException(ApiErrorKind.…) for a deliberate public classification. Arbitrary exception types, messages, causes, SQL, and stack traces always map to opaque INTERNAL; CancellationException and fatal Error values are rethrown.

ApiErrorKindHTTP
BAD_REQUEST400
UNAUTHENTICATED401
PERMISSION_DENIED403
NOT_FOUND404
REQUEST_TIMEOUT408
CONFLICT409
GONE410
PAYLOAD_TOO_LARGE413
VALIDATION_FAILED, UNSUPPORTED_FORMAT422
RATE_LIMITED429
DEPENDENCY_UNAVAILABLE503
INTERNAL500

installCallLogging() records only request ID, method, matched route template, status, bounded duration, and typed error category. It never formats raw paths, path parameters, query strings, headers, cookies, bodies, SQL, URLs, or exception causes. Set RequestDiagnosticsPolicy(includeRouteTemplate = false) when route templates themselves are sensitive. DebugToolbarConfig(profile = DiagnosticsProfile.PRIVATE_PRODUCTION) disables toolbar injection and SQL capture. Do not use development diagnostics in a private production process.

DSL

You can use the pipeline builder function:

val appPipeline = pipeline {
    use(loggingMiddleware)
    use(authMiddleware)
}

Rate Limit Middleware

The rate limit middleware provides quota-based request limiting to protect your API from abuse.

Basic Usage

pipeline.installRateLimit {
    quotaProvider = InMemoryQuotaProvider(
        limit = 100,          // 100 requests
        windowMillis = 60_000 // per minute
    )
    keyExtractor = { exchange ->
        exchange.request.headers.get("X-API-Key") ?: exchange.request.remoteAddress
    }
}

Configuration Options

pipeline.installRateLimit {
    // Required: Quota provider
    quotaProvider = InMemoryQuotaProvider(limit = 1000, windowMillis = 3600_000)
    
    // Extract key to identify the client (default: IP address)
    keyExtractor = { exchange -> exchange.request.remoteAddress }
    
    // Cost per request (default: 1)
    costFunction = { exchange ->
        if (exchange.request.path.startsWith("/api/heavy/")) 10L else 1L
    }
    
    // Paths to exclude from rate limiting
    excludedPaths = setOf("/health", "/metrics")
    excludedPathPrefixes = setOf("/public/", "/static/")
    
    // Bypass condition (e.g., for admin users)
    bypassCondition = { exchange ->
        exchange.attributes[UserKey]?.isAdmin == true
    }
    
    // Response customization
    statusCode = 429  // Too Many Requests (default)
    errorMessage = "Rate limit exceeded"
    includeHeaders = true  // X-RateLimit-* headers
    
    // Custom response headers
    responseHeaders = { usage ->
        mapOf("X-Credits-Remaining" to usage.remaining.toString())
    }
}

Database-Backed Credits

For persistent quota tracking:

pipeline.installRateLimitWithCredits(
    getCredits = { userId -> userService.getCredits(userId) },
    deductCredits = { userId, amount -> userService.deductCredits(userId, amount) }
) {
    keyExtractor = { exchange -> exchange.attributes[UserKey]?.id }
}

Response Headers

When includeHeaders = true, these headers are added:

  • X-RateLimit-Limit: Total quota
  • X-RateLimit-Remaining: Remaining quota
  • X-RateLimit-Reset: Unix timestamp when quota resets

HTTP Reverse Proxy

The proxy middleware enables forwarding requests to upstream services with full streaming support.

Simple Proxy

// One-liner: forward entire request and response
exchange.proxyTo("https://api.example.com/v1${exchange.request.path}")

Proxy with Inspection

// Inspect response before forwarding
val response = exchange.proxyRequest("https://api.example.com/v1/data")
if (response.statusCode == 200) {
    exchange.respondBytes(response.statusCode, response.contentType, response.body)
} else {
    exchange.internalError("Upstream error")
}

Proxy Middleware DSL

pipeline.installProxy {
    // Route by path prefix
    route("/api/users") {
        target = "https://users-service.internal:8080"
        stripPrefix = true  // /api/users/123 -> /123
    }
    
    route("/api/orders") {
        target = "https://orders-service.internal:8080"
        
        // Header management
        addHeaders = mapOf("X-Internal-Token" to "secret")
        removeHeaders = setOf("Cookie", "Authorization")
        
        // Path rewriting
        pathRewriter = { path -> "/v2$path" }
    }
    
    // Global settings
    timeout = 30_000  // 30 seconds
    includeForwardedHeaders = true  // X-Forwarded-For, X-Forwarded-Proto
    
    // Circuit breaker
    circuitBreaker {
        failureThreshold = 5
        successThreshold = 2
        timeout = 60_000  // Open state duration
    }
}

Streaming Support

The proxy supports true streaming for SSE and chunked responses:

route("/api/stream") {
    target = "https://streaming-service.internal:8080"
    streaming = true  // Enable Flow<ByteArray> streaming
}

Error Handling

pipeline.apply {
    handleProxyExceptions()  // Automatic 502/503/504 responses
    installProxy { ... }
}

Exception types:

  • ProxyConnectionException → 502 Bad Gateway
  • ProxyTimeoutException → 504 Gateway Timeout
  • ProxyCircuitOpenException → 503 Service Unavailable
Architected in Kotlin. Rendered with Materia. Powered by Aether.
© 2026 Yousef.