This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added DatabaseConfiguration.java to point to GCP db
- Loading branch information
GKAISER9
committed
Oct 26, 2023
1 parent
90f94f3
commit 2ff6013
Showing
4 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
/build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
/out/ | ||
|
||
/src/main/resources/static/ | ||
|
||
bin | ||
bin | ||
*.pem | ||
*.pkcs8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
api/src/main/java/com/ford/labs/retroquest/database_configuration/DatabaseConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.ford.labs.retroquest.database_configuration; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.core.io.Resource; | ||
|
||
import javax.sql.DataSource; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
@Configuration | ||
@Profile("gcp") | ||
public class DatabaseConfiguration { | ||
|
||
private final String dbHost; | ||
private final String dbPort; | ||
private final String dbName; | ||
private final String username; | ||
private final String password; | ||
|
||
|
||
public DatabaseConfiguration( | ||
@Value("${database.dbHost}") final String dbHost, | ||
@Value("${database.dbPort}") final String dbPort, | ||
@Value("${database.dbName}") final String dbName, | ||
@Value("${database.username}") final String username, | ||
@Value("${database.password}") final String password | ||
) { | ||
this.dbHost = dbHost; | ||
this.dbPort = dbPort; | ||
this.dbName = dbName; | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
@Bean | ||
public DataSource getDataSource() throws IOException { | ||
|
||
Resource certResource = new ClassPathResource("client_cert.pem"); | ||
File certFile = certResource.getFile(); | ||
Resource keyResource = new ClassPathResource("private_key.pkcs8"); | ||
File keyFile = keyResource.getFile(); | ||
|
||
String url = "jdbc:postgresql://" + dbHost + ":" + dbPort + "/" + dbName | ||
+ "?sslMode=require" | ||
+ "&sslcert=" + certFile.getPath() | ||
+ "&sslkey=" + keyFile.getPath(); | ||
|
||
return DataSourceBuilder.create() | ||
.url(url) | ||
.username(username) | ||
.password(password) | ||
.driverClassName("org.postgresql.Driver") | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
database.dbHost=${vcap.services.database.credentials.dbHost} | ||
database.dbPort=${vcap.services.database.credentials.dbPort} | ||
database.dbName=${vcap.services.database.credentials.dbName} | ||
database.username=${vcap.services.database.credentials.username} | ||
database.password=${vcap.services.database.credentials.password} |