1
0
mirror of https://github.com/Stirling-Tools/Stirling-PDF.git synced 2024-06-30 22:50:11 +02:00

rename to settins.yml

This commit is contained in:
Anthony Stirling 2023-08-26 22:33:23 +01:00
parent d749b63549
commit 0c454a08dc
9 changed files with 89 additions and 48 deletions

View File

@ -55,10 +55,10 @@ public class SPdfApplication {
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(new ConfigInitializer());
if (Files.exists(Paths.get("configs/application.yml"))) { if (Files.exists(Paths.get("configs/settings.yml"))) {
app.setDefaultProperties(Collections.singletonMap("spring.config.location", "file:configs/application.yml")); app.setDefaultProperties(Collections.singletonMap("spring.config.location", "file:configs/settings.yml"));
} else { } else {
System.out.println("External configuration file 'configs/application.yml' does not exist. Using default configuration and environment configuration instead."); System.out.println("External configuration file 'configs/settings.yml' does not exist. Using default configuration and environment configuration instead.");
} }
app.run(args); app.run(args);

View File

@ -22,7 +22,7 @@ public class ConfigInitializer implements ApplicationContextInitializer<Configur
public void ensureConfigExists() throws IOException { public void ensureConfigExists() throws IOException {
// Define the path to the external config directory // Define the path to the external config directory
Path destPath = Paths.get("configs", "application.yml"); Path destPath = Paths.get("configs", "settings.yml");
// Check if the file already exists // Check if the file already exists
if (Files.notExists(destPath)) { if (Files.notExists(destPath)) {
@ -30,11 +30,11 @@ public class ConfigInitializer implements ApplicationContextInitializer<Configur
Files.createDirectories(destPath.getParent()); Files.createDirectories(destPath.getParent());
// Copy the resource from classpath to the external directory // Copy the resource from classpath to the external directory
try (InputStream in = getClass().getClassLoader().getResourceAsStream("application.yml.template")) { try (InputStream in = getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
if (in != null) { if (in != null) {
Files.copy(in, destPath); Files.copy(in, destPath);
} else { } else {
throw new FileNotFoundException("Resource file not found: application.yml.template"); throw new FileNotFoundException("Resource file not found: settings.yml.template");
} }
} }
} }

View File

@ -1,20 +1,28 @@
package stirling.software.SPDF.config; package stirling.software.SPDF.config;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import stirling.software.SPDF.model.ApplicationProperties;
@Service @Service
public class EndpointConfiguration { public class EndpointConfiguration {
private static final Logger logger = LoggerFactory.getLogger(EndpointConfiguration.class); private static final Logger logger = LoggerFactory.getLogger(EndpointConfiguration.class);
private Map<String, Boolean> endpointStatuses = new ConcurrentHashMap<>(); private Map<String, Boolean> endpointStatuses = new ConcurrentHashMap<>();
private Map<String, Set<String>> endpointGroups = new ConcurrentHashMap<>(); private Map<String, Set<String>> endpointGroups = new ConcurrentHashMap<>();
public EndpointConfiguration() { private final ApplicationProperties applicationProperties;
@Autowired
public EndpointConfiguration(ApplicationProperties applicationProperties) {
this.applicationProperties = applicationProperties;
init(); init();
processEnvironmentConfigs(); processEnvironmentConfigs();
} }
@ -198,21 +206,19 @@ public class EndpointConfiguration {
} }
private void processEnvironmentConfigs() { private void processEnvironmentConfigs() {
String endpointsToRemove = System.getenv("ENDPOINTS_TO_REMOVE"); List<String> endpointsToRemove = applicationProperties.getEndpoints().getToRemove();
String groupsToRemove = System.getenv("GROUPS_TO_REMOVE"); List<String> groupsToRemove = applicationProperties.getEndpoints().getGroupsToRemove();
if (endpointsToRemove != null) { if (endpointsToRemove != null) {
String[] endpoints = endpointsToRemove.split(","); for (String endpoint : endpointsToRemove) {
for (String endpoint : endpoints) {
disableEndpoint(endpoint.trim()); disableEndpoint(endpoint.trim());
} }
} }
if (groupsToRemove != null) { if (groupsToRemove != null) {
String[] groups = groupsToRemove.split(","); for (String group : groupsToRemove) {
for (String group : groups) {
disableGroup(group.trim()); disableGroup(group.trim());
} }
} }

View File

@ -24,11 +24,14 @@ public class InitialSetup {
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired
ApplicationProperties applicationProperties;
@PostConstruct @PostConstruct
public void init() { public void init() {
if (!userService.hasUsers()) { if (!userService.hasUsers()) {
String initialUsername = System.getenv("INITIAL_USERNAME"); String initialUsername = applicationProperties.getSecurity().getInitialLogin().getUsername();
String initialPassword = System.getenv("INITIAL_PASSWORD"); String initialPassword = applicationProperties.getSecurity().getInitialLogin().getPassword();
if (initialUsername != null && initialPassword != null) { if (initialUsername != null && initialPassword != null) {
userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId()); userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId());
} }
@ -36,8 +39,7 @@ public class InitialSetup {
} }
} }
@Autowired
ApplicationProperties applicationProperties;
@PostConstruct @PostConstruct
public void initSecretKey() throws IOException { public void initSecretKey() throws IOException {
@ -49,7 +51,7 @@ public class InitialSetup {
} }
private void saveKeyToConfig(String key) throws IOException { private void saveKeyToConfig(String key) throws IOException {
Path path = Paths.get("configs", "application.yml"); // Target the configs/application.yml Path path = Paths.get("configs", "settings.yml"); // Target the configs/settings.yml
List<String> lines = Files.readAllLines(path); List<String> lines = Files.readAllLines(path);
boolean keyFound = false; boolean keyFound = false;

View File

@ -47,6 +47,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import stirling.software.SPDF.model.ApplicationProperties;
import stirling.software.SPDF.model.PipelineConfig; import stirling.software.SPDF.model.PipelineConfig;
import stirling.software.SPDF.model.PipelineOperation; import stirling.software.SPDF.model.PipelineOperation;
import stirling.software.SPDF.utils.WebResponseUtils; import stirling.software.SPDF.utils.WebResponseUtils;
@ -91,6 +92,10 @@ public class PipelineController {
} }
} }
@Autowired
ApplicationProperties applicationProperties;
private void handleDirectory(Path dir) throws Exception { private void handleDirectory(Path dir) throws Exception {
logger.info("Handling directory: {}", dir); logger.info("Handling directory: {}", dir);
Path jsonFile = dir.resolve(jsonFileName); Path jsonFile = dir.resolve(jsonFileName);
@ -182,8 +187,7 @@ public class PipelineController {
// {filename} {folder} {date} {tmime} {pipeline} // {filename} {folder} {date} {tmime} {pipeline}
String outputDir = config.getOutputDir(); String outputDir = config.getOutputDir();
// Check if the environment variable 'automatedOutputFolder' is set String outputFolder = applicationProperties.getAutoPipeline().getOutputFolder();
String outputFolder = System.getenv("automatedOutputFolder");
if (outputFolder == null || outputFolder.isEmpty()) { if (outputFolder == null || outputFolder.isEmpty()) {
// If the environment variable is not set, use the default value // If the environment variable is not set, use the default value

View File

@ -1,5 +1,6 @@
package stirling.software.SPDF.controller.web; package stirling.software.SPDF.controller.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -7,6 +8,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.v3.oas.annotations.Hidden; import io.swagger.v3.oas.annotations.Hidden;
import stirling.software.SPDF.model.ApplicationProperties;
@Controller @Controller
public class HomeWebController { public class HomeWebController {
@ -31,18 +33,16 @@ public class HomeWebController {
return "redirect:/"; return "redirect:/";
} }
@Autowired
ApplicationProperties applicationProperties;
@GetMapping(value = "/robots.txt", produces = MediaType.TEXT_PLAIN_VALUE) @GetMapping(value = "/robots.txt", produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseBody @ResponseBody
@Hidden @Hidden
public String getRobotsTxt() { public String getRobotsTxt() {
String allowGoogleVisibility = System.getProperty("ALLOW_GOOGLE_VISIBILITY"); Boolean allowGoogle = applicationProperties.getSystem().getGooglevisibility();
if (allowGoogleVisibility == null) if(Boolean.TRUE.equals(allowGoogle)) {
allowGoogleVisibility = System.getenv("ALLOW_GOOGLE_VISIBILITY");
if (allowGoogleVisibility == null)
allowGoogleVisibility = "false";
if (Boolean.parseBoolean(allowGoogleVisibility)) {
return "User-agent: Googlebot\nAllow: /\n\nUser-agent: *\nAllow: /"; return "User-agent: Googlebot\nAllow: /\n\nUser-agent: *\nAllow: /";
} else { } else {
return "User-agent: Googlebot\nDisallow: /\n\nUser-agent: *\nDisallow: /"; return "User-agent: Googlebot\nDisallow: /\n\nUser-agent: *\nDisallow: /";

View File

@ -8,6 +8,7 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -24,26 +25,28 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import stirling.software.SPDF.config.StartupApplicationListener; import stirling.software.SPDF.config.StartupApplicationListener;
import stirling.software.SPDF.model.ApplicationProperties;
@RestController @RestController
@RequestMapping("/api/v1") @RequestMapping("/api/v1")
@Tag(name = "API", description = "Info APIs") @Tag(name = "API", description = "Info APIs")
public class MetricsController { public class MetricsController {
@Autowired
ApplicationProperties applicationProperties;
private final MeterRegistry meterRegistry; private final MeterRegistry meterRegistry;
private boolean isEndpointEnabled; private boolean metricsEnabled;
@PostConstruct @PostConstruct
public void init() { public void init() {
String isEndpointEnabled = System.getProperty("ENABLE_API_METRICS"); Boolean metricsEnabled = applicationProperties.getMetrics().getEnabled();
if (isEndpointEnabled == null) { if(metricsEnabled == null)
isEndpointEnabled = System.getenv("ENABLE_API_METRICS"); metricsEnabled = true;
if (isEndpointEnabled == null) { this.metricsEnabled = metricsEnabled;
isEndpointEnabled = "true";
}
}
this.isEndpointEnabled = "true".equalsIgnoreCase(isEndpointEnabled);
} }
public MetricsController(MeterRegistry meterRegistry) { public MetricsController(MeterRegistry meterRegistry) {
@ -54,7 +57,7 @@ public class MetricsController {
@Operation(summary = "Application status and version", @Operation(summary = "Application status and version",
description = "This endpoint returns the status of the application and its version number.") description = "This endpoint returns the status of the application and its version number.")
public ResponseEntity<?> getStatus() { public ResponseEntity<?> getStatus() {
if (!isEndpointEnabled) { if (!metricsEnabled) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled."); return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
} }
@ -68,7 +71,7 @@ public class MetricsController {
@Operation(summary = "GET request count", @Operation(summary = "GET request count",
description = "This endpoint returns the total count of GET requests or the count of GET requests for a specific endpoint.") description = "This endpoint returns the total count of GET requests or the count of GET requests for a specific endpoint.")
public ResponseEntity<?> getPageLoads(@RequestParam(required = false, name = "endpoint") @Parameter(description = "endpoint") Optional<String> endpoint) { public ResponseEntity<?> getPageLoads(@RequestParam(required = false, name = "endpoint") @Parameter(description = "endpoint") Optional<String> endpoint) {
if (!isEndpointEnabled) { if (!metricsEnabled) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled."); return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
} }
try { try {
@ -109,7 +112,7 @@ public class MetricsController {
@Operation(summary = "GET requests count for all endpoints", @Operation(summary = "GET requests count for all endpoints",
description = "This endpoint returns the count of GET requests for each endpoint.") description = "This endpoint returns the count of GET requests for each endpoint.")
public ResponseEntity<?> getAllEndpointLoads() { public ResponseEntity<?> getAllEndpointLoads() {
if (!isEndpointEnabled) { if (!metricsEnabled) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled."); return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
} }
try { try {
@ -170,7 +173,7 @@ public class MetricsController {
@Operation(summary = "POST request count", @Operation(summary = "POST request count",
description = "This endpoint returns the total count of POST requests or the count of POST requests for a specific endpoint.") description = "This endpoint returns the total count of POST requests or the count of POST requests for a specific endpoint.")
public ResponseEntity<?> getTotalRequests(@RequestParam(required = false, name = "endpoint") @Parameter(description = "endpoint") Optional<String> endpoint) { public ResponseEntity<?> getTotalRequests(@RequestParam(required = false, name = "endpoint") @Parameter(description = "endpoint") Optional<String> endpoint) {
if (!isEndpointEnabled) { if (!metricsEnabled) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled."); return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
} }
try { try {
@ -208,7 +211,7 @@ public class MetricsController {
@Operation(summary = "POST requests count for all endpoints", @Operation(summary = "POST requests count for all endpoints",
description = "This endpoint returns the count of POST requests for each endpoint.") description = "This endpoint returns the count of POST requests for each endpoint.")
public ResponseEntity<?> getAllPostRequests() { public ResponseEntity<?> getAllPostRequests() {
if (!isEndpointEnabled) { if (!metricsEnabled) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled."); return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
} }
try { try {
@ -244,7 +247,7 @@ public class MetricsController {
@GetMapping("/uptime") @GetMapping("/uptime")
public ResponseEntity<?> getUptime() { public ResponseEntity<?> getUptime() {
if (!isEndpointEnabled) { if (!metricsEnabled) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled."); return ResponseEntity.status(HttpStatus.FORBIDDEN).body("This endpoint is disabled.");
} }

View File

@ -11,7 +11,7 @@ import stirling.software.SPDF.config.YamlPropertySourceFactory;
@Configuration @Configuration
@ConfigurationProperties(prefix = "") @ConfigurationProperties(prefix = "")
@PropertySource(value = "file:./configs/application.yml", factory = YamlPropertySourceFactory.class) @PropertySource(value = "file:./configs/settings.yml", factory = YamlPropertySourceFactory.class)
public class ApplicationProperties { public class ApplicationProperties {
private Security security; private Security security;
private System system; private System system;
@ -19,6 +19,15 @@ public class ApplicationProperties {
private Endpoints endpoints; private Endpoints endpoints;
private Metrics metrics; private Metrics metrics;
private AutomaticallyGenerated automaticallyGenerated; private AutomaticallyGenerated automaticallyGenerated;
private AutoPipeline autoPipeline;
public AutoPipeline getAutoPipeline() {
return autoPipeline != null ? autoPipeline : new AutoPipeline();
}
public void setAutoPipeline(AutoPipeline autoPipeline) {
this.autoPipeline = autoPipeline;
}
public Security getSecurity() { public Security getSecurity() {
return security != null ? security : new Security(); return security != null ? security : new Security();
@ -67,16 +76,33 @@ public class ApplicationProperties {
public void setAutomaticallyGenerated(AutomaticallyGenerated automaticallyGenerated) { public void setAutomaticallyGenerated(AutomaticallyGenerated automaticallyGenerated) {
this.automaticallyGenerated = automaticallyGenerated; this.automaticallyGenerated = automaticallyGenerated;
} }
@Override @Override
public String toString() { public String toString() {
return "ApplicationProperties [security=" + security + ", system=" + system + ", ui=" + ui + ", endpoints=" return "ApplicationProperties [security=" + security + ", system=" + system + ", ui=" + ui + ", endpoints="
+ endpoints + ", metrics=" + metrics + ", automaticallyGenerated=" + endpoints + ", metrics=" + metrics + ", automaticallyGenerated=" + automaticallyGenerated
+ automaticallyGenerated + "]"; + ", autoPipeline=" + autoPipeline + "]";
} }
public static class AutoPipeline {
private String outputFolder;
public String getOutputFolder() {
return outputFolder;
}
public void setOutputFolder(String outputFolder) {
this.outputFolder = outputFolder;
}
@Override
public String toString() {
return "AutoPipeline [outputFolder=" + outputFolder + "]";
}
}
public static class Security { public static class Security {
private Boolean enableLogin; private Boolean enableLogin;
private InitialLogin initialLogin; private InitialLogin initialLogin;