Skip to content

Commit

Permalink
Finished Info servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
FabiPunktExe committed Mar 23, 2024
1 parent 27257de commit bda3bc5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=0.1.5
version=0.2.0
group=diruptio
jetbrains_annotations_version=24.1.0
gson_version=2.10.1
Expand Down
29 changes: 27 additions & 2 deletions info-module/src/main/java/diruptio/spikedog/info/InfoServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import diruptio.spikedog.HttpRequest;
import diruptio.spikedog.HttpResponse;
import diruptio.spikedog.Module;
import diruptio.spikedog.ModuleLoader;
import diruptio.spikedog.Spikedog;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiConsumer;

public class InfoServlet implements BiConsumer<HttpRequest, HttpResponse> {
Expand All @@ -26,7 +32,26 @@ public void accept(HttpRequest request, HttpResponse response) {

// Authorized
response.setStatus(200, "OK");
response.setHeader("Content-Type", "text/plain");
response.setContent("<h1>Spikedog Info</h1>");
response.setHeader("Content-Type", "text/html");
StringBuilder content = new StringBuilder("<html>");
content.append("<head><title>Spikedog Info></title></head>");
content.append("<body>");
content.append("<h1>Spikedog Info</h1>");
content.append("<b>Version:</b> ").append(Spikedog.VERSION).append("<hr>");
content.append("<b>Modules:</b><ul>");
List<Module> modules = new ArrayList<>(ModuleLoader.getModules());
modules.sort(Comparator.comparing(module -> module.file().getFileName().toString()));
for (Module module : modules) {
content.append("<li>").append(module.file().getFileName()).append("</li>");
}
content.append("</ul><hr><b>Servlets:</b><ul>");
List<Spikedog.Servlet> servlets = new ArrayList<>(Spikedog.getServlets());
servlets.sort(Comparator.comparing(Spikedog.Servlet::path));
for (Spikedog.Servlet servlet : servlets) {
content.append("<li>").append(servlet.path()).append("</li>");
}
content.append("</body>");
content.append("</html>");
response.setContent(content.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void accept(HttpRequest request, HttpResponse response) {

// Authorized
response.setStatus(200, "OK");
response.setHeader("Content-Type", "text/plain");
response.setHeader("Content-Type", "text/html");
response.setContent("<h1>Reloading modules...</h1>");

// Reload
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/diruptio/spikedog/ServeThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void run() {
}

response.setHeader("Content-Length", String.valueOf(response.getContent().length()));
response.setHeader("Server", "Spikedog/" + BuildConstants.VERSION);
response.setHeader("Server", "Spikedog/" + Spikedog.VERSION);
byte[] bytes = response.toString().getBytes();
ByteBuffer buffer = ByteBuffer.wrap(bytes);
while (buffer.hasRemaining()) client.write(buffer);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/diruptio/spikedog/Spikedog.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jetbrains.annotations.NotNull;

public class Spikedog {
public static final String VERSION = BuildConstants.VERSION;
public static final String BIND_ADDRESS = "0.0.0.0";
public static final int PORT = 8080;
public static final Path MODULES_DIRECTORY = Path.of("modules");
Expand Down

0 comments on commit bda3bc5

Please sign in to comment.