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;
}
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);
});
}
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);
});
}
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);
}
};
}
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));
}
Aggregations