Files
Stirling-PDF/src/main/java/stirling/software/SPDF/SPDFApplication.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

240 lines
8.7 KiB
Java
Raw Normal View History

2024-02-23 23:46:42 +05:30
package stirling.software.SPDF;
2024-02-24 00:01:20 +05:30
import java.io.IOException;
2025-01-06 12:41:30 +00:00
import java.net.URISyntaxException;
2024-01-03 17:59:04 +00:00
import java.nio.file.Files;
2024-02-24 00:01:20 +05:30
import java.nio.file.Path;
2024-01-03 17:59:04 +00:00
import java.nio.file.Paths;
import java.util.Collections;
2024-05-03 20:43:48 +01:00
import java.util.HashMap;
import java.util.Map;
2024-12-11 21:54:05 +00:00
import java.util.Properties;
2024-02-23 23:46:42 +05:30
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
2024-01-03 17:59:04 +00:00
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
2025-05-27 13:01:52 +01:00
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
2024-01-03 17:59:04 +00:00
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
2024-12-13 16:58:34 +00:00
import io.github.pixee.security.SystemCommand;
2024-12-17 10:26:18 +01:00
2024-02-23 23:46:42 +05:30
import jakarta.annotation.PostConstruct;
2024-12-11 21:54:05 +00:00
import jakarta.annotation.PreDestroy;
2024-12-11 21:54:05 +00:00
import lombok.extern.slf4j.Slf4j;
2024-12-11 21:54:05 +00:00
import stirling.software.SPDF.UI.WebBrowser;
2025-05-27 13:01:52 +01:00
import stirling.software.common.configuration.ConfigInitializer;
import stirling.software.common.configuration.InstallationPathConfig;
import stirling.software.common.model.ApplicationProperties;
import stirling.software.common.util.UrlUtils;
2024-01-03 17:59:04 +00:00
2024-12-11 21:54:05 +00:00
@Slf4j
2025-01-06 18:58:26 +00:00
@EnableScheduling
2025-05-27 13:01:52 +01:00
@SpringBootApplication(
scanBasePackages = {"stirling.software.common", "stirling.software.SPDF"},
exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class
})
2025-01-06 18:58:26 +00:00
public class SPDFApplication {
2024-01-03 17:59:04 +00:00
private static String serverPortStatic;
2025-01-06 18:58:26 +00:00
private static String baseUrlStatic;
private static String contextPathStatic;
2025-01-06 18:58:26 +00:00
private final Environment env;
private final ApplicationProperties applicationProperties;
private final WebBrowser webBrowser;
@Value("${baseUrl:http://localhost}")
private String baseUrl;
@Value("${server.servlet.context-path:/}")
private String contextPath;
2025-01-06 18:58:26 +00:00
public SPDFApplication(
Environment env,
ApplicationProperties applicationProperties,
@Autowired(required = false) WebBrowser webBrowser) {
this.env = env;
this.applicationProperties = applicationProperties;
this.webBrowser = webBrowser;
}
2024-02-24 00:01:20 +05:30
public static void main(String[] args) throws IOException, InterruptedException {
2025-01-06 18:58:26 +00:00
SpringApplication app = new SpringApplication(SPDFApplication.class);
2024-12-11 22:09:35 +00:00
Properties props = new Properties();
2025-01-06 18:58:26 +00:00
2024-12-13 11:31:49 +00:00
if (Boolean.parseBoolean(System.getProperty("STIRLING_PDF_DESKTOP_UI", "false"))) {
2024-12-11 23:13:23 +00:00
System.setProperty("java.awt.headless", "false");
app.setHeadless(false);
2024-12-13 11:31:49 +00:00
props.put("java.awt.headless", "false");
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);
2024-12-11 21:56:50 +00:00
}
2025-01-06 18:58:26 +00:00
app.setAdditionalProfiles(getActiveProfile(args));
2025-01-06 12:41:30 +00:00
ConfigInitializer initializer = new ConfigInitializer();
try {
initializer.ensureConfigExists();
} catch (IOException | URISyntaxException e) {
log.error("Error initialising configuration", e);
}
2024-05-03 20:43:48 +01:00
Map<String, String> propertyFiles = new HashMap<>();
2025-01-06 18:58:26 +00:00
// External config files
Path settingsPath = Paths.get(InstallationPathConfig.getSettingsPath());
log.info("Settings file: {}", settingsPath.toString());
if (Files.exists(settingsPath)) {
propertyFiles.put(
"spring.config.additional-location", "file:" + settingsPath.toString());
2024-02-23 23:46:42 +05:30
} else {
log.warn("External configuration file '{}' does not exist.", settingsPath.toString());
2024-02-23 23:46:42 +05:30
}
2025-01-06 18:58:26 +00:00
Path customSettingsPath = Paths.get(InstallationPathConfig.getCustomSettingsPath());
log.info("Custom settings file: {}", customSettingsPath.toString());
if (Files.exists(customSettingsPath)) {
2024-08-08 21:13:59 +01:00
String existingLocation =
propertyFiles.getOrDefault("spring.config.additional-location", "");
2024-07-31 13:25:48 -07:00
if (!existingLocation.isEmpty()) {
existingLocation += ",";
2024-05-03 20:43:48 +01:00
}
propertyFiles.put(
"spring.config.additional-location",
existingLocation + "file:" + customSettingsPath.toString());
2024-05-03 20:43:48 +01:00
} else {
log.warn(
"Custom configuration file '{}' does not exist.",
customSettingsPath.toString());
2024-05-03 20:43:48 +01:00
}
2024-12-11 22:09:35 +00:00
Properties finalProps = new Properties();
2025-01-06 18:58:26 +00:00
2024-12-11 23:13:23 +00:00
if (!propertyFiles.isEmpty()) {
finalProps.putAll(
Collections.singletonMap(
"spring.config.additional-location",
propertyFiles.get("spring.config.additional-location")));
}
2025-01-06 18:58:26 +00:00
2024-12-13 11:31:49 +00:00
if (!props.isEmpty()) {
2024-12-11 23:13:23 +00:00
finalProps.putAll(props);
}
2024-12-11 22:09:35 +00:00
app.setDefaultProperties(finalProps);
2025-01-06 18:58:26 +00:00
2024-02-23 23:46:42 +05:30
app.run(args);
2025-01-06 18:58:26 +00:00
// Ensure directories are created
2024-02-24 00:01:20 +05:30
try {
2025-01-06 12:41:30 +00:00
Files.createDirectories(Path.of(InstallationPathConfig.getTemplatesPath()));
Files.createDirectories(Path.of(InstallationPathConfig.getStaticPath()));
2025-02-25 22:31:20 +01:00
} catch (IOException e) {
2024-12-17 10:26:18 +01:00
log.error("Error creating directories: {}", e.getMessage());
2024-02-24 00:01:20 +05:30
}
2025-01-06 18:58:26 +00:00
2024-02-24 00:01:20 +05:30
printStartupLogs();
}
2024-02-23 23:46:42 +05:30
2024-12-11 21:54:05 +00:00
@PostConstruct
public void init() {
baseUrlStatic = this.baseUrl;
contextPathStatic = this.contextPath;
String url = baseUrl + ":" + getStaticPort() + contextPath;
2024-12-13 11:31:49 +00:00
if (webBrowser != null
&& Boolean.parseBoolean(System.getProperty("STIRLING_PDF_DESKTOP_UI", "false"))) {
2024-12-11 21:54:05 +00:00
webBrowser.initWebUI(url);
2024-12-13 16:58:34 +00:00
} else {
2024-12-17 10:26:18 +01:00
String browserOpenEnv = env.getProperty("BROWSER_OPEN");
2024-12-13 16:58:34 +00:00
boolean browserOpen = browserOpenEnv != null && "true".equalsIgnoreCase(browserOpenEnv);
if (browserOpen) {
try {
String os = System.getProperty("os.name").toLowerCase();
Runtime rt = Runtime.getRuntime();
if (os.contains("win")) {
// For Windows
SystemCommand.runCommand(rt, "rundll32 url.dll,FileProtocolHandler " + url);
} else if (os.contains("mac")) {
SystemCommand.runCommand(rt, "open " + url);
} else if (os.contains("nix") || os.contains("nux")) {
SystemCommand.runCommand(rt, "xdg-open " + url);
}
} catch (IOException e) {
2024-12-17 10:26:18 +01:00
log.error("Error opening browser: {}", e.getMessage());
2024-12-13 16:58:34 +00:00
}
}
2024-12-11 21:54:05 +00:00
}
2024-12-17 10:26:18 +01:00
log.info("Running configs {}", applicationProperties.toString());
2024-12-11 21:54:05 +00:00
}
2025-01-06 18:58:26 +00:00
@Value("${server.port:8080}")
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) {
2025-01-06 18:58:26 +00:00
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;
}
}
2024-12-11 21:54:05 +00:00
@PreDestroy
public void cleanup() {
if (webBrowser != null) {
webBrowser.cleanup();
}
}
2025-01-06 18:58:26 +00:00
private static void printStartupLogs() {
log.info("Stirling-PDF Started.");
String url = baseUrlStatic + ":" + getStaticPort() + contextPathStatic;
2025-01-06 18:58:26 +00:00
log.info("Navigate to {}", url);
}
private static String[] getActiveProfile(String[] args) {
if (args == null) {
return new String[] {"default"};
}
for (String arg : args) {
if (arg.contains("spring.profiles.active")) {
return arg.substring(args[0].indexOf('=') + 1).split(", ");
}
}
return new String[] {"default"};
}
public static String getStaticBaseUrl() {
return baseUrlStatic;
}
public static String getStaticPort() {
return serverPortStatic;
}
public static String getStaticContextPath() {
return contextPathStatic;
}
2024-01-03 17:59:04 +00:00
}