use of io.servicetalk.http.router.predicate.HttpPredicateRouterBuilder in project servicetalk by apple.
the class GatewayServer method main.
/**
* Starts this server.
*
* @param args Program arguments, none supported yet.
* @throws Exception If the server could not be started.
*/
public static void main(String[] args) throws Exception {
// Create an AutoCloseable representing all resources used in this example.
try (CompositeCloseable resources = newCompositeCloseable()) {
// Shared IoExecutor for the application.
IoExecutor ioExecutor = resources.prepend(createIoExecutor());
// Create clients for the different backends we are going to use in the gateway.
StreamingHttpClient recommendationsClient = newClient(ioExecutor, RECOMMENDATIONS_BACKEND_ADDRESS, resources, "recommendations backend");
HttpClient metadataClient = newClient(ioExecutor, METADATA_BACKEND_ADDRESS, resources, "metadata backend").asClient();
HttpClient userClient = newClient(ioExecutor, USER_BACKEND_ADDRESS, resources, "user backend").asClient();
HttpClient ratingsClient = newClient(ioExecutor, RATINGS_BACKEND_ADDRESS, resources, "ratings backend").asClient();
// Gateway supports different endpoints for blocking, streaming or aggregated implementations.
// We create a router to express these endpoints.
HttpPredicateRouterBuilder routerBuilder = new HttpPredicateRouterBuilder();
final StreamingHttpService gatewayService = routerBuilder.whenPathStartsWith("/recommendations/stream").thenRouteTo(new StreamingGatewayService(recommendationsClient, metadataClient, ratingsClient, userClient)).whenPathStartsWith("/recommendations/aggregated").thenRouteTo(new GatewayService(recommendationsClient.asClient(), metadataClient, ratingsClient, userClient)).whenPathStartsWith("/recommendations/blocking").thenRouteTo(new BlockingGatewayService(recommendationsClient.asBlockingClient(), metadataClient.asBlockingClient(), ratingsClient.asBlockingClient(), userClient.asBlockingClient())).buildStreaming();
// Create configurable starter for HTTP server.
// Starting the server will start listening for incoming client requests.
ServerContext serverContext = HttpServers.forPort(8080).ioExecutor(ioExecutor).appendServiceFilter(new BadResponseHandlingServiceFilter()).listenStreamingAndAwait(gatewayService);
LOGGER.info("Listening on {}", serverContext.listenAddress());
// Blocks and awaits shutdown of the server this ServerContext represents.
serverContext.awaitShutdown();
}
}
use of io.servicetalk.http.router.predicate.HttpPredicateRouterBuilder in project servicetalk by apple.
the class RecommendationBackend method newRecommendationsService.
static StreamingHttpService newRecommendationsService() {
HttpPredicateRouterBuilder routerBuilder = new HttpPredicateRouterBuilder();
routerBuilder.whenPathStartsWith("/recommendations/stream").thenRouteTo(new StreamingService());
routerBuilder.whenPathStartsWith("/recommendations/aggregated").thenRouteTo(new AggregatedService());
return routerBuilder.buildStreaming();
}
Aggregations