-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(server): extracted template processors into lazy loaded coll…
…ection Refs: #47
- Loading branch information
1 parent
4912c4e
commit b09770d
Showing
5 changed files
with
202 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/ru/ewc/checklogic/server/ResourceTemplateProcessors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/main/java/ru/ewc/checklogic/server/TemplateProcessors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
b09770d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
47-3152e263
disappeared fromsrc/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.