-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
maseungmin
authored and
maseungmin
committed
Nov 28, 2024
1 parent
5a865da
commit 1ae1ab0
Showing
4 changed files
with
64 additions
and
2 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
Submodule config
updated
from e9f531 to 81b28e
59 changes: 59 additions & 0 deletions
59
src/main/java/icurriculum/global/config/SSHDataSourceConfig.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,59 @@ | ||
package icurriculum.global.config; | ||
|
||
|
||
import javax.sql.DataSource; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; | ||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
|
||
@Slf4j | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class SSHDataSourceConfig { | ||
|
||
private final SSHConfig initializer; | ||
|
||
@Value("${server}") | ||
private String isServer; | ||
|
||
@Value("${cloud.aws.ec2.database_endpoint}") | ||
private String databaseEndpoint; | ||
@Value("${cloud.aws.ec2.database_port}") | ||
private int databasePort; | ||
|
||
|
||
@Bean("dataSource") | ||
@Primary | ||
public DataSource dataSource(DataSourceProperties properties) { | ||
String url = properties.getUrl().replace("localhost", databaseEndpoint); | ||
url = url.replace("[forwardedPort]", String.valueOf(3306)); | ||
Integer forwardedPort = null; | ||
|
||
// SSH 터널을 통해 RDS에 연결해야 할 경우 | ||
if (isServer.equals("false")) { | ||
url = properties.getUrl(); // jdbc:mysql://localhost:[forwardedPort]/dev | ||
forwardedPort = initializer.buildSshConnection(databaseEndpoint, databasePort); | ||
url = url.replace("[forwardedPort]", String.valueOf(forwardedPort)); | ||
} | ||
|
||
log.info("Datasource Properties URL: {}", url); | ||
log.info("Datasource Properties Username: {}", properties.getUsername()); | ||
log.info("Datasource Properties Driver ClassName: {}", properties.getDriverClassName()); | ||
|
||
log.info(url); | ||
|
||
DataSource dataSourceInstance = DataSourceBuilder.create() | ||
.url(url) | ||
.username(properties.getUsername()) | ||
.password(properties.getPassword()) | ||
.driverClassName(properties.getDriverClassName()) | ||
.build(); | ||
return dataSourceInstance; | ||
} | ||
} |
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