Skip to content

Commit

Permalink
feat(server): initializing with a barebones project
Browse files Browse the repository at this point in the history
Closes: #33
  • Loading branch information
nergal-perm committed Jul 31, 2024
1 parent 6215944 commit 3559ed5
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 106 deletions.
33 changes: 16 additions & 17 deletions src/main/java/ru/ewc/checklogic/FullServerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @since 0.1.0
*/
@SuppressWarnings("PMD.ProhibitPublicStaticMethods")
public final class FullServerContext implements ServerContext {
public final class FullServerContext {
/**
* The root path for the external business logic resources.
*/
Expand All @@ -47,7 +47,7 @@ public final class FullServerContext implements ServerContext {
/**
* The stored state of the system, persisted between requests.
*/
private final State state;
private State state;

/**
* The URI of the tables folder, used to recreate the computation context for each request.
Expand All @@ -69,21 +69,25 @@ public final class FullServerContext implements ServerContext {
*/
private final Map<String, String> parameters;

/**
* The factory for the states.
*/
private final StateFactory states;

FullServerContext(final StateFactory initial, final URI tables, final URI commands) {
this.root = initial.getRoot();
this.state = initial.initialState();
this.states = initial;
this.root = this.states.getRoot();
this.state = this.states.initialState();
this.tables = tables;
this.commands = commands;
this.context = new ComputationContext(this.state, tables, commands);
this.parameters = new HashMap<>(2);
}

@Override
public void perform(final String command) {
this.perform(command, Map.of());
}

@Override
public void perform(final String command, final Map<String, String> args) {
args.forEach(
(key, value) -> {
Expand All @@ -96,7 +100,6 @@ public void perform(final String command, final Map<String, String> args) {
this.context = new ComputationContext(this.state, this.tables, this.commands);
}

@Override
public Map<String, String> stateFor(final String table, final Map<String, String> entities) {
final Map<String, String> actual = HashMap.newHashMap(entities.size());
for (final String fragment : entities.keySet()) {
Expand All @@ -105,12 +108,10 @@ public Map<String, String> stateFor(final String table, final Map<String, String
return actual;
}

@Override
public Map<String, Map<String, Object>> storedState() {
return this.context.storedState();
}

@Override
public String valueFor(final String locator, final String fragment) {
String value;
try {
Expand All @@ -121,33 +122,27 @@ public String valueFor(final String locator, final String fragment) {
return value;
}

@Override
public Map<String, List<String>> commandData() {
return this.context.commandData();
}

@Override
public boolean isAvailable(final String command, final String field) {
return "true".equalsIgnoreCase(this.context.decisionFor(command).get(field));
}

@Override
public void update(final List<String> values) {
this.state.locators().put(this.cached("request"), InMemoryStorage.from(values));
this.context = new ComputationContext(this.state, this.tables, this.commands);
}

@Override
public String cached(final String parameter) {
return this.parameters.getOrDefault(parameter, "");
}

@Override
public void cache(final String parameter, final String value) {
this.parameters.put(parameter, value);
}

@Override
public void putLocators(final Map<String, Map<String, Object>> raw) {
raw.forEach(
(name, data) -> {
Expand All @@ -160,13 +155,17 @@ public void putLocators(final Map<String, Map<String, Object>> raw) {
this.context = new ComputationContext(this.state, this.tables, this.commands);
}

@Override
public boolean isEmpty() {
return this.state instanceof NullState;
}

@Override
public String getRoot() {
return this.root;
}

public void initialize() {
this.states.initialize();
this.state = this.states.initialState();
this.context = new ComputationContext(this.state, this.tables, this.commands);
}
}
61 changes: 0 additions & 61 deletions src/main/java/ru/ewc/checklogic/ServerContext.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/ru/ewc/checklogic/ServerContextFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ServerContextFactory(final String root) {
*
* @return A new server context initialized with the basic set of empty Locators.
*/
public ServerContext initialState() {
public FullServerContext initialState() {
final FullServerContext result = new FullServerContext(
new StateFactory(this.root),
Path.of(this.root, "tables").toUri(),
Expand All @@ -64,7 +64,7 @@ public ServerContext initialState() {
* @param file The stream of the test file's contents.
* @return A new server context initialized with state described in test file.
*/
public ServerContext fromStateFile(final InputStream file) {
public FullServerContext fromStateFile(final InputStream file) {
return new FullServerContext(
new StateFactory(this.root).with(file),
Path.of(this.root, "tables").toUri(),
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/ru/ewc/checklogic/StateFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
*/
package ru.ewc.checklogic;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
Expand Down Expand Up @@ -76,6 +79,16 @@ public StateFactory with(final InputStream file) {
return this;
}

@SneakyThrows
public void initialize() {
final File config = Path.of(this.root, "application.yaml").toFile();
if (!config.exists() && config.createNewFile()) {
try (OutputStream out = Files.newOutputStream(config.toPath())) {
out.write("locators:\n - request\n".getBytes(StandardCharsets.UTF_8));
}
}
}

@SuppressWarnings("unchecked")
private Map<String, Locator> locatorsFromFile() {
final Map<String, Locator> locators = new HashMap<>();
Expand Down
33 changes: 25 additions & 8 deletions src/main/java/ru/ewc/checklogic/server/AllEndpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import com.renomad.minum.web.Request;
import com.renomad.minum.web.Response;
import com.renomad.minum.web.WebFramework;
import ru.ewc.checklogic.ServerContext;
import java.util.Map;
import ru.ewc.checklogic.FullServerContext;

/**
* I am a class providing access to all the pages and static files packed inside the jar.
Expand All @@ -37,28 +38,44 @@ public final class AllEndpoints implements Endpoints {
/**
* The context to be used for the server.
*/
private final ServerContext context;
private final FullServerContext context;

/**
* The template renderer, creating pages to be served.
*/
private final WebPages pages;

public AllEndpoints(final ServerContext context) {
public AllEndpoints(final FullServerContext context) {
this.context = context;
this.pages = new WebPages(new ResourceTemplateProcessors(), context.getRoot());
}

@Override
public void register(final WebFramework web) {
web.registerPartialPath(GET, "static", AllEndpoints::staticResource);
web.registerPath(GET, "", this::getRequestDispatcher);
web.registerPath(GET, "test", this::getRequestDispatcher);
web.registerPath(GET, "state", this::getRequestDispatcher);
web.registerPath(GET, "", this::httpGetRouter);
web.registerPath(GET, "test", this::httpGetRouter);
web.registerPath(GET, "state", this::httpGetRouter);
web.registerPath(POST, "state", this::httpPostRouter);
}

@SuppressWarnings("PMD.UnusedFormalParameter")
private Response getRequestDispatcher(final Request request) {
private Response httpPostRouter(final Request request) {
final Response result;
final String address = request.requestLine().getPathDetails().getIsolatedPath();
if (this.context.isEmpty()) {
if ("state".equals(address)) {
this.context.initialize();
result = Response.htmlOk("OK", Map.of("HX-Redirect", "/"));
} else {
result = this.pages.uninitializedPage();
}
} else {
result = new Response(NOT_FOUND, "", PLAIN_TEXT);
}
return result;
}

private Response httpGetRouter(final Request request) {
final Response result;
if (this.context.isEmpty()) {
result = this.pages.uninitializedPage();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/ru/ewc/checklogic/server/CommandMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import ru.ewc.checklogic.ServerContext;
import ru.ewc.checklogic.FullServerContext;

/**
* I am a class encapsulating commands metadata. My main responsibility is to present that metadata
Expand Down Expand Up @@ -62,7 +62,7 @@ public CommandMetadata(final Map<String, List<String>> metadata) {
* @param outcome The decision table's outcome field that shows command's availability.
* @return The command names as an HTML list to be used in a page template.
*/
public String namesAsHtmlList(final ServerContext computation, final String outcome) {
public String namesAsHtmlList(final FullServerContext computation, final String outcome) {
return this.names.stream().map(
command -> """
<button class="btn %2$s"
Expand All @@ -79,7 +79,7 @@ public String namesAsHtmlList(final ServerContext computation, final String outc
* @param context The server context to get the existing arguments values from.
* @return The command arguments as an HTML form to be used in a page template.
*/
public String commandArgsAsHtmlForm(final String command, final ServerContext context) {
public String commandArgsAsHtmlForm(final String command, final FullServerContext context) {
return this.metadata.get(command).stream()
.distinct()
.filter(arg -> context.cached("request").equals(arg.split("::")[0]))
Expand All @@ -96,7 +96,7 @@ public String commandArgsAsHtmlForm(final String command, final ServerContext co
}

private static String buttonCssClass(
final ServerContext computation,
final FullServerContext computation,
final String command,
final String availability
) {
Expand All @@ -109,7 +109,7 @@ private static String buttonCssClass(
return result;
}

private static String extractValue(final ServerContext context, final String arg) {
private static String extractValue(final FullServerContext context, final String arg) {
return context.valueFor(arg.split("::")[0], arg.split("::")[1]);
}
}
6 changes: 3 additions & 3 deletions src/main/java/ru/ewc/checklogic/server/CommandPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import ru.ewc.checklogic.ServerContext;
import ru.ewc.checklogic.FullServerContext;
import ru.ewc.decisions.api.DecitaException;

/**
Expand All @@ -44,7 +44,7 @@ public final class CommandPage implements Endpoints {
/**
* The computation to be used for the command processing.
*/
private final ServerContext computation;
private final FullServerContext computation;

/**
* The template processor for the Command page.
Expand All @@ -61,7 +61,7 @@ public final class CommandPage implements Endpoints {
*
* @param computation The computation to be used for the command processing.
*/
public CommandPage(final ServerContext computation) {
public CommandPage(final FullServerContext computation) {
this.computation = computation;
this.description = TemplateProcessor.buildProcessor(
WebResource.readFileFromResources("templates/command-info.html")
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ru/ewc/checklogic/server/ContextPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;
import ru.ewc.checklogic.ServerContext;
import ru.ewc.checklogic.FullServerContext;

/**
* I am the configuration and logic for the context web page.
Expand All @@ -41,9 +41,9 @@ public final class ContextPage implements Endpoints {
/**
* The server context.
*/
private final ServerContext context;
private final FullServerContext context;

public ContextPage(final ServerContext context) {
public ContextPage(final FullServerContext context) {
this.context = context;
}

Expand Down
Loading

0 comments on commit 3559ed5

Please sign in to comment.