Skip to content

Commit

Permalink
refactor(server): extracted template processors into lazy loaded coll…
Browse files Browse the repository at this point in the history
…ection

Refs: #47
  • Loading branch information
nergal-perm committed Jul 31, 2024
1 parent 4912c4e commit b09770d
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/main/java/ru/ewc/checklogic/server/AllEndpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, TemplateProcessor> 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);
}
}
41 changes: 41 additions & 0 deletions src/main/java/ru/ewc/checklogic/server/TemplateProcessors.java
Original file line number Diff line number Diff line change
@@ -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);
}
55 changes: 55 additions & 0 deletions src/main/java/ru/ewc/checklogic/server/WebPages.java
Original file line number Diff line number Diff line change
@@ -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<String, String> values) {
return this.processors.forTemplate(template).renderTemplate(values);
}
}
44 changes: 44 additions & 0 deletions src/main/resources/templates/uninitialized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!--
~ 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.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>LC Home</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/htmx.org@1.9.12"
integrity="sha384-ujb1lZYygJmzgSwoxRggbCHcjc0rB2XoQrxeTUQyRjrOnlCoYta87iKBWq3EsdM2"
crossorigin="anonymous"></script>
<script src="https://unpkg.com/hyperscript.org@0.9.12"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
<link rel="stylesheet" href="static/main.css">
</head>
<body class="container">
<h1>Server is not initialized</h1>
<button class="btn btn-primary">Create barebones context</button>
</body>
</html>

1 comment on commit b09770d

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on b09770d Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 47-3152e263 disappeared from src/main/java/ru/ewc/checklogic/server/AllEndpoints.java), that's why I closed #48. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.