Added php

This commit is contained in:
linux-yankee 2024-01-13 11:08:18 -08:00
parent e01da82661
commit ffdff60a10
3 changed files with 47 additions and 1 deletions

View file

@ -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);
}
}

View file

@ -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.");
}

View file

@ -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();
}
}
}