Search in sources :

Example 1 with Single

use of io.helidon.common.reactive.Single in project helidon by oracle.

the class Main method startServer.

/**
 * Start the server.
 * @return the created {@link WebServer} instance
 */
static Single<WebServer> startServer() {
    // load logging configuration
    LogConfig.configureRuntime();
    // By default this will pick up application.yaml from the classpath
    Config config = Config.create();
    WebServer server = WebServer.builder().routing(createRouting(config)).config(config.get("server")).addMediaSupport(JsonpSupport.create()).build();
    Single<WebServer> webserver = server.start();
    // Try to start the server. If successful, print some info and arrange to
    // print a message at shutdown. If unsuccessful, print the exception.
    webserver.thenAccept(ws -> {
        System.out.println("WEB server is up! http://localhost:" + ws.port() + "/greet");
        ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    }).exceptionallyAccept(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
    });
    return webserver;
}
Also used : JsonpSupport(io.helidon.media.jsonp.JsonpSupport) KeyPerformanceIndicatorMetricsSettings(io.helidon.metrics.KeyPerformanceIndicatorMetricsSettings) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) Single(io.helidon.common.reactive.Single) MetricsSupport(io.helidon.metrics.MetricsSupport) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig)

Example 2 with Single

use of io.helidon.common.reactive.Single in project helidon by oracle.

the class Main method startFrontendServer.

/**
 * Start the server.
 * @return the created {@link WebServer} instance
 */
public static Single<WebServer> startFrontendServer() {
    // configure logging in order to not have the standard JVM defaults
    LogConfig.configureRuntime();
    Config config = Config.builder().sources(ConfigSources.environmentVariables()).build();
    WebServer webServer = WebServer.builder(Routing.builder().register(new TranslatorFrontendService(config.get("backend.host").asString().orElse("localhost"), 9080))).port(8080).tracer(TracerBuilder.create(config.get("tracing")).serviceName("helidon-webserver-translator-frontend").registerGlobal(false).build()).build();
    return webServer.start().peek(ws -> {
        System.out.println("WEB server is up! http://localhost:" + ws.port());
        ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    }).onError(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
    });
}
Also used : TracerBuilder(io.helidon.tracing.TracerBuilder) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) Single(io.helidon.common.reactive.Single) LogConfig(io.helidon.common.LogConfig) ConfigSources(io.helidon.config.ConfigSources) Routing(io.helidon.webserver.Routing) WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig)

Example 3 with Single

use of io.helidon.common.reactive.Single in project helidon by oracle.

the class ServerMain method startServer.

/**
 * Start the server.
 *
 * @return the created {@link WebServer} instance
 */
static Single<WebServer> startServer() {
    // By default this will pick up application.yaml from the classpath
    Config config = Config.create();
    WebServer server = WebServer.builder(createRouting(config)).config(config.get("server")).addMediaSupport(JsonpSupport.create()).build();
    // Server threads are not daemon. No need to block. Just react.
    return server.start().peek(ws -> {
        serverPort = ws.port();
        System.out.println("WEB server is up! http://localhost:" + ws.port() + "/greet");
        ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    }).onError(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
    });
}
Also used : JsonpSupport(io.helidon.media.jsonp.JsonpSupport) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) Single(io.helidon.common.reactive.Single) MetricsSupport(io.helidon.metrics.MetricsSupport) Routing(io.helidon.webserver.Routing) WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config)

Example 4 with Single

use of io.helidon.common.reactive.Single in project helidon by oracle.

the class RestApiBase method requestJsonPayload.

/**
 * Create a supplier for a response with JSON request.
 * Defaults to "{@code () -> clientRequest.submit(jsonObject)}".
 * Also configures content type and accept headers.
 *
 * @param path path requested
 * @param request API request
 * @param method HTTP method
 * @param requestId ID of this request
 * @param requestBuilder {@link io.helidon.webclient.WebClient} request builder
 * @param jsonObject JSON object that should be sent as a request entity
 *
 * @return supplier of a web client response
 */
protected Supplier<Single<WebClientResponse>> requestJsonPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, JsonObject jsonObject) {
    requestBuilder.accept(request.responseMediaType().orElse(MediaType.APPLICATION_JSON));
    requestBuilder.contentType(request.requestMediaType().orElse(MediaType.APPLICATION_JSON));
    AtomicBoolean updated = new AtomicBoolean();
    return () -> {
        // we should only update request builder once - if a retry is done, it should not be reset
        if (updated.compareAndSet(false, true)) {
            Single<WebClientRequestBuilder> res = updateRequestBuilder(requestBuilder, path, request, method, requestId, jsonObject);
            return res.flatMapSingle(it -> it.submit(jsonObject));
        } else {
            return requestBuilder.submit(jsonObject);
        }
    };
}
Also used : JsonWriterFactory(jakarta.json.JsonWriterFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) JsonReaderFactory(jakarta.json.JsonReaderFactory) DataChunk(io.helidon.common.http.DataChunk) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Supplier(java.util.function.Supplier) MediaType(io.helidon.common.http.MediaType) Flow(java.util.concurrent.Flow) JsonObject(jakarta.json.JsonObject) Single(io.helidon.common.reactive.Single) Http(io.helidon.common.http.Http) Multi(io.helidon.common.reactive.Multi) FtHandler(io.helidon.faulttolerance.FtHandler) Collector(io.helidon.common.reactive.Collector) UUID(java.util.UUID) Logger(java.util.logging.Logger) WebClientRequestHeaders(io.helidon.webclient.WebClientRequestHeaders) Contexts(io.helidon.common.context.Contexts) SpanContext(io.opentracing.SpanContext) StringReader(java.io.StringReader) Optional(java.util.Optional) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Single(io.helidon.common.reactive.Single)

Example 5 with Single

use of io.helidon.common.reactive.Single in project helidon by oracle.

the class RestApiBase method invokeBytesRequest.

@Override
public <T extends ApiResponse> Single<T> invokeBytesRequest(Http.RequestMethod method, String path, ApiRequest<?> request, Flow.Publisher<DataChunk> byteRequest, ApiResponse.Builder<?, T> responseBuilder) {
    String requestId = requestId(request);
    LOGGER.finest(() -> requestId + ": Invoking " + method + " on path " + path + " with bytes request");
    WebClientRequestBuilder requestBuilder = webClient.method(method).path(path);
    addHeaders(requestBuilder, path, request, method, requestId);
    addQueryParams(requestBuilder, path, request, method, requestId);
    Supplier<Single<WebClientResponse>> responseSupplier = requestBytesPayload(path, request, method, requestId, requestBuilder, byteRequest);
    return ftHandler.invoke(responseSupplier).flatMapSingle(response -> handleResponse(path, request, method, requestId, response, responseBuilder));
}
Also used : Single(io.helidon.common.reactive.Single) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder)

Aggregations

Single (io.helidon.common.reactive.Single)39 Config (io.helidon.config.Config)23 Routing (io.helidon.webserver.Routing)18 WebServer (io.helidon.webserver.WebServer)18 LogConfig (io.helidon.common.LogConfig)16 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)15 MetricsSupport (io.helidon.metrics.MetricsSupport)12 Optional (java.util.Optional)12 Logger (java.util.logging.Logger)12 Http (io.helidon.common.http.Http)11 MediaType (io.helidon.common.http.MediaType)9 DataChunk (io.helidon.common.http.DataChunk)8 HealthSupport (io.helidon.health.HealthSupport)8 HealthChecks (io.helidon.health.checks.HealthChecks)8 WebClient (io.helidon.webclient.WebClient)8 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)8 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)8 JsonObject (jakarta.json.JsonObject)8 Supplier (java.util.function.Supplier)8 Contexts (io.helidon.common.context.Contexts)7