From ba4568cb35fd26f47aed9ad55418e0b30b25a96c Mon Sep 17 00:00:00 2001 From: grimsi <9295182+grimsi@users.noreply.github.com> Date: Sat, 13 Aug 2022 12:43:51 +0200 Subject: [PATCH] Set db and cache path from first library root --- .../gameyfin/config/FilesystemConfig.java | 66 +++++++++++++++++++ ...ratioLoader.java => SecureProperties.java} | 9 +-- .../gameyfin/service/LibraryService.java | 2 +- .../src/main/resources/config/database.yml | 10 +-- .../src/main/resources/config/gameyfin.yml | 9 --- docker/docker-compose.example.yml | 4 -- 6 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 backend/src/main/java/de/grimsi/gameyfin/config/FilesystemConfig.java rename backend/src/main/java/de/grimsi/gameyfin/config/{CustomConfiguratioLoader.java => SecureProperties.java} (64%) diff --git a/backend/src/main/java/de/grimsi/gameyfin/config/FilesystemConfig.java b/backend/src/main/java/de/grimsi/gameyfin/config/FilesystemConfig.java new file mode 100644 index 0000000..2c7aa59 --- /dev/null +++ b/backend/src/main/java/de/grimsi/gameyfin/config/FilesystemConfig.java @@ -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; + } + +} diff --git a/backend/src/main/java/de/grimsi/gameyfin/config/CustomConfiguratioLoader.java b/backend/src/main/java/de/grimsi/gameyfin/config/SecureProperties.java similarity index 64% rename from backend/src/main/java/de/grimsi/gameyfin/config/CustomConfiguratioLoader.java rename to backend/src/main/java/de/grimsi/gameyfin/config/SecureProperties.java index 467435f..3140b37 100644 --- a/backend/src/main/java/de/grimsi/gameyfin/config/CustomConfiguratioLoader.java +++ b/backend/src/main/java/de/grimsi/gameyfin/config/SecureProperties.java @@ -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) { diff --git a/backend/src/main/java/de/grimsi/gameyfin/service/LibraryService.java b/backend/src/main/java/de/grimsi/gameyfin/service/LibraryService.java index 0f1770c..c0e1efd 100644 --- a/backend/src/main/java/de/grimsi/gameyfin/service/LibraryService.java +++ b/backend/src/main/java/de/grimsi/gameyfin/service/LibraryService.java @@ -39,7 +39,7 @@ public class LibraryService { public List getGameFiles() { List gamefiles = new ArrayList<>(); - libraryFolders.parallelStream().map(Path::of).forEach( + libraryFolders.stream().map(Path::of).forEach( folder -> { try (Stream stream = Files.list(folder)) { // return all sub-folders (non-recursive) and files that have an extension that indicates that they are a downloadable file diff --git a/backend/src/main/resources/config/database.yml b/backend/src/main/resources/config/database.yml index 7c473a2..0e02ae7 100644 --- a/backend/src/main/resources/config/database.yml +++ b/backend/src/main/resources/config/database.yml @@ -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 \ No newline at end of file + url: jdbc:h2:file:${gameyfin.db}/${spring.datasource.db-name} + driverClassName: org.h2.Driver \ No newline at end of file diff --git a/backend/src/main/resources/config/gameyfin.yml b/backend/src/main/resources/config/gameyfin.yml index 68089f9..f70079e 100644 --- a/backend/src/main/resources/config/gameyfin.yml +++ b/backend/src/main/resources/config/gameyfin.yml @@ -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: diff --git a/docker/docker-compose.example.yml b/docker/docker-compose.example.yml index 5c2e439..265152c 100644 --- a/docker/docker-compose.example.yml +++ b/docker/docker-compose.example.yml @@ -8,10 +8,6 @@ services: - gameyfin.password= - gameyfin.igdb.api.client-id= - gameyfin.igdb.api.client-secret= - # 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=/.gameyfin/cache - - gameyfin.db=/.gameyfin/db volumes: - :/opt/gameyfin-library ports: