2024-01-03 18:59:04 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
2024-02-23 19:16:36 +01:00
|
|
|
import io.github.pixee.security.SystemCommand;
|
2024-01-03 18:59:04 +01:00
|
|
|
import org.springframework.boot.SpringApplication;
|
|
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
2024-02-23 19:16:36 +01:00
|
|
|
import org.springframework.context.ApplicationContextInitializer;
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
2024-01-03 18:59:04 +01:00
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
|
2024-02-23 19:16:36 +01:00
|
|
|
import static java.nio.file.Files.createDirectories;
|
2024-01-03 18:59:04 +01:00
|
|
|
|
|
|
|
@SpringBootApplication
|
|
|
|
@EnableScheduling
|
|
|
|
public class SPdfApplication {
|
|
|
|
|
2024-02-23 19:16:36 +01:00
|
|
|
private static Environment env;
|
|
|
|
|
|
|
|
public SPdfApplication(Environment env) {
|
|
|
|
SPdfApplication.env = env;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
SpringApplication app = new SpringApplication(SPdfApplication.class);
|
|
|
|
app.addInitializers((ApplicationContextInitializer<ConfigurableApplicationContext>) applicationContext -> {
|
|
|
|
if (Files.exists(Paths.get("configs/settings.yml"))) {
|
|
|
|
app.setDefaultProperties(
|
|
|
|
Collections.singletonMap(
|
|
|
|
"spring.config.additional-location", "file:configs/settings.yml"));
|
|
|
|
} else {
|
|
|
|
System.out.println(
|
|
|
|
"External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead.");
|
|
|
|
}
|
|
|
|
ConfigurableApplicationContext context = app.run(args);
|
|
|
|
|
|
|
|
createDirectories();
|
|
|
|
printStartupMessage(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void createDirectories() {
|
|
|
|
try {
|
|
|
|
createDirectories(Paths.get("customFiles/static/"));
|
|
|
|
createDirectories(Paths.get("customFiles/templates/"));
|
|
|
|
} catch (Exception e) {
|
|
|
|
System.err.println("Error creating directories: " + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void printStartupMessage(ConfigurableApplicationContext context) {
|
|
|
|
String port = context.getEnvironment().getProperty("local.server.port", "8080");
|
|
|
|
String url = "http://localhost:" + port;
|
|
|
|
System.out.println("Stirling-PDF Started.");
|
|
|
|
System.out.println("Navigate to " + url);
|
|
|
|
|
|
|
|
// Open browser if BROWSER_OPEN environment variable is set to true
|
|
|
|
openBrowserIfRequired(context);
|
|
|
|
}
|
2024-01-03 18:59:04 +01:00
|
|
|
|
2024-02-23 19:16:36 +01:00
|
|
|
private static void openBrowserIfRequired(ConfigurableApplicationContext context) {
|
|
|
|
Environment environment = context.getEnvironment();
|
|
|
|
String browserOpenEnv = environment.getProperty("BROWSER_OPEN");
|
2024-02-02 01:29:18 +01:00
|
|
|
boolean browserOpen = browserOpenEnv != null && "true".equalsIgnoreCase(browserOpenEnv);
|
2024-01-03 18:59:04 +01:00
|
|
|
|
|
|
|
if (browserOpen) {
|
|
|
|
try {
|
2024-02-23 19:16:36 +01:00
|
|
|
String url = "http://localhost:" + context.getEnvironment().getProperty("local.server.port", "8080");
|
2024-01-03 18:59:04 +01:00
|
|
|
|
|
|
|
String os = System.getProperty("os.name").toLowerCase();
|
|
|
|
Runtime rt = Runtime.getRuntime();
|
|
|
|
if (os.contains("win")) {
|
|
|
|
// For Windows
|
2024-02-02 00:18:24 +01:00
|
|
|
SystemCommand.runCommand(rt, "rundll32 url.dll,FileProtocolHandler " + url);
|
2024-01-03 18:59:04 +01:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2024-02-23 19:16:36 +01:00
|
|
|
System.err.println("Error opening browser: " + e.getMessage());
|
2024-01-03 18:59:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|