From 682e0d0f25d26e28abe8afbc56f1e5c417aefdb3 Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Sun, 14 Feb 2021 17:28:36 +0100 Subject: [PATCH] Intermediate commit; to be amended. --- pom.xml | 8 +- .../ch/fritteli/labyrinth/server/Main.java | 14 +++- .../UndertowPlayground.java | 78 +++++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 src/main/java/ch/fritteli/labyrinth/server/undertow_playground/UndertowPlayground.java diff --git a/pom.xml b/pom.xml index 5035e90..8259a2d 100644 --- a/pom.xml +++ b/pom.xml @@ -48,10 +48,14 @@ logback-classic ${logback.version} + + io.undertow + undertow-core + 2.2.22.Final + org.junit.jupiter junit-jupiter-api - test @@ -83,7 +87,7 @@ scm:git:git://gittr.ch/java/labyrinth-server.git scm:git:ssh://git@gittr.ch/java/labyrinth-server.git https://gittr.ch/java/labyrinth-server - v0.0.1 + HEAD diff --git a/src/main/java/ch/fritteli/labyrinth/server/Main.java b/src/main/java/ch/fritteli/labyrinth/server/Main.java index ced13bd..21e0d6c 100644 --- a/src/main/java/ch/fritteli/labyrinth/server/Main.java +++ b/src/main/java/ch/fritteli/labyrinth/server/Main.java @@ -1,11 +1,21 @@ package ch.fritteli.labyrinth.server; +import ch.fritteli.labyrinth.server.undertow_playground.UndertowPlayground; +import io.undertow.Undertow; import lombok.extern.slf4j.Slf4j; @Slf4j public class Main { public static void main(String[] args) { - LabyrinthServer.createAndStartServer() - .onEmpty(() -> log.error("Failed to create server. Stopping.")); +// LabyrinthServer.createAndStartServer() +// .onEmpty(() -> log.error("Failed to create server. Stopping.")); + + final ServerConfig config = ServerConfig.init(); + log.info("Starting Server at http://{}:{}/", config.getAddress().getHostAddress(), config.getPort()); + Undertow.builder() + .addHttpListener(config.getPort(), config.getAddress().getHostAddress()) + .setHandler(UndertowPlayground.r) + .build() + .start(); } } diff --git a/src/main/java/ch/fritteli/labyrinth/server/undertow_playground/UndertowPlayground.java b/src/main/java/ch/fritteli/labyrinth/server/undertow_playground/UndertowPlayground.java new file mode 100644 index 0000000..b64a343 --- /dev/null +++ b/src/main/java/ch/fritteli/labyrinth/server/undertow_playground/UndertowPlayground.java @@ -0,0 +1,78 @@ +package ch.fritteli.labyrinth.server.undertow_playground; + +import io.undertow.server.HttpHandler; +import io.undertow.server.HttpServerExchange; +import io.undertow.server.RoutingHandler; +import io.undertow.util.HeaderValues; +import io.undertow.util.Headers; +import io.undertow.util.HttpString; +import io.vavr.control.Option; +import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; + +import java.util.Deque; +import java.util.Map; + +@Slf4j +public class UndertowPlayground { + public static final RoutingHandler r = new RoutingHandler() + .get("/create/{output}", new HttpHandler() { + @Override + public void handleRequest(HttpServerExchange exchange) throws Exception { + if (exchange.isInIoThread()) { + exchange.dispatch(this); + return; + } + final Map> queryParameters = exchange.getQueryParameters(); + final Option output = getFirstOption(queryParameters, "output"); + final Option width = getIntOption(queryParameters, "width"); + final Option height = getIntOption(queryParameters, "height"); + final Option id = getIntOption(queryParameters, "id"); + + log.info("Output: {}", output); + log.info("Width: {}", width); + log.info("Height: {}", height); + log.info("Id: {}", id); + exchange.getResponseSender().send( + "Output: " + output + + ", Width: " + width + + ", Height: " + height + + ", Id: " + id + ); + } + }) + .post("/render", new HttpHandler() { + @Override + public void handleRequest(final HttpServerExchange exchange) { + if (exchange.isInIoThread()) { + exchange.dispatch(this); + return; + } + exchange.getResponseSender().send("TODO: read body, render stuff"); + } + }) + .setFallbackHandler(new HttpHandler() { + @Override + public void handleRequest(HttpServerExchange exchange) throws Exception { + exchange.getResponseSender().send("Request: " + exchange.getRequestURI()); + final HeaderValues strings = exchange.getRequestHeaders().get(Headers.ACCEPT); + strings.peekFirst(); + } + } + ); + + @NonNull + private static Option getFirstOption(@NonNull final Map> queryParams, @NonNull final String paramName) { + return Option.of(queryParams.get(paramName)) + .map(Deque::peek) + .flatMap(Option::of); + } + + @NonNull + private static Option getIntOption(@NonNull final Map> queryParams, @NonNull final String paramName) { + return getFirstOption(queryParams, paramName) + .toTry() + .map(Integer::parseInt) + .toOption(); + } +}