From 1c39359279388a735206a6e1bb6e2ee0ed92b948 Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Thu, 15 Jul 2021 01:37:38 +0530 Subject: [PATCH] Allow configuring database in config.properties. --- src/main/java/me/kavin/piped/consts/Constants.java | 8 ++++++++ .../java/me/kavin/piped/utils/DatabaseSessionFactory.java | 7 ++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index 876be91..50695ab 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -12,6 +12,7 @@ import org.schabi.newpipe.extractor.StreamingService; import com.fasterxml.jackson.databind.ObjectMapper; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import me.kavin.piped.utils.PageMixin; public class Constants { @@ -39,6 +40,8 @@ public class Constants { public static final ObjectMapper mapper = new ObjectMapper().addMixIn(Page.class, PageMixin.class); + public static final Object2ObjectOpenHashMap hibernateProperties = new Object2ObjectOpenHashMap<>(); + static { Properties prop = new Properties(); try { @@ -51,6 +54,11 @@ public class Constants { CAPTCHA_BASE_URL = prop.getProperty("CAPTCHA_BASE_URL"); CAPTCHA_API_KEY = prop.getProperty("CAPTCHA_API_KEY"); PUBLIC_URL = prop.getProperty("API_URL"); + prop.forEach((_key, _value) -> { + String key = String.valueOf(_key), value = String.valueOf(_value); + if (key.startsWith("hibernate")) + hibernateProperties.put(key, value); + }); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/src/main/java/me/kavin/piped/utils/DatabaseSessionFactory.java b/src/main/java/me/kavin/piped/utils/DatabaseSessionFactory.java index 1976374..5a8fffd 100644 --- a/src/main/java/me/kavin/piped/utils/DatabaseSessionFactory.java +++ b/src/main/java/me/kavin/piped/utils/DatabaseSessionFactory.java @@ -4,6 +4,7 @@ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; +import me.kavin.piped.consts.Constants; import me.kavin.piped.utils.obj.db.Channel; import me.kavin.piped.utils.obj.db.User; import me.kavin.piped.utils.obj.db.Video; @@ -16,11 +17,7 @@ public class DatabaseSessionFactory { final Configuration configuration = new Configuration(); - configuration.setProperty("hibernate.connection.url", "jdbc:postgresql://"); - configuration.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver"); - configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.CockroachDB192Dialect"); - configuration.setProperty("hibernate.connection.username", "piped"); - configuration.setProperty("hibernate.connection.password", "@8LQuf7JUabCker$zQYS"); + Constants.hibernateProperties.forEach((key, value) -> configuration.setProperty(key, value)); configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false"); configuration.configure();