update .env logic to use dotenv for java instead of using intellij's internal environent manager

This commit is contained in:
Mika Bomm 2024-09-05 23:20:10 +02:00
parent e5188c8c16
commit 654e7b1801
6 changed files with 80 additions and 8 deletions

View file

@ -1,3 +1,7 @@
# Database section
DB_USER=
DB_PASSWORD=
DB_PASSWORD=
# Minio S3 section
MINIO_ACCESS_KEY=
MINIO_SECRET_KEY=

View file

@ -2,10 +2,6 @@
<configuration default="false" name="start server" type="Application" factoryName="Application">
<option name="ALTERNATIVE_JRE_PATH" value="17" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<envs>
<env name="DB_USER" value="postgres" />
<env name="DB_PASSWORD" value="postgres" />
</envs>
<option name="MAIN_CLASS_NAME" value="com.mixel.docusphere.DocuSphereApplication" />
<module name="DocuSphere.server.main" />
<method v="2">

View file

@ -24,6 +24,12 @@ dependencies {
implementation 'org.springframework.security:spring-security-crypto:6.3.3'
implementation 'org.bouncycastle:bcprov-jdk15on:1.70'
// Dotenv manager
implementation 'io.github.cdimascio:java-dotenv:5.2.2'
// Minio S3 Storage
implementation 'io.minio:minio:8.5.12'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

View file

@ -0,0 +1,28 @@
package com.mixel.docusphere.config;
import io.github.cdimascio.dotenv.Dotenv;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;
@Configuration
public class DataSourceConfig {
@Bean
@Primary
public DataSource dataSource() {
// Load environment variables from .env file
Dotenv dotenv = Dotenv.configure().load();
// Build the DataSource using Dotenv values
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create()
.driverClassName("org.postgresql.Driver")
.url(dotenv.get("DB_URL"))
.username(dotenv.get("DB_USER"))
.password(dotenv.get("DB_PASSWORD"));
return dataSourceBuilder.build();
}
}

View file

@ -0,0 +1,33 @@
package com.mixel.docusphere.config;
import io.github.cdimascio.dotenv.Dotenv;
import jakarta.annotation.PostConstruct;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DotenvConfig {
private final Dotenv dotenv;
public DotenvConfig() {
this.dotenv = Dotenv.configure()
.directory("../") // Adjust path as needed
.filename(".env")
.ignoreIfMissing()
.load();
}
@PostConstruct
public void init() {
dotenv.entries().forEach(entry ->
System.setProperty(entry.getKey(), entry.getValue())
);
System.out.println("Dotenv variables loaded");
}
@Bean
public Dotenv dotenv() {
return dotenv;
}
}

View file

@ -4,13 +4,11 @@ spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/docusphere
username: ${DB_USER}
password: ${DB_PASSWORD}
jpa:
hibernate:
ddl-auto: create-drop # update # Use update later on but use create-drop for testing first
dialect: org.hibernate.dialect.PostgreSQLDialect
show-sql: true
properties:
hibernate:
@ -20,3 +18,10 @@ logging:
level:
org.hibernate.SQL: debug
org.hibernate.type.descriptor.sql.BasicBinder: trace
org.springframework.core.env: debug
minio:
endpoint: "http://localhost:9000" # Your MinIO endpoint URL
access-key: ${MINIO_ACCESS_KEY} # Environment variable for the access key
secret-key: ${MINIO_SECRET_KEY} # Environment variable for the secret key
bucket-name: docusphere # The bucket name you want to use