Update SPdfApplication.java

This commit is contained in:
Parth P Shah 2024-02-23 23:46:36 +05:30
parent 4e2156ad79
commit 33f8d60900

View File

@ -1,52 +1,30 @@
package stirling.software.SPDF;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Collections; import java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired; import io.github.pixee.security.SystemCommand;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import io.github.pixee.security.SystemCommand; import static java.nio.file.Files.createDirectories;
import jakarta.annotation.PostConstruct;
import stirling.software.SPDF.config.ConfigInitializer;
import stirling.software.SPDF.utils.GeneralUtils;
@SpringBootApplication @SpringBootApplication
@EnableScheduling @EnableScheduling
public class SPdfApplication { public class SPdfApplication {
@Autowired private Environment env; private static Environment env;
@PostConstruct public SPdfApplication(Environment env) {
public void init() { SPdfApplication.env = env;
// Check if the BROWSER_OPEN environment variable is set to true
String browserOpenEnv = env.getProperty("BROWSER_OPEN");
boolean browserOpen = browserOpenEnv != null && "true".equalsIgnoreCase(browserOpenEnv);
if (browserOpen) {
try {
String url = "http://localhost:" + getPort();
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);
}
} catch (Exception e) {
e.printStackTrace();
}
}
} }
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication app = new SpringApplication(SPdfApplication.class); SpringApplication app = new SpringApplication(SPdfApplication.class);
app.addInitializers(new ConfigInitializer()); app.addInitializers((ApplicationContextInitializer<ConfigurableApplicationContext>) applicationContext -> {
if (Files.exists(Paths.get("configs/settings.yml"))) { if (Files.exists(Paths.get("configs/settings.yml"))) {
app.setDefaultProperties( app.setDefaultProperties(
Collections.singletonMap( Collections.singletonMap(
@ -55,29 +33,50 @@ public class SPdfApplication {
System.out.println( System.out.println(
"External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead."); "External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead.");
} }
app.run(args); ConfigurableApplicationContext context = app.run(args);
createDirectories();
printStartupMessage(context);
}
}
private static void createDirectories() {
try { try {
Thread.sleep(1000); createDirectories(Paths.get("customFiles/static/"));
} catch (InterruptedException e) { createDirectories(Paths.get("customFiles/templates/"));
// TODO Auto-generated catch block } catch (Exception e) {
e.printStackTrace(); System.err.println("Error creating directories: " + e.getMessage());
}
} }
GeneralUtils.createDir("customFiles/static/"); private static void printStartupMessage(ConfigurableApplicationContext context) {
GeneralUtils.createDir("customFiles/templates/"); String port = context.getEnvironment().getProperty("local.server.port", "8080");
String url = "http://localhost:" + port;
System.out.println("Stirling-PDF Started."); System.out.println("Stirling-PDF Started.");
String url = "http://localhost:" + getPort();
System.out.println("Navigate to " + url); System.out.println("Navigate to " + url);
// Open browser if BROWSER_OPEN environment variable is set to true
openBrowserIfRequired(context);
} }
public static String getPort() { private static void openBrowserIfRequired(ConfigurableApplicationContext context) {
String port = System.getProperty("local.server.port"); Environment environment = context.getEnvironment();
if (port == null || port.isEmpty()) { String browserOpenEnv = environment.getProperty("BROWSER_OPEN");
port = "8080"; boolean browserOpen = browserOpenEnv != null && "true".equalsIgnoreCase(browserOpenEnv);
if (browserOpen) {
try {
String url = "http://localhost:" + context.getEnvironment().getProperty("local.server.port", "8080");
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);
}
} catch (Exception e) {
System.err.println("Error opening browser: " + e.getMessage());
}
} }
return port;
} }
} }