Skip to content

Commit

Permalink
refactor(server): started creating the single request dispatcher
Browse files Browse the repository at this point in the history
Refs: #47
  • Loading branch information
nergal-perm committed Jul 31, 2024
1 parent 90f9ba4 commit b24e116
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
58 changes: 0 additions & 58 deletions src/main/java/ru/ewc/checklogic/server/IndexPage.java

This file was deleted.

3 changes: 1 addition & 2 deletions src/main/java/ru/ewc/checklogic/server/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

2 comments on commit b24e116

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on b24e116 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 discovered in src/main/java/ru/ewc/checklogic/server/AllEndpoints.java) and submitted as #48. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on b24e116 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-49dd145d discovered in src/main/java/ru/ewc/checklogic/server/AllEndpoints.java) and submitted as #49. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.