feature/undertow #4

Merged
manuel merged 15 commits from feature/undertow into master 2023-04-08 22:40:46 +02:00
3 changed files with 96 additions and 4 deletions
Showing only changes of commit 682e0d0f25 - Show all commits

View file

@ -48,10 +48,14 @@
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>2.2.22.Final</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
@ -83,7 +87,7 @@
<connection>scm:git:git://gittr.ch/java/labyrinth-server.git</connection>
<developerConnection>scm:git:ssh://git@gittr.ch/java/labyrinth-server.git</developerConnection>
<url>https://gittr.ch/java/labyrinth-server</url>
<tag>v0.0.1</tag>
<tag>HEAD</tag>
</scm>
<distributionManagement>
<repository>

View file

@ -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();
}
}

View file

@ -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<String, Deque<String>> queryParameters = exchange.getQueryParameters();
final Option<String> output = getFirstOption(queryParameters, "output");
final Option<Integer> width = getIntOption(queryParameters, "width");
final Option<Integer> height = getIntOption(queryParameters, "height");
final Option<Integer> 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<String> getFirstOption(@NonNull final Map<String, Deque<String>> queryParams, @NonNull final String paramName) {
return Option.of(queryParams.get(paramName))
.map(Deque::peek)
.flatMap(Option::of);
}
@NonNull
private static Option<Integer> getIntOption(@NonNull final Map<String, Deque<String>> queryParams, @NonNull final String paramName) {
return getFirstOption(queryParams, paramName)
.toTry()
.map(Integer::parseInt)
.toOption();
}
}