config template
This commit is contained in:
parent
41bd801e0d
commit
2053a6950d
4 changed files with 174 additions and 36 deletions
|
@ -4,6 +4,7 @@ import java.io.IOException;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
|
@ -51,7 +52,14 @@ public class SPdfApplication {
|
|||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SPdfApplication.class, args);
|
||||
SpringApplication app = new SpringApplication(SPdfApplication.class);
|
||||
if (Files.exists(Paths.get("configs/application.yml"))) {
|
||||
app.setDefaultProperties(Collections.singletonMap("spring.config.location", "file:configs/application.yml"));
|
||||
} else {
|
||||
System.out.println("External configuration file 'configs/application.yml' does not exist. Using default configuration and environment configuration instead.");
|
||||
}
|
||||
app.run(args);
|
||||
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
|
|
|
@ -1,37 +1,34 @@
|
|||
package stirling.software.SPDF.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
@Configuration
|
||||
public class AppConfig {
|
||||
|
||||
|
||||
|
||||
@Bean(name = "rateLimit")
|
||||
public boolean rateLimit() {
|
||||
String appName = System.getProperty("rateLimit");
|
||||
if (appName == null)
|
||||
appName = System.getenv("rateLimit");
|
||||
System.out.println("rateLimit=" + appName);
|
||||
return (appName != null) ? Boolean.valueOf(appName) : false;
|
||||
}
|
||||
|
||||
@Bean(name = "loginEnabled")
|
||||
@Value("${login.enabled:false}")
|
||||
private boolean defaultLoginEnabled;
|
||||
|
||||
@Value("${ui.homeName:Stirling PDF}")
|
||||
private String defaultAppName;
|
||||
|
||||
@Value("${ui.homeDescription:null}")
|
||||
private String defaultHomeText;
|
||||
|
||||
@Value("${ui.navbarName:Stirling PDF}")
|
||||
private String defaultNavBarText;
|
||||
|
||||
@Bean(name = "loginEnabled")
|
||||
public boolean loginEnabled() {
|
||||
String appName = System.getProperty("login.enabled");
|
||||
if (appName == null)
|
||||
appName = System.getenv("login.enabled");
|
||||
System.out.println("loginEnabled=" + appName);
|
||||
return (appName != null) ? Boolean.valueOf(appName) : false;
|
||||
return getBooleanValue("login.enabled", defaultLoginEnabled);
|
||||
}
|
||||
|
||||
|
||||
@Bean(name = "appName")
|
||||
public String appName() {
|
||||
String appName = System.getProperty("APP_HOME_NAME");
|
||||
if (appName == null)
|
||||
appName = System.getenv("APP_HOME_NAME");
|
||||
return (appName != null) ? appName : "Stirling PDF";
|
||||
return getStringValue("APP_HOME_NAME", defaultAppName);
|
||||
}
|
||||
|
||||
@Bean(name = "appVersion")
|
||||
|
@ -42,22 +39,40 @@ public class AppConfig {
|
|||
|
||||
@Bean(name = "homeText")
|
||||
public String homeText() {
|
||||
String homeText = System.getProperty("APP_HOME_DESCRIPTION");
|
||||
if (homeText == null)
|
||||
homeText = System.getenv("APP_HOME_DESCRIPTION");
|
||||
return (homeText != null) ? homeText : "null";
|
||||
return getStringValue("APP_HOME_DESCRIPTION", defaultHomeText);
|
||||
}
|
||||
|
||||
@Bean(name = "navBarText")
|
||||
public String navBarText() {
|
||||
String navBarText = System.getProperty("APP_NAVBAR_NAME");
|
||||
if (navBarText == null)
|
||||
navBarText = System.getenv("APP_NAVBAR_NAME");
|
||||
if (navBarText == null)
|
||||
navBarText = System.getProperty("APP_HOME_NAME");
|
||||
if (navBarText == null)
|
||||
navBarText = System.getenv("APP_HOME_NAME");
|
||||
|
||||
return (navBarText != null) ? navBarText : "Stirling PDF";
|
||||
String navBarText = getStringValue("APP_NAVBAR_NAME", null);
|
||||
if (navBarText == null) {
|
||||
navBarText = getStringValue("APP_HOME_NAME", defaultNavBarText);
|
||||
}
|
||||
return navBarText;
|
||||
}
|
||||
|
||||
private boolean getBooleanValue(String key, boolean defaultValue) {
|
||||
String value = System.getProperty(key);
|
||||
if (value == null)
|
||||
value = System.getenv(key);
|
||||
return (value != null) ? Boolean.valueOf(value) : defaultValue;
|
||||
}
|
||||
|
||||
private String getStringValue(String key, String defaultValue) {
|
||||
String value = System.getProperty(key);
|
||||
if (value == null)
|
||||
value = System.getenv(key);
|
||||
return (value != null) ? value : defaultValue;
|
||||
}
|
||||
|
||||
@Bean(name = "rateLimit")
|
||||
public boolean rateLimit() {
|
||||
String appName = System.getProperty("rateLimit");
|
||||
if (appName == null)
|
||||
appName = System.getenv("rateLimit");
|
||||
System.out.println("rateLimit=" + appName);
|
||||
return (appName != null) ? Boolean.valueOf(appName) : false;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,11 +1,22 @@
|
|||
package stirling.software.SPDF.config.security;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import stirling.software.SPDF.model.Role;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
@Component
|
||||
public class InitialSetup {
|
||||
|
||||
|
@ -23,4 +34,81 @@ public class InitialSetup {
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Value("${AutomaticallyGeneratedDoNotEdit.key:}")
|
||||
private String secretKey;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
|
||||
public void ensureConfigExists() throws IOException {
|
||||
// Define the path to the external config directory
|
||||
Path destPath = Paths.get("configs", "application.yml");
|
||||
|
||||
// Check if the file already exists
|
||||
if (Files.notExists(destPath)) {
|
||||
// Ensure the destination directory exists
|
||||
Files.createDirectories(destPath.getParent());
|
||||
|
||||
// Copy the resource from classpath to the external directory
|
||||
try (InputStream in = getClass().getClassLoader().getResourceAsStream("application.yml.template")) {
|
||||
if (in != null) {
|
||||
Files.copy(in, destPath);
|
||||
} else {
|
||||
throw new FileNotFoundException("Resource file not found: application.yml.template");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void initSecretKey() throws IOException {
|
||||
ensureConfigExists();
|
||||
if (secretKey == null || secretKey.isEmpty() || "placeholder".equals(secretKey)) {
|
||||
secretKey = UUID.randomUUID().toString(); // Generating a random UUID as the secret key
|
||||
saveKeyToConfig(secretKey);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveKeyToConfig(String key) throws IOException {
|
||||
Path path = Paths.get("configs", "application.yml"); // Target the configs/application.yml
|
||||
List<String> lines = Files.readAllLines(path);
|
||||
boolean keyFound = false;
|
||||
|
||||
// Search for the existing key to replace it or place to add it
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
if (lines.get(i).startsWith("AutomaticallyGeneratedDoNotEdit:")) {
|
||||
keyFound = true;
|
||||
if (i + 1 < lines.size() && lines.get(i + 1).trim().startsWith("key:")) {
|
||||
lines.set(i + 1, " key: " + key);
|
||||
break;
|
||||
} else {
|
||||
lines.add(i + 1, " key: " + key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the section doesn't exist, append it
|
||||
if (!keyFound) {
|
||||
lines.add("AutomaticallyGeneratedDoNotEdit:");
|
||||
lines.add(" key: " + key);
|
||||
}
|
||||
|
||||
// Add a comment (if not already added)
|
||||
if (!lines.get(0).startsWith("# Automatically Generated Settings (Do Not Edit Directly)")) {
|
||||
lines.add(0, "# Automatically Generated Settings (Do Not Edit Directly)");
|
||||
}
|
||||
|
||||
// Write back to the file
|
||||
Files.write(path, lines);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
27
src/main/resources/application.yml.template
Normal file
27
src/main/resources/application.yml.template
Normal file
|
@ -0,0 +1,27 @@
|
|||
security:
|
||||
enableLogin: false # set to 'true' to enable login
|
||||
initialLogin:
|
||||
username: 'username' # Specify the initial username for first boot (e.g. 'admin')
|
||||
password: 'password'# Specify the initial password for first boot (e.g. 'password123')
|
||||
csrfDisabled: true
|
||||
|
||||
system:
|
||||
defaultLocale: 'en-US' # Set the default language (e.g. 'de-DE', 'fr-FR', etc)
|
||||
googlevisibility: false # 'true' to allow Google visibility, 'false' to disallow
|
||||
rootPath: / # Set the application's root URI (e.g. /pdf-app)
|
||||
customstaticFilePath: '/customFiles/static/' # Directory path for custom static files
|
||||
maxFileSize: 2000 # Set the maximum file size in MB
|
||||
|
||||
ui:
|
||||
#homeName: 'Stirling PDF Application' # Application's visible name
|
||||
#homeDescription: "The best PDF tool. Short description or tagline."
|
||||
#navbarName: 'Stirling Navbar' # Name displayed on the navigation bar
|
||||
|
||||
endpoints:
|
||||
toRemove: [] # List endpoints to disable (e.g. ['img-to-pdf', 'remove-pages'])
|
||||
groupsToRemove: [] # List groups to disable (e.g. ['LibreOffice'])
|
||||
|
||||
metrics:
|
||||
enabled: true # 'true' to enable metric API endpoints, 'false' to disable
|
||||
|
||||
|
Loading…
Reference in a new issue