Search in sources :

Example 1 with HttpPredicateRouterBuilder

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();
    }
}
Also used : StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) IoExecutor(io.servicetalk.transport.api.IoExecutor) NettyIoExecutors.createIoExecutor(io.servicetalk.transport.netty.NettyIoExecutors.createIoExecutor) ServerContext(io.servicetalk.transport.api.ServerContext) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) HttpClient(io.servicetalk.http.api.HttpClient) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpPredicateRouterBuilder(io.servicetalk.http.router.predicate.HttpPredicateRouterBuilder)

Example 2 with HttpPredicateRouterBuilder

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();
}
Also used : HttpPredicateRouterBuilder(io.servicetalk.http.router.predicate.HttpPredicateRouterBuilder)

Aggregations

HttpPredicateRouterBuilder (io.servicetalk.http.router.predicate.HttpPredicateRouterBuilder)2 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)1 CompositeCloseable (io.servicetalk.concurrent.api.CompositeCloseable)1 HttpClient (io.servicetalk.http.api.HttpClient)1 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)1 StreamingHttpService (io.servicetalk.http.api.StreamingHttpService)1 IoExecutor (io.servicetalk.transport.api.IoExecutor)1 ServerContext (io.servicetalk.transport.api.ServerContext)1 NettyIoExecutors.createIoExecutor (io.servicetalk.transport.netty.NettyIoExecutors.createIoExecutor)1