2024-02-23 19:16:42 +01:00
|
|
|
package stirling.software.SPDF;
|
|
|
|
|
2024-02-23 19:31:20 +01:00
|
|
|
import java.io.IOException;
|
2024-01-03 18:59:04 +01:00
|
|
|
import java.nio.file.Files;
|
2024-02-23 19:31:20 +01:00
|
|
|
import java.nio.file.Path;
|
2024-01-03 18:59:04 +01:00
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
2024-03-04 21:51:49 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2024-02-23 19:16:42 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2024-02-26 01:15:03 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2024-01-03 18:59:04 +01:00
|
|
|
import org.springframework.boot.SpringApplication;
|
|
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
2024-03-04 21:51:49 +01:00
|
|
|
|
2024-02-23 19:16:42 +01:00
|
|
|
import io.github.pixee.security.SystemCommand;
|
|
|
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
|
|
import stirling.software.SPDF.config.ConfigInitializer;
|
2024-03-07 21:12:07 +01:00
|
|
|
import stirling.software.SPDF.model.ApplicationProperties;
|
2024-01-03 18:59:04 +01:00
|
|
|
|
|
|
|
@SpringBootApplication
|
|
|
|
@EnableScheduling
|
|
|
|
public class SPdfApplication {
|
|
|
|
|
2024-02-23 19:31:20 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SPdfApplication.class);
|
|
|
|
|
2024-02-23 19:16:42 +01:00
|
|
|
@Autowired private Environment env;
|
2024-01-03 18:59:04 +01:00
|
|
|
|
2024-03-07 21:12:07 +01:00
|
|
|
@Autowired ApplicationProperties applicationProperties;
|
|
|
|
|
2024-02-26 01:15:03 +01:00
|
|
|
private static String serverPortStatic;
|
|
|
|
|
|
|
|
@Value("${server.port:8080}")
|
|
|
|
public void setServerPortStatic(String port) {
|
|
|
|
SPdfApplication.serverPortStatic = port;
|
|
|
|
}
|
|
|
|
|
2024-02-23 19:16:42 +01:00
|
|
|
@PostConstruct
|
|
|
|
public void init() {
|
|
|
|
// Check if the BROWSER_OPEN environment variable is set to true
|
|
|
|
String browserOpenEnv = env.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-26 01:15:03 +01:00
|
|
|
String url = "http://localhost:" + getNonStaticPort();
|
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:31:20 +01:00
|
|
|
logger.error("Error opening browser: {}", e.getMessage());
|
2024-01-03 18:59:04 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-07 21:12:07 +01:00
|
|
|
logger.info("Running configs {}", applicationProperties.toString());
|
2024-01-03 18:59:04 +01:00
|
|
|
}
|
2024-02-23 19:16:42 +01:00
|
|
|
|
2024-02-23 19:31:20 +01:00
|
|
|
public static void main(String[] args) throws IOException, InterruptedException {
|
2024-02-23 19:16:42 +01:00
|
|
|
SpringApplication app = new SpringApplication(SPdfApplication.class);
|
|
|
|
app.addInitializers(new ConfigInitializer());
|
|
|
|
if (Files.exists(Paths.get("configs/settings.yml"))) {
|
|
|
|
app.setDefaultProperties(
|
|
|
|
Collections.singletonMap(
|
|
|
|
"spring.config.additional-location", "file:configs/settings.yml"));
|
|
|
|
} else {
|
2024-03-04 21:51:49 +01:00
|
|
|
logger.warn(
|
|
|
|
"External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead.");
|
2024-02-23 19:16:42 +01:00
|
|
|
}
|
|
|
|
app.run(args);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
} catch (InterruptedException e) {
|
2024-02-24 21:22:47 +01:00
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
throw new RuntimeException("Thread interrupted while sleeping", e);
|
2024-02-23 19:16:42 +01:00
|
|
|
}
|
|
|
|
|
2024-02-23 19:31:20 +01:00
|
|
|
try {
|
|
|
|
Files.createDirectories(Path.of("customFiles/static/"));
|
|
|
|
Files.createDirectories(Path.of("customFiles/templates/"));
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Error creating directories: {}", e.getMessage());
|
|
|
|
}
|
|
|
|
printStartupLogs();
|
|
|
|
}
|
2024-02-23 19:16:42 +01:00
|
|
|
|
2024-02-23 19:31:20 +01:00
|
|
|
private static void printStartupLogs() {
|
|
|
|
logger.info("Stirling-PDF Started.");
|
2024-02-26 01:15:03 +01:00
|
|
|
String url = "http://localhost:" + getStaticPort();
|
2024-02-23 19:31:20 +01:00
|
|
|
logger.info("Navigate to {}", url);
|
2024-02-23 19:16:42 +01:00
|
|
|
}
|
|
|
|
|
2024-02-26 01:15:03 +01:00
|
|
|
public static String getStaticPort() {
|
|
|
|
return serverPortStatic;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getNonStaticPort() {
|
|
|
|
return serverPortStatic;
|
2024-02-23 19:16:42 +01:00
|
|
|
}
|
2024-01-03 18:59:04 +01:00
|
|
|
}
|