dynamic port for UI from 8080 up (8081 etc)

This commit is contained in:
Anthony Stirling
2025-02-23 23:46:38 +00:00
parent ee6fbdd61f
commit 71cdc640c0
5 changed files with 42 additions and 37 deletions

View File

@@ -1,7 +1,6 @@
package stirling.software.SPDF; package stirling.software.SPDF;
import java.io.IOException; import java.io.IOException;
import java.net.ServerSocket;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@@ -29,6 +28,7 @@ import stirling.software.SPDF.UI.WebBrowser;
import stirling.software.SPDF.config.ConfigInitializer; import stirling.software.SPDF.config.ConfigInitializer;
import stirling.software.SPDF.config.InstallationPathConfig; import stirling.software.SPDF.config.InstallationPathConfig;
import stirling.software.SPDF.model.ApplicationProperties; import stirling.software.SPDF.model.ApplicationProperties;
import stirling.software.SPDF.utils.UrlUtils;
@Slf4j @Slf4j
@EnableScheduling @EnableScheduling
@@ -64,6 +64,12 @@ public class SPDFApplication {
app.setHeadless(false); app.setHeadless(false);
props.put("java.awt.headless", "false"); props.put("java.awt.headless", "false");
props.put("spring.main.web-application-type", "servlet"); props.put("spring.main.web-application-type", "servlet");
int desiredPort = 8080;
String port = UrlUtils.findAvailablePort(desiredPort);
props.put("server.port", port);
System.setProperty("server.port", port);
log.info("Desktop UI mode: Using port {}", port);
} }
app.setAdditionalProfiles(getActiveProfile(args)); app.setAdditionalProfiles(getActiveProfile(args));
@@ -160,7 +166,17 @@ public class SPDFApplication {
} }
@Value("${server.port:8080}") @Value("${server.port:8080}")
public void setServerPortStatic(String port) { public void setServerPort(String port) {
if ("auto".equalsIgnoreCase(port)) {
// Use Spring Boot's automatic port assignment (server.port=0)
SPDFApplication.serverPortStatic =
"0"; // This will let Spring Boot assign an available port
} else {
SPDFApplication.serverPortStatic = port;
}
}
public static void setServerPortStatic(String port) {
if ("auto".equalsIgnoreCase(port)) { if ("auto".equalsIgnoreCase(port)) {
// Use Spring Boot's automatic port assignment (server.port=0) // Use Spring Boot's automatic port assignment (server.port=0)
SPDFApplication.serverPortStatic = SPDFApplication.serverPortStatic =
@@ -197,36 +213,11 @@ public class SPDFApplication {
return new String[] {"default"}; return new String[] {"default"};
} }
private static boolean isPortAvailable(int port) {
try (ServerSocket socket = new ServerSocket(port)) {
return true;
} catch (IOException e) {
return false;
}
}
// Optionally keep this method if you want to provide a manual port-incrementation fallback.
private static String findAvailablePort(int startPort) {
int port = startPort;
while (!isPortAvailable(port)) {
port++;
}
return String.valueOf(port);
}
public static String getStaticBaseUrl() { public static String getStaticBaseUrl() {
return baseUrlStatic; return baseUrlStatic;
} }
public String getNonStaticBaseUrl() {
return baseUrlStatic;
}
public static String getStaticPort() { public static String getStaticPort() {
return serverPortStatic; return serverPortStatic;
} }
public String getNonStaticPort() {
return serverPortStatic;
}
} }

View File

@@ -57,12 +57,11 @@ public class RuntimePathConfig {
this.pipelineDefaultWebUiConfigs = webUiConfigsPath; this.pipelineDefaultWebUiConfigs = webUiConfigsPath;
boolean isDocker = isRunningInDocker(); boolean isDocker = isRunningInDocker();
// Initialize Operation paths // Initialize Operation paths
String weasyPrintPath = isDocker ? "/opt/venv/bin/weasyprint" : "weasyprint"; String weasyPrintPath = isDocker ? "/opt/venv/bin/weasyprint" : "weasyprint";
String unoConvertPath = isDocker ? "/opt/venv/bin/unoconvert" : "unoconvert"; String unoConvertPath = isDocker ? "/opt/venv/bin/unoconvert" : "unoconvert";
// Check for custom operation paths // Check for custom operation paths
Operations operations = properties.getSystem().getCustomPaths().getOperations(); Operations operations = properties.getSystem().getCustomPaths().getOperations();
if (operations != null) { if (operations != null) {
@@ -78,9 +77,8 @@ public class RuntimePathConfig {
this.weasyPrintPath = weasyPrintPath; this.weasyPrintPath = weasyPrintPath;
this.unoConvertPath = unoConvertPath; this.unoConvertPath = unoConvertPath;
} }
private boolean isRunningInDocker() { private boolean isRunningInDocker() {
return Files.exists(Paths.get("/.dockerenv")); return Files.exists(Paths.get("/.dockerenv"));
} }
} }

View File

@@ -52,7 +52,8 @@ public class CompressController {
this.pdfDocumentFactory = pdfDocumentFactory; this.pdfDocumentFactory = pdfDocumentFactory;
} }
private void compressImagesInPDF(Path pdfFile, double initialScaleFactor, boolean grayScale) throws Exception { private void compressImagesInPDF(Path pdfFile, double initialScaleFactor, boolean grayScale)
throws Exception {
byte[] fileBytes = Files.readAllBytes(pdfFile); byte[] fileBytes = Files.readAllBytes(pdfFile);
try (PDDocument doc = Loader.loadPDF(fileBytes)) { try (PDDocument doc = Loader.loadPDF(fileBytes)) {
double scaleFactor = initialScaleFactor; double scaleFactor = initialScaleFactor;

View File

@@ -1,5 +1,8 @@
package stirling.software.SPDF.utils; package stirling.software.SPDF.utils;
import java.io.IOException;
import java.net.ServerSocket;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
public class UrlUtils { public class UrlUtils {
@@ -14,4 +17,20 @@ public class UrlUtils {
return scheme + "://" + serverName + ":" + serverPort + contextPath; return scheme + "://" + serverName + ":" + serverPort + contextPath;
} }
public static boolean isPortAvailable(int port) {
try (ServerSocket socket = new ServerSocket(port)) {
return true;
} catch (IOException e) {
return false;
}
}
public static String findAvailablePort(int startPort) {
int port = startPort;
while (!isPortAvailable(port)) {
port++;
}
return String.valueOf(port);
}
} }

View File

@@ -31,7 +31,7 @@ public class SPDFApplicationTest {
private ApplicationProperties applicationProperties; private ApplicationProperties applicationProperties;
@InjectMocks @InjectMocks
private SPDFApplication SPDFApplication; private SPDFApplication sPDFApplication;
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
@@ -49,8 +49,4 @@ public class SPDFApplicationTest {
assertEquals("8080", SPDFApplication.getStaticPort()); assertEquals("8080", SPDFApplication.getStaticPort());
} }
@Test
public void testGetNonStaticPort() {
assertEquals("8080", SPDFApplication.getNonStaticPort());
}
} }