diff --git a/labyrinth-server.iml b/labyrinth-server.iml index 8d460f3..9a37eeb 100644 --- a/labyrinth-server.iml +++ b/labyrinth-server.iml @@ -12,7 +12,7 @@ - + diff --git a/pom.xml b/pom.xml index b18d2cf..3a3f0b1 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ ch.fritteli.labyrinth labyrinth-generator - 0.0.1 + 0.0.2-SNAPSHOT io.vavr diff --git a/src/main/java/ch/fritteli/labyrinth/server/LabyrinthServer.java b/src/main/java/ch/fritteli/labyrinth/server/LabyrinthServer.java index 0eb2530..bcb6bd4 100644 --- a/src/main/java/ch/fritteli/labyrinth/server/LabyrinthServer.java +++ b/src/main/java/ch/fritteli/labyrinth/server/LabyrinthServer.java @@ -7,12 +7,7 @@ import ch.fritteli.labyrinth.generator.renderer.text.TextRenderer; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpServer; -import io.vavr.collection.HashMap; -import io.vavr.collection.HashSet; -import io.vavr.collection.List; -import io.vavr.collection.Map; -import io.vavr.collection.Set; -import io.vavr.collection.Stream; +import io.vavr.collection.*; import io.vavr.control.Option; import io.vavr.control.Try; import lombok.Getter; @@ -82,7 +77,7 @@ public class LabyrinthServer { public void start() { Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "listener-stopper")); this.httpServer.start(); - log.info("Listening on {}", this.httpServer.getAddress()); + log.info("Listening on http://{}:{}", this.httpServer.getAddress().getHostString(), this.httpServer.getAddress().getPort()); } public void stop() { @@ -101,8 +96,8 @@ public class LabyrinthServer { return; } final Map requestParams = this.parseQueryString(exchange.getRequestURI().getQuery()); - final int width = this.getOrDefault(requestParams.get(RequestParameter.WIDTH), Integer::valueOf, 1); - final int height = this.getOrDefault(requestParams.get(RequestParameter.HEIGHT), Integer::valueOf, 1); + final int width = this.getOrDefault(requestParams.get(RequestParameter.WIDTH), Integer::valueOf, 5); + final int height = this.getOrDefault(requestParams.get(RequestParameter.HEIGHT), Integer::valueOf, 5); final Option idOption = requestParams.get(RequestParameter.ID).toTry().map(Long::valueOf).toOption(); final Option outputOption = requestParams.get(RequestParameter.OUTPUT).flatMap(OutputType::ofString); final Headers responseHeaders = exchange.getResponseHeaders(); @@ -115,10 +110,21 @@ public class LabyrinthServer { exchange.close(); return; } + final Labyrinth labyrinth = new Labyrinth(width, height, id); + final byte[] render; + try { + render = output.render(labyrinth); + } catch (Exception e) { + responseHeaders.add("Content-type", "text/plain; charset=UTF-8"); + exchange.sendResponseHeaders(500, 0); + final OutputStream responseBody = exchange.getResponseBody(); + responseBody.write(("Error: " + e).getBytes(StandardCharsets.UTF_8)); + responseBody.flush(); + exchange.close(); + return; + } responseHeaders.add("Content-type", output.getContentType()); exchange.sendResponseHeaders(200, 0); - final Labyrinth labyrinth = new Labyrinth(width, height, id); - final byte[] render = output.render(labyrinth); final OutputStream responseBody = exchange.getResponseBody(); responseBody.write(render); responseBody.flush();