Search in sources :

Example 36 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().tracer(TracerBuilder.create(config.get("tracing"))).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 : TracerBuilder(io.helidon.tracing.TracerBuilder) 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) 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 37 with Single

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

the class RestApiBase method responseSupplier.

/**
 * Create a response supplier from the request.
 * This method checks if there is a payload, and prepares the supplier based on this information.
 *
 * @param method HTTP method to invoke
 * @param path path to invoke
 * @param request request that may contain a JSON entity
 * @param requestId request ID to use for this request
 * @return supplier of response that is used with fault tolerance
 */
protected Supplier<Single<WebClientResponse>> responseSupplier(Http.RequestMethod method, String path, ApiRequest<?> request, String requestId) {
    WebClientRequestBuilder requestBuilder = webClient.method(method).path(path);
    addHeaders(requestBuilder, path, request, method, requestId);
    addQueryParams(requestBuilder, path, request, method, requestId);
    Optional<JsonObject> payload = request.toJson(jsonBuilderFactory);
    Supplier<Single<WebClientResponse>> responseSupplier;
    // now let's update the request
    if (payload.isPresent()) {
        responseSupplier = requestJsonPayload(path, request, method, requestId, requestBuilder, payload.get());
    } else {
        responseSupplier = requestPayload(path, request, method, requestId, requestBuilder);
    }
    return responseSupplier;
}
Also used : Single(io.helidon.common.reactive.Single) JsonObject(jakarta.json.JsonObject) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder)

Example 38 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
 * @throws IOException if there are problems reading logging properties
 */
static Single<WebServer> startServer() throws IOException {
    // load logging configuration
    LogConfig.configureRuntime();
    // By default this will pick up application.yaml from the classpath
    Config config = Config.create();
    // Get webserver config from the "server" section of application.yaml
    Single<WebServer> server = WebServer.builder(createRouting(config)).config(config.get("server")).addMediaSupport(JsonpSupport.create()).build().start();
    server.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!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    return server;
}
Also used : Config(io.helidon.config.Config) IOException(java.io.IOException) CrossOriginConfig(io.helidon.webserver.cors.CrossOriginConfig) Logger(java.util.logging.Logger) HealthSupport(io.helidon.health.HealthSupport) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) WebServer(io.helidon.webserver.WebServer) CorsSupport(io.helidon.webserver.cors.CorsSupport) Single(io.helidon.common.reactive.Single) MetricsSupport(io.helidon.metrics.MetricsSupport) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) HealthChecks(io.helidon.health.checks.HealthChecks) WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config) CrossOriginConfig(io.helidon.webserver.cors.CrossOriginConfig) LogConfig(io.helidon.common.LogConfig)

Example 39 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();
    // Get webserver config from the "server" section of application.yaml and JSON support registration
    Single<WebServer> server = WebServer.builder(createRouting(config)).config(config.get("server")).addMediaSupport(JsonbSupport.create()).build().start();
    server.thenAccept(ws -> {
        System.out.println("WEB server is up!");
        System.out.println("Web client at: http://localhost:" + ws.port() + "/public/index.html");
        ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    return server;
}
Also used : JsonbSupport(io.helidon.media.jsonb.JsonbSupport) StaticContentSupport(io.helidon.webserver.staticcontent.StaticContentSupport) 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) HealthSupport(io.helidon.health.HealthSupport) HealthChecks(io.helidon.health.checks.HealthChecks) WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig)

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