ru.tinkoff.kora.http.server.undertow.UndertowPrivateHttpServer Maven / Gradle / Ivy
package ru.tinkoff.kora.http.server.undertow;
import io.undertow.Undertow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xnio.XnioWorker;
import ru.tinkoff.kora.application.graph.ValueOf;
import ru.tinkoff.kora.http.server.common.HttpServerConfig;
import ru.tinkoff.kora.http.server.common.PrivateHttpServer;
import ru.tinkoff.kora.logging.common.arg.StructuredArgument;
import jakarta.annotation.Nullable;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
public class UndertowPrivateHttpServer implements PrivateHttpServer {
private static final Logger logger = LoggerFactory.getLogger(UndertowPrivateHttpServer.class);
private final ValueOf config;
private final ValueOf privateApiHandler;
private final XnioWorker xnioWorker;
private volatile Undertow undertow;
public UndertowPrivateHttpServer(ValueOf config, ValueOf privateApiHandler, @Nullable XnioWorker xnioWorker) {
this.config = config;
this.privateApiHandler = privateApiHandler;
this.xnioWorker = xnioWorker;
}
@Override
public void release() {
try {
Thread.sleep(this.config.get().shutdownWait().toMillis());
} catch (InterruptedException e) {
}
logger.debug("Private HTTP Server (Undertow) stopping...");
final long started = System.nanoTime();
if (this.undertow != null) {
this.undertow.stop();
this.undertow = null;
}
logger.info("Private HTTP Server (Undertow) stopped in {}", Duration.ofNanos(System.nanoTime() - started).toString().substring(2).toLowerCase());
}
@Override
public void init() throws InterruptedException {
logger.debug("Private HTTP Server (Undertow) starting...");
final long started = System.nanoTime();
this.undertow = this.createServer();
this.undertow.start();
var data = StructuredArgument.marker("port", this.port());
logger.info(data, "Private HTTP Server (Undertow) started in {}", Duration.ofNanos(System.nanoTime() - started).toString().substring(2).toLowerCase());
}
private Undertow createServer() {
return Undertow.builder()
.addHttpListener(this.config.get().privateApiHttpPort(), "0.0.0.0", exchange -> this.privateApiHandler.get().handleRequest(exchange))
.setWorker(this.xnioWorker)
.build();
}
@Override
public int port() {
if (this.undertow == null) {
return -1;
}
var info = this.undertow.getListenerInfo().get(0);
var address = (InetSocketAddress) info.getAddress();
return address.getPort();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy