Skip to content

Commit

Permalink
feat(state): ability to reset state from the web UI
Browse files Browse the repository at this point in the history
Closes: #68
  • Loading branch information
nergal-perm committed Sep 17, 2024
1 parent 773a6ff commit 43ab144
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/ru/ewc/checklogic/LogicChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import ru.ewc.checklogic.server.CommandPage;
import ru.ewc.checklogic.server.ContextPage;
import ru.ewc.checklogic.server.Endpoints;
import ru.ewc.checklogic.server.StatePage;
import ru.ewc.checklogic.server.config.ConfigPage;

/**
Expand Down Expand Up @@ -58,6 +59,7 @@ public static void main(final String[] args) {
registerEndpoints(web, new CommandPage(context));
registerEndpoints(web, new ContextPage(context, factory.configuration()));
registerEndpoints(web, new AllEndpoints(context, factory.configuration()));
registerEndpoints(web, new StatePage(context));
minum.block();
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/ru/ewc/checklogic/server/Endpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public interface Endpoints {
*/
RequestLine.Method POST = RequestLine.Method.POST;

/**
* The shortcut to define an endpoint for a DELETE method.
*/
RequestLine.Method DELETE = RequestLine.Method.DELETE;

/**
* Content type for CSS files.
*/
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/ru/ewc/checklogic/server/StatePage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.Request;
import com.renomad.minum.web.RequestLine;
import com.renomad.minum.web.Response;
import com.renomad.minum.web.WebFramework;
import java.util.Map;
import ru.ewc.checklogic.ServerInstance;

/**
* I am a class providing access to the state page.
*
* @since 0.4.1
*/
public final class StatePage implements Endpoints {
/**
* The context that stores the current state of the system.
*/
private final ServerInstance context;

public StatePage(final ServerInstance context) {
this.context = context;
}

@Override
public void register(final WebFramework web) {
web.registerPath(RequestLine.Method.DELETE, "state", this::resetState);
}

private Response resetState(final Request request) {
assert request.requestLine().getMethod().equals(RequestLine.Method.DELETE);
this.context.initialize();
return Response.htmlOk("OK", Map.of("HX-Redirect", "/state"));
}
}
12 changes: 10 additions & 2 deletions src/main/resources/templates/state.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@
<div class="row">
<div class="col">
<h1>State entities</h1>
{{ state }}
<div class="row">
<div class="col col-4">
<button class="btn btn-primary" hx-delete="/state" hx-trigger="click">Reset</button>
</div>
</div>
<div class="row">
{{ state }}
</div>
</div>
<div class="col">
<h1>Context</h1>
<form class="mb-3" hx-post="/context" hx-target="#commands" hx-swap="innerHTML" hx-trigger="submit, load">
<form class="mb-3" hx-post="/context" hx-target="#commands" hx-swap="innerHTML"
hx-trigger="submit, load">
<div class="mb-3">
<label class="col-form-label" for="reqValues">Request values:</label>
<textarea class="form-control" id="reqValues" name="reqValues" rows="3"
Expand Down

0 comments on commit 43ab144

Please sign in to comment.