This commit is contained in:
		
							parent
							
								
									916b163ea8
								
							
						
					
					
						commit
						11580f2927
					
				
					 3 changed files with 19 additions and 13 deletions
				
			
		|  | @ -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<RequestParameter, String> 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<Long> idOption = requestParams.get(RequestParameter.ID).toTry().map(Long::valueOf).toOption(); | ||||
|         final Option<OutputType> 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(); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue