opcua-service/main/java/de/opcua/app/RestApiLauncher.java
2026-05-11 19:40:18 +02:00

83 lines
4.2 KiB
Java

package de.opcua.app;
import de.opcua.app.opc.OpcUaService;
import de.opcua.app.rest.OpcUaRestApi;
import de.opcua.app.scripting.Store;
import de.opcua.app.service.ActionService;
/**
* Standalone REST API launcher for debugging
* Run with: mvn exec:java -Dexec.mainClass="de.opcua.app.RestApiLauncher"
*/
public class RestApiLauncher {
public static void main(String[] args) {
System.out.println("═══════════════════════════════════════════");
System.out.println(" REST API Standalone Launcher");
System.out.println("═══════════════════════════════════════════");
System.out.println();
try {
System.out.println("[1/3] Creating OPC UA Service...");
OpcUaService opc = new OpcUaService();
System.out.println("✅ OPC UA Service created");
System.out.println();
System.out.println("[2/3] Creating REST API on port 8081...");
ActionService actionService = new ActionService(opc, new Store());
OpcUaRestApi api = new OpcUaRestApi(opc, 8081, actionService);
System.out.println("✅ REST API object created");
System.out.println();
System.out.println("[3/3] Starting HTTP Server...");
api.start();
System.out.println("✅ HTTP Server started successfully!");
System.out.println();
System.out.println("═══════════════════════════════════════════");
System.out.println(" ✅ REST API is now running!");
System.out.println("═══════════════════════════════════════════");
System.out.println();
System.out.println("Test with:");
System.out.println(" curl http://localhost:8081/");
System.out.println(" curl http://localhost:8081/api/status");
System.out.println();
System.out.println("Or open in browser:");
System.out.println(" http://localhost:8081/");
System.out.println();
System.out.println("Press Ctrl+C to stop...");
System.out.println("─────────────────────────────────────────");
// Keep alive
Thread.sleep(Long.MAX_VALUE);
} catch (Exception e) {
System.err.println();
System.err.println("═══════════════════════════════════════════");
System.err.println(" ❌ ERROR - Failed to start REST API!");
System.err.println("═══════════════════════════════════════════");
System.err.println();
System.err.println("Error message: " + e.getMessage());
System.err.println();
System.err.println("Stack trace:");
e.printStackTrace();
System.err.println();
System.err.println("Common causes:");
System.err.println("1. Port 8081 already in use");
System.err.println(" Check: lsof -i :8081");
System.err.println(" Or: netstat -an | grep 8081");
System.err.println();
System.err.println("2. com.sun.net.httpserver not available");
System.err.println(" Make sure you're using JDK (not JRE)");
System.err.println(" Check: java -version");
System.err.println();
System.err.println("3. Firewall blocking port");
System.err.println(" Try: sudo ufw allow 8081 (Linux)");
System.err.println(" Or check System Preferences (macOS)");
System.err.println();
System.exit(1);
}
}
}