Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
added DatabaseConfiguration.java to point to GCP db
Browse files Browse the repository at this point in the history
  • Loading branch information
GKAISER9 committed Oct 26, 2023
1 parent 90f94f3 commit 2ff6013
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
6 changes: 3 additions & 3 deletions api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/build/
!gradle/wrapper/gradle-wrapper.jar
/out/

/src/main/resources/static/

bin
bin
*.pem
*.pkcs8
2 changes: 2 additions & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ dependencies {
testImplementation 'org.springframework.security:spring-security-test'

developmentOnly 'org.springframework.boot:spring-boot-devtools'

runtimeOnly 'org.postgresql:postgresql:42.4.0'
}

static def noProfileOrDbDefined(tasks) {
Expand Down
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();
}
}
5 changes: 5 additions & 0 deletions api/src/main/resources/application-gcp.properties
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}

0 comments on commit 2ff6013

Please sign in to comment.