From ffdff60a109d6e3360c46ce5302cf92cf640db7b Mon Sep 17 00:00:00 2001 From: linux-yankee Date: Sat, 13 Jan 2024 11:08:18 -0800 Subject: [PATCH] Added php --- .../mcwebserver/config/ModConfigs.java | 4 ++ .../mcwebserver/web/ServerHandler.java | 6 ++- .../mcwebserver/web/php/PhpServer.java | 38 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/jonasjones/mcwebserver/web/php/PhpServer.java diff --git a/src/main/java/me/jonasjones/mcwebserver/config/ModConfigs.java b/src/main/java/me/jonasjones/mcwebserver/config/ModConfigs.java index 8c1f077..f74522d 100644 --- a/src/main/java/me/jonasjones/mcwebserver/config/ModConfigs.java +++ b/src/main/java/me/jonasjones/mcwebserver/config/ModConfigs.java @@ -18,6 +18,8 @@ public class ModConfigs { public static Boolean SERVER_API_ENABLED; public static Boolean ADV_API_ENABLED; public static String WEB_FILE_NOSUPPORT; + + public static Boolean PHP_ENABLED; public static Boolean VERBOSE = false; //needs to be set to false since the verbose logger is called before config file is fully loaded @@ -46,6 +48,7 @@ public class ModConfigs { config.addKeyValuePair(new Pair<>("web.api.adv", true), "whether or not the api should expose information such as player coordinates and inventory"); config.addKeyValuePair(new Pair<>("web.file.notSupported", "not_supported.html"), "the name of the html file for 'not supported' page"); config.addKeyValuePair(new Pair<>("logger.verbose", true), "whether or not to log verbose output"); + config.addKeyValuePair(new Pair<>("web.php", false), "enable php"); } private static void assignConfigs() { @@ -58,5 +61,6 @@ public class ModConfigs { ADV_API_ENABLED = CONFIG.getOrDefault("web.api.adv", false); WEB_FILE_NOSUPPORT = CONFIG.getOrDefault("web.file.notSupported", "not_supported.html"); VERBOSE = CONFIG.getOrDefault("logger.verbose", true); + PHP_ENABLED = CONFIG.getOrDefault("web.php", false); } } diff --git a/src/main/java/me/jonasjones/mcwebserver/web/ServerHandler.java b/src/main/java/me/jonasjones/mcwebserver/web/ServerHandler.java index 536c0af..ed15b2b 100644 --- a/src/main/java/me/jonasjones/mcwebserver/web/ServerHandler.java +++ b/src/main/java/me/jonasjones/mcwebserver/web/ServerHandler.java @@ -1,6 +1,7 @@ package me.jonasjones.mcwebserver.web; import me.jonasjones.mcwebserver.config.ModConfigs; +import me.jonasjones.mcwebserver.web.php.PhpServer; import net.fabricmc.loader.api.FabricLoader; import java.net.HttpURLConnection; @@ -75,11 +76,14 @@ public class ServerHandler implements Runnable { } public void run() { - if (ModConfigs.IS_ENABLED) { + if ((ModConfigs.IS_ENABLED && !ModConfigs.PHP_ENABLED)) { LOGGER.info("Starting Webserver..."); new HttpServer(socket); HttpServer.main(); + } else if (ModConfigs.PHP_ENABLED) { + LOGGER.info("Starting php Webserver..."); + PhpServer.main(); } else { LOGGER.info("Webserver disabled in the config file."); } diff --git a/src/main/java/me/jonasjones/mcwebserver/web/php/PhpServer.java b/src/main/java/me/jonasjones/mcwebserver/web/php/PhpServer.java new file mode 100644 index 0000000..a1f84db --- /dev/null +++ b/src/main/java/me/jonasjones/mcwebserver/web/php/PhpServer.java @@ -0,0 +1,38 @@ +package me.jonasjones.mcwebserver.web.php; + +import me.jonasjones.mcwebserver.config.ModConfigs; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +public class PhpServer { + + public static void main() { + try { + + String documentRoot = ModConfigs.WEB_ROOT; + + // Set the port number + int port = ModConfigs.WEB_PORT; + + // Build the command to start the PHP built-in web server + String command = String.format("php -S localhost:%d -t %s", port, documentRoot); + + // Start the process + ProcessBuilder processBuilder = new ProcessBuilder(command.split("\\s+")); + processBuilder.redirectErrorStream(true); + Process process = processBuilder.start(); + + // Read the output + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + + System.out.println("PHP server is running. Press Enter to stop."); + } catch (IOException e) { + e.printStackTrace(); + } + } +}