|
@@ -0,0 +1,53 @@
|
|
1
|
+package com.ziplinegreen.vault.Config;
|
|
2
|
+
|
|
3
|
+import org.apache.tomcat.jdbc.pool.DataSource;
|
|
4
|
+import org.slf4j.Logger;
|
|
5
|
+import org.slf4j.LoggerFactory;
|
|
6
|
+import org.springframework.context.annotation.Bean;
|
|
7
|
+import org.springframework.context.annotation.Configuration;
|
|
8
|
+import org.springframework.context.annotation.Profile;
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+import java.net.URI;
|
|
12
|
+import java.net.URISyntaxException;
|
|
13
|
+
|
|
14
|
+@Configuration
|
|
15
|
+ public class DataSourceConfig {
|
|
16
|
+
|
|
17
|
+ Logger log = LoggerFactory.getLogger(getClass());
|
|
18
|
+
|
|
19
|
+ @Bean
|
|
20
|
+ @Profile("postgres")
|
|
21
|
+ public DataSource postgresDataSource() {
|
|
22
|
+ String databaseUrl = System.getenv("DATABASE_URL");
|
|
23
|
+ log.info("Initializing PostgreSQL database: {}", databaseUrl);
|
|
24
|
+
|
|
25
|
+ URI dbUri;
|
|
26
|
+ try {
|
|
27
|
+ dbUri = new URI(databaseUrl);
|
|
28
|
+ }
|
|
29
|
+ catch (URISyntaxException e) {
|
|
30
|
+ log.error(String.format("Invalid DATABASE_URL: %s", databaseUrl), e);
|
|
31
|
+ return null;
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ String username = dbUri.getUserInfo().split(":")[0];
|
|
35
|
+ String password = dbUri.getUserInfo().split(":")[1];
|
|
36
|
+ String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':'
|
|
37
|
+ + dbUri.getPort() + dbUri.getPath();
|
|
38
|
+
|
|
39
|
+ org.apache.tomcat.jdbc.pool.DataSource dataSource
|
|
40
|
+ = new org.apache.tomcat.jdbc.pool.DataSource();
|
|
41
|
+ dataSource.setDriverClassName("org.postgresql.Driver");
|
|
42
|
+ dataSource.setUrl(dbUrl);
|
|
43
|
+ dataSource.setUsername(username);
|
|
44
|
+ dataSource.setPassword(password);
|
|
45
|
+ dataSource.setTestOnBorrow(true);
|
|
46
|
+ dataSource.setTestWhileIdle(true);
|
|
47
|
+ dataSource.setTestOnReturn(true);
|
|
48
|
+ dataSource.setValidationQuery("SELECT 1");
|
|
49
|
+ return dataSource;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ }
|
|
53
|
+
|