Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/Jex.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ default Jex options(String path, ExchangeHandler handler, Role... roles) {
* @param roles An array of roles that are associated with this endpoint.
*/
default Jex sse(String path, Consumer<SseClient> handler, Role... roles) {
return get(path, new SseHandler(handler), roles);
return get(path, SseClient.handler(handler), roles);
}

/** Add a filter for all matched requests. */
Expand Down
4 changes: 2 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/Routing.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ default Routing after(Consumer<Context> handler) {
* @param handler The sse handler to invoke when a GET request matches the path.
* @param roles An array of roles that are associated with this endpoint.
*/
default Routing sse(String path, Consumer<SseClient> consumer, Role... roles) {
return get(path, new SseHandler(consumer), roles);
default Routing sse(String path, Consumer<SseClient> handler, Role... roles) {
return get(path, SseClient.handler(handler), roles);
}

/** Return all the registered handlers. */
Expand Down
12 changes: 5 additions & 7 deletions avaje-jex/src/main/java/io/avaje/jex/http/sse/SseClient.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.avaje.jex.http.sse;

import java.io.Closeable;
import java.util.function.Consumer;

import io.avaje.jex.http.Context;
import io.avaje.jex.http.ExchangeHandler;
import io.avaje.jex.spi.JsonService;

/**
Expand All @@ -16,13 +18,9 @@
*/
public sealed interface SseClient extends Closeable permits SseClientImpl {

/**
* @param ctx
* @return the new SseClient instance
*/
static SseClient of(Context ctx) {

return new SseClientImpl(ctx);
/** Return an SseClient handler. */
static ExchangeHandler handler(Consumer<SseClient> consumer) {
return new SseHandler(consumer);
}

/** Close the SseClient and release keepAlive block if any */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.jex;
package io.avaje.jex.http.sse;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand All @@ -8,10 +8,9 @@
import io.avaje.jex.http.BadRequestException;
import io.avaje.jex.http.Context;
import io.avaje.jex.http.ExchangeHandler;
import io.avaje.jex.http.sse.SseClient;

/** Handler that configures a request for Server Sent Events */
class SseHandler implements ExchangeHandler {
final class SseHandler implements ExchangeHandler {

private static final String TEXT_EVENT_STREAM = "text/event-stream";
private final Consumer<SseClient> consumer;
Expand All @@ -34,7 +33,7 @@ public void handle(Context ctx) throws Exception {
headers.add("Cache-Control", "no-cache");
headers.add("X-Accel-Buffering", "no"); // See https://serverfault.com/a/801629

try (var sse = SseClient.of(ctx)) {
try (var sse = new SseClientImpl(ctx)) {
exchange.sendResponseHeaders(200, 0);
consumer.accept(sse);
} catch (final IOException e) {
Expand Down