From b24e1165ee1fc73a9d3f3da94e65cbc9dbcab604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=A2=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D1=85=D0=BE=D0=B2?= Date: Wed, 31 Jul 2024 10:03:37 +0400 Subject: [PATCH] refactor(server): started creating the single request dispatcher Refs: #47 --- ...StaticResources.java => AllEndpoints.java} | 33 ++++++++++- .../ru/ewc/checklogic/server/IndexPage.java | 58 ------------------- .../ru/ewc/checklogic/server/WebServer.java | 3 +- 3 files changed, 31 insertions(+), 63 deletions(-) rename src/main/java/ru/ewc/checklogic/server/{StaticResources.java => AllEndpoints.java} (60%) delete mode 100644 src/main/java/ru/ewc/checklogic/server/IndexPage.java diff --git a/src/main/java/ru/ewc/checklogic/server/StaticResources.java b/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java similarity index 60% rename from src/main/java/ru/ewc/checklogic/server/StaticResources.java rename to src/main/java/ru/ewc/checklogic/server/AllEndpoints.java index 5774b9e..7cec0d5 100644 --- a/src/main/java/ru/ewc/checklogic/server/StaticResources.java +++ b/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java @@ -23,19 +23,46 @@ */ package ru.ewc.checklogic.server; +import com.renomad.minum.templating.TemplateProcessor; import com.renomad.minum.web.Request; import com.renomad.minum.web.Response; import com.renomad.minum.web.WebFramework; +import java.util.Map; /** - * I am a class providing access to static files, packed inside a jar. + * I am a class providing access to all the pages and static files packed inside the jar. * * @since 0.3.0 */ -public final class StaticResources implements Endpoints { +public final class AllEndpoints implements Endpoints { + /** + * The template processor for the index page. + */ + private final TemplateProcessor index; + + // @todo #47 Extract template processing into a class with lazy loading + public AllEndpoints() { + this.index = TemplateProcessor.buildProcessor( + WebResource.readFileFromResources("templates/index.html") + ); + } + @Override public void register(final WebFramework web) { - web.registerPartialPath(GET, "static", StaticResources::staticResource); + web.registerPartialPath(GET, "static", AllEndpoints::staticResource); + web.registerPath(GET, "", this::getRequestDispatcher); + } + + // @todo #47 Check the server state before dispatching the request + @SuppressWarnings("PMD.UnusedFormalParameter") + private Response getRequestDispatcher(final Request request) { + final Response result; + if (request.requestLine().getPathDetails().getIsolatedPath().isEmpty()) { + result = Response.htmlOk(this.index.renderTemplate(Map.of())); + } else { + result = new Response(NOT_FOUND, "", PLAIN_TEXT); + } + return result; } private static Response staticResource(final Request request) { diff --git a/src/main/java/ru/ewc/checklogic/server/IndexPage.java b/src/main/java/ru/ewc/checklogic/server/IndexPage.java deleted file mode 100644 index e6aaced..0000000 --- a/src/main/java/ru/ewc/checklogic/server/IndexPage.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2024 Decision-Driven Development - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package ru.ewc.checklogic.server; - -import com.renomad.minum.templating.TemplateProcessor; -import com.renomad.minum.web.Request; -import com.renomad.minum.web.Response; -import com.renomad.minum.web.WebFramework; -import java.util.Map; - -/** - * I am the configuration and logic for the index web page. - * - * @since 0.3.2 - */ -public final class IndexPage implements Endpoints { - /** - * The template processor for the State page. - */ - private final TemplateProcessor template; - - public IndexPage() { - this.template = TemplateProcessor.buildProcessor( - WebResource.readFileFromResources("templates/index.html") - ); - } - - @Override - public void register(final WebFramework web) { - web.registerPath(GET, "", this::indexPage); - } - - @SuppressWarnings("PMD.UnusedFormalParameter") - private Response indexPage(final Request request) { - return Response.htmlOk(this.template.renderTemplate(Map.of())); - } -} diff --git a/src/main/java/ru/ewc/checklogic/server/WebServer.java b/src/main/java/ru/ewc/checklogic/server/WebServer.java index 05a460a..4cb3435 100644 --- a/src/main/java/ru/ewc/checklogic/server/WebServer.java +++ b/src/main/java/ru/ewc/checklogic/server/WebServer.java @@ -62,8 +62,7 @@ public void start() { registerEndpoints(web, new CommandPage(this.context)); registerEndpoints(web, new ResultOfTestsPage(this.root)); registerEndpoints(web, new ContextPage(this.context)); - registerEndpoints(web, new IndexPage()); - registerEndpoints(web, new StaticResources()); + registerEndpoints(web, new AllEndpoints()); minum.block(); }