diff --git a/src/main/kotlin/de/tschuehly/htmx/spring/supabase/auth/SupabaseAutoConfiguration.kt b/src/main/kotlin/de/tschuehly/htmx/spring/supabase/auth/SupabaseAutoConfiguration.kt index 98d21fb..7b7ec45 100644 --- a/src/main/kotlin/de/tschuehly/htmx/spring/supabase/auth/SupabaseAutoConfiguration.kt +++ b/src/main/kotlin/de/tschuehly/htmx/spring/supabase/auth/SupabaseAutoConfiguration.kt @@ -3,6 +3,7 @@ package de.tschuehly.htmx.spring.supabase.auth import com.auth0.jwt.JWT import com.auth0.jwt.JWTVerifier import com.auth0.jwt.algorithms.Algorithm +import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import de.tschuehly.htmx.spring.supabase.auth.config.DefaultExceptionHandlerConfig import de.tschuehly.htmx.spring.supabase.auth.config.SupabaseProperties @@ -20,14 +21,12 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration -import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties -import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.jdbc.DataSourceBuilder import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Import -import org.springframework.context.annotation.Primary +import java.util.* import javax.sql.DataSource @@ -80,4 +79,28 @@ class SupabaseAutoConfiguration( return JWT.require(Algorithm.HMAC256(supabaseProperties.jwtSecret)).build() } + + @Bean + @ConditionalOnProperty(prefix = "supabase.database", name = ["host"]) + fun dataSource( + supabaseProperties: SupabaseProperties, + ): DataSource { + val properties = Properties() + properties.setProperty("maximumPoolSize", "20") + properties.setProperty("minimumIdle", "15") + + properties.setProperty("driverClassName", "org.postgresql.Driver"); + supabaseProperties.database?.let { db -> + db.username?.let { + properties.setProperty("username", it); + } ?: let { + properties.setProperty("username", "postgres.${supabaseProperties.projectId}") + } + properties.setProperty("jdbcUrl","jdbc:postgresql://${db.host}:${db.port}/${db.name}") + properties.setProperty("password", db.password); + } + + val config = HikariConfig(properties) + return HikariDataSource(config) + } }