mirror of
https://github.com/JonasunderscoreJones/McWebserver.git
synced 2025-10-23 19:39:19 +02:00
small changes and added stop method
This commit is contained in:
parent
ffdff60a10
commit
178e38f9ab
2 changed files with 33 additions and 16 deletions
|
@ -3,6 +3,7 @@ package me.jonasjones.mcwebserver.mixin;
|
|||
import me.jonasjones.mcwebserver.McWebserver;
|
||||
import me.jonasjones.mcwebserver.config.ModConfigs;
|
||||
import me.jonasjones.mcwebserver.web.ServerHandler;
|
||||
import me.jonasjones.mcwebserver.web.php.PhpServer;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
|
@ -16,5 +17,6 @@ public class WebserverStopMixin {
|
|||
McWebserver.LOGGER.info("Stopping Webserver...");
|
||||
ModConfigs.VERBOSE = false;
|
||||
ServerHandler.mcserveractive = false;
|
||||
PhpServer.stop();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,38 +1,53 @@
|
|||
package me.jonasjones.mcwebserver.web.php;
|
||||
|
||||
import me.jonasjones.mcwebserver.config.ModConfigs;
|
||||
import me.jonasjones.mcwebserver.McWebserver;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import static me.jonasjones.mcwebserver.config.ModConfigs.WEB_PORT;
|
||||
import static me.jonasjones.mcwebserver.config.ModConfigs.WEB_ROOT;
|
||||
|
||||
public class PhpServer {
|
||||
private static Process process;
|
||||
|
||||
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();
|
||||
process = getProcess();
|
||||
|
||||
// Read the output
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
McWebserver.VERBOSELOGGER.info(line);
|
||||
}
|
||||
|
||||
System.out.println("PHP server is running. Press Enter to stop.");
|
||||
McWebserver.LOGGER.info("PHP server started!");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
McWebserver.LOGGER.error("Error with PHP server: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static Process getProcess() throws IOException {
|
||||
|
||||
// Build the command to start the PHP built-in web server
|
||||
String command = String.format("php -S localhost:%d -t %s", WEB_PORT, WEB_ROOT);
|
||||
|
||||
// Start the process
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(command.split("\\s+"));
|
||||
processBuilder.redirectErrorStream(true);
|
||||
return processBuilder.start();
|
||||
}
|
||||
|
||||
public static void stop() {
|
||||
if (process == null || !process.isAlive()) {
|
||||
return;
|
||||
}
|
||||
process.destroy();
|
||||
McWebserver.LOGGER.info("PHP server stopped!");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue