diff --git a/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java b/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java index 00d3cea..f0b5f5b 100644 --- a/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java +++ b/src/main/java/ru/ewc/checklogic/server/AllEndpoints.java @@ -23,11 +23,9 @@ */ 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; import ru.ewc.checklogic.ServerContext; /** @@ -37,21 +35,18 @@ */ public final class AllEndpoints implements Endpoints { /** - * The template processor for the index page. + * The context to be used for the server. */ - private final TemplateProcessor index; + private final ServerContext context; /** - * The context to be used for the server. + * The template renderer, creating pages to be served. */ - private final ServerContext context; + private final WebPages pages; - // @todo #47 Extract template processing into a class with lazy loading public AllEndpoints(final ServerContext context) { this.context = context; - this.index = TemplateProcessor.buildProcessor( - WebResource.readFileFromResources("templates/index.html") - ); + this.pages = new WebPages(new ResourceTemplateProcessors()); } @Override @@ -65,9 +60,9 @@ public void register(final WebFramework web) { private Response getRequestDispatcher(final Request request) { final Response result; if (this.context.isEmpty()) { - result = Response.htmlOk("No state available"); + result = this.pages.uninitializedPage(); } else if (request.requestLine().getPathDetails().getIsolatedPath().isEmpty()) { - result = Response.htmlOk(this.index.renderTemplate(Map.of())); + result = this.pages.indexPage(); } else { result = new Response(NOT_FOUND, "", PLAIN_TEXT); } diff --git a/src/main/java/ru/ewc/checklogic/server/ResourceTemplateProcessors.java b/src/main/java/ru/ewc/checklogic/server/ResourceTemplateProcessors.java new file mode 100644 index 0000000..e889faa --- /dev/null +++ b/src/main/java/ru/ewc/checklogic/server/ResourceTemplateProcessors.java @@ -0,0 +1,55 @@ +/* + * 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 java.util.HashMap; +import java.util.Map; + +/** + * I am a collection of template processors that render the pages to be served based on template + * files packed into 'resources' folder inside jar-file. + * + * @since 0.3.2 + */ +public final class ResourceTemplateProcessors implements TemplateProcessors { + /** + * The map of template processors for the templates. Used to lazy load the processors, because + * they are expensive to create. + */ + private final Map processors; + + public ResourceTemplateProcessors() { + this.processors = new HashMap<>(); + } + + @Override + public TemplateProcessor forTemplate(final String template) { + this.processors.putIfAbsent( + template, + TemplateProcessor.buildProcessor(WebResource.readFileFromResources(template)) + ); + return this.processors.get(template); + } +} diff --git a/src/main/java/ru/ewc/checklogic/server/TemplateProcessors.java b/src/main/java/ru/ewc/checklogic/server/TemplateProcessors.java new file mode 100644 index 0000000..fa55fd5 --- /dev/null +++ b/src/main/java/ru/ewc/checklogic/server/TemplateProcessors.java @@ -0,0 +1,41 @@ +/* + * 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; + +/** + * I am a collection of template processors that render the pages to be served. + * + * @since 0.3.2 + */ +public interface TemplateProcessors { + /** + * I return a template processor for the given template. + * + * @param template The name of the template to be rendered by a processor. + * @return The processor for the template. + */ + TemplateProcessor forTemplate(String template); +} diff --git a/src/main/java/ru/ewc/checklogic/server/WebPages.java b/src/main/java/ru/ewc/checklogic/server/WebPages.java new file mode 100644 index 0000000..f584ee8 --- /dev/null +++ b/src/main/java/ru/ewc/checklogic/server/WebPages.java @@ -0,0 +1,55 @@ +/* + * 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.web.Response; +import java.util.Map; + +/** + * I am a collection of template processors that render the pages to be served. + * + * @since 0.3.2 + */ +public final class WebPages { + /** + * The template processors to be used for rendering the pages. + */ + private final TemplateProcessors processors; + + public WebPages(final TemplateProcessors processors) { + this.processors = processors; + } + + public Response indexPage() { + return Response.htmlOk(this.templateNamed("templates/index.html", Map.of())); + } + + public Response uninitializedPage() { + return Response.htmlOk(this.templateNamed("templates/uninitialized.html", Map.of())); + } + + private String templateNamed(final String template, final Map values) { + return this.processors.forTemplate(template).renderTemplate(values); + } +} diff --git a/src/main/resources/templates/uninitialized.html b/src/main/resources/templates/uninitialized.html new file mode 100644 index 0000000..e26ec02 --- /dev/null +++ b/src/main/resources/templates/uninitialized.html @@ -0,0 +1,44 @@ + + + + + LC Home + + + + + + + + +

Server is not initialized

+ + + \ No newline at end of file