Set db and cache path from first library root

This commit is contained in:
grimsi
2022-08-13 12:43:51 +02:00
parent 7a3a323212
commit ba4568cb35
6 changed files with 73 additions and 27 deletions
@@ -0,0 +1,66 @@
package de.grimsi.gameyfin.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.*;
import org.springframework.util.PropertyPlaceholderHelper;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Properties;
import java.util.stream.StreamSupport;
@Configuration
public class FilesystemConfig {
@Value("#{'${gameyfin.root}'.split(',')[0]}")
private String firstLibraryPath;
@Autowired
Environment env;
@Autowired
public void setConfigurableEnvironment(ConfigurableEnvironment env) {
Properties props = new Properties();
props.setProperty("gameyfin.db", "%s/.gameyfin/db".formatted(firstLibraryPath));
props.setProperty("gameyfin.cache", "%s/.gameyfin/cache".formatted(firstLibraryPath));
env.getPropertySources().addFirst(new PropertiesPropertySource("gameyfinFilesystemProperties", props));
}
/**
* This bean is needed so Spring initializes the data source after we are done messing with the configuration environment
* @return DataSource
*/
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
@Primary
public DataSource getDataSource() {
Properties properties = loadAllProperties();
return DataSourceBuilder
.create()
.url(properties.getProperty("spring.datasource.url"))
.build();
}
private Properties loadAllProperties() {
Properties props = new Properties();
MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources();
StreamSupport.stream(propSrcs.spliterator(), false)
.filter(ps -> ps instanceof EnumerablePropertySource)
.map(ps -> ((EnumerablePropertySource<?>) ps).getPropertyNames())
.flatMap(Arrays::stream)
.forEach(propName -> props.setProperty(propName, env.getProperty(propName)));
return props;
}
}
@@ -9,20 +9,13 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.util.Objects;
import java.util.Properties;
@Configuration
public class CustomConfiguratioLoader {
public class SecureProperties {
@Autowired
public void setConfigurableEnvironment(ConfigurableEnvironment env) {
try {
String firstLibraryPath = env.resolvePlaceholders("gameyfin.root").split(",")[0];
Properties props = new Properties();
props.setProperty("gameyfin.db", "%s/.gameyfin/db".formatted(firstLibraryPath));
props.setProperty("gameyfin.cache", "%s/.gameyfin/cache".formatted(firstLibraryPath));
env.getPropertySources().addFirst(new PropertiesPropertySource("dynamicallyLoadedGameyfinProperties", props));
Resource resource = new ClassPathResource("/config/secure.yml");
env.getPropertySources().addFirst(new PropertiesPropertySource(Objects.requireNonNull(resource.getFilename()), PropertiesLoaderUtils.loadProperties(resource)));
} catch (Exception ex) {
@@ -39,7 +39,7 @@ public class LibraryService {
public List<Path> getGameFiles() {
List<Path> gamefiles = new ArrayList<>();
libraryFolders.parallelStream().map(Path::of).forEach(
libraryFolders.stream().map(Path::of).forEach(
folder -> {
try (Stream<Path> stream = Files.list(folder)) {
// return all sub-folders (non-recursive) and files that have an extension that indicates that they are a downloadable file
@@ -1,9 +1,9 @@
spring:
jpa:
open-in-view: 'true'
open-in-view: true
properties:
hibernate:
enable_lazy_load_no_trans: 'true'
enable_lazy_load_no_trans: true
event:
merge:
entity_copy_observer: allow
@@ -12,7 +12,7 @@ spring:
ddl-auto: update
datasource:
username: gfadmin
url: jdbc:h2:file:${gameyfin.db}/${spring.datasource.db-name}
driverClassName: org.h2.Driver
password: gameyfin
db-name: gameyfin_db
password: gameyfin
url: jdbc:h2:file:${gameyfin.db}/${spring.datasource.db-name}
driverClassName: org.h2.Driver
@@ -1,14 +1,5 @@
gameyfin:
user:
password:
root:
db: ./.gameyfin/db
cache: ./.gameyfin/cache
file-extensions: iso, zip, rar, 7z, exe
igdb:
api:
client-id:
-4
View File
@@ -8,10 +8,6 @@ services:
- gameyfin.password=<your password here>
- gameyfin.igdb.api.client-id=<your twitch client-id here>
- gameyfin.igdb.api.client-secret=<your twitch client-secret here>
# The following two environment variables only need to be set if you have more than one library folder.
# If you have just one you can safely delete them.
- gameyfin.cache=<path to your game library>/.gameyfin/cache
- gameyfin.db=<path to your game library>/.gameyfin/db
volumes:
- <path to your game library>:/opt/gameyfin-library
ports: