-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
23 changed files
with
268 additions
and
46 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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
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,42 @@ | ||
.gradle | ||
build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea/modules.xml | ||
.idea/jarRepositories.xml | ||
.idea/compiler.xml | ||
.idea/libraries/ | ||
*.iws | ||
*.iml | ||
*.ipr | ||
out/ | ||
!**/src/main/**/out/ | ||
!**/src/test/**/out/ | ||
|
||
### Eclipse ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
bin/ | ||
!**/src/main/**/bin/ | ||
!**/src/test/**/bin/ | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store |
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,46 @@ | ||
plugins { | ||
id("java") | ||
id("org.springframework.boot") version "3.0.6" | ||
id("io.spring.dependency-management") version "1.1.0" | ||
} | ||
|
||
group = "ru.relay.infrastructure" | ||
version = "0.0.1" | ||
|
||
extra["testcontainersVersion"] = "1.18.0" | ||
|
||
dependencyManagement { | ||
imports { | ||
mavenBom("org.testcontainers:testcontainers-bom:${property("testcontainersVersion")}") | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
|
||
|
||
dependencies { | ||
implementation("org.springframework.boot:spring-boot-starter") | ||
testImplementation(platform("org.junit:junit-bom:5.9.1")) | ||
testImplementation("org.junit.jupiter:junit-jupiter") | ||
implementation("org.testcontainers:postgresql:1.18.0") | ||
implementation("org.springframework.boot:spring-boot-devtools") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} | ||
|
||
publishing { | ||
publications { | ||
create<MavenPublication>("maven") { | ||
groupId = group.toString() | ||
artifactId = "local-postgres" | ||
version = version.toString() | ||
|
||
from(components["java"]) | ||
} | ||
} | ||
} |
Binary file not shown.
5 changes: 5 additions & 0 deletions
5
infrastructure/local-postgres/gradle/wrapper/gradle-wrapper.properties
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 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
111 changes: 111 additions & 0 deletions
111
...al-postgres/src/main/java/ru/relay/infrastructure/localpostgres/LocalDeveloperConfig.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,111 @@ | ||
package ru.relay.infrastructure.localpostgres; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.SpringApplicationRunListener; | ||
import org.springframework.boot.devtools.restart.Restarter; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.EnumerablePropertySource; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
public class LocalDeveloperConfig implements SpringApplicationRunListener { | ||
public LocalDeveloperConfig(SpringApplication app, String[] args) { | ||
app.addInitializers(configurableApplicationContext -> { | ||
try { | ||
// Restarter.getInstance(); | ||
} catch (Throwable ignored) { | ||
// Restarter is not initialized, exiting | ||
return; | ||
} | ||
|
||
ConfigurableEnvironment environment = configurableApplicationContext.getEnvironment(); | ||
environment.getPropertySources().addLast(new PostgreSQLPropertySource(environment)); | ||
}); | ||
} | ||
|
||
private static class PostgreSQLPropertySource extends ContainerPropertySource<PostgreSQLContainer<?>> { | ||
|
||
private final ConfigurableEnvironment environment; | ||
|
||
public PostgreSQLPropertySource(ConfigurableEnvironment environment) { | ||
super("org.testcontainers.postgresql"); | ||
this.environment = environment; | ||
|
||
addPropertyMapper("spring.r2dbc.url", it -> { | ||
return String.format( | ||
"r2dbc:postgresql://%s:%d/test", | ||
it.getHost(), | ||
it.getMappedPort(5432) | ||
); | ||
}); | ||
addPropertyMapper("spring.r2dbc.username", PostgreSQLContainer::getUsername); | ||
addPropertyMapper("spring.r2dbc.password", PostgreSQLContainer::getPassword); | ||
addPropertyMapper("spring.datasource.url", PostgreSQLContainer::getJdbcUrl); | ||
addPropertyMapper("spring.datasource.username", PostgreSQLContainer::getUsername); | ||
addPropertyMapper("spring.datasource.password", PostgreSQLContainer::getPassword); | ||
} | ||
|
||
@Override | ||
protected PostgreSQLContainer<?> createContainer() { | ||
String image = environment.getProperty("testcontainers.postgres", "postgres:13.3"); | ||
|
||
try (PostgreSQLContainer container = new PostgreSQLContainer<>(image) | ||
.withReuse(true)) { | ||
return container; | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
/** | ||
* Base class for Testcontainers-based property sources | ||
*/ | ||
private abstract static class ContainerPropertySource<T extends GenericContainer<?>> extends EnumerablePropertySource<T> { | ||
|
||
private final Map<String, Function<T, Object>> propertyMappers = new HashMap<>(); | ||
|
||
public ContainerPropertySource(String name) { | ||
super(name); | ||
} | ||
|
||
protected void addPropertyMapper(String name, Function<T, Object> mapper) { | ||
propertyMappers.put(name, mapper); | ||
} | ||
|
||
protected abstract T createContainer(); | ||
|
||
@Override | ||
public String[] getPropertyNames() { | ||
return propertyMappers.keySet().toArray(new String[0]); | ||
} | ||
|
||
@Override | ||
public boolean containsProperty(String name) { | ||
return propertyMappers.containsKey(name); | ||
} | ||
|
||
@Override | ||
public Object getProperty(String name) { | ||
Function<T, Object> mapper = propertyMappers.get(name); | ||
if (mapper == null) { | ||
return null; | ||
} | ||
|
||
return mapper.apply( | ||
(T) Restarter.getInstance().getOrAddAttribute( | ||
"ContainerPropertySource." + getName(), | ||
() -> { | ||
T result = createContainer(); | ||
result.start(); | ||
return result; | ||
} | ||
) | ||
); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
infrastructure/local-postgres/src/main/resources/spring.factories
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 @@ | ||
org.springframework.boot.SpringApplicationRunListener=ru.relay.infrastructure.localpostgres.LocalDeveloperConfig |
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,3 +2,4 @@ rootProject.name = "infrastructure" | |
|
||
include(":id") | ||
include(":db") | ||
include(":local-postgres") |
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
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
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
Oops, something went wrong.