Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IL-84]Environments #92

Merged
merged 30 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
APP_ENV=prod
APP_ENV=dev
APP_VERSION=0.0.1-SNAPSHOT
HUB=zufarexplainedit
voltwatt marked this conversation as resolved.
Show resolved Hide resolved
TAG=1.0.0-fix-docker-environment
TAG=1.0.0-environments
DATASOURCE_URL=jdbc:postgresql://postgresdb:5432/testdb
23 changes: 15 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# Build stage with Maven
FROM maven:3.8.3-openjdk-17 as maven_build

voltwatt marked this conversation as resolved.
Show resolved Hide resolved
ENV HOME=/opt/app
RUN mkdir -p $HOME
WORKDIR $HOME
ADD pom.xml $HOME
RUN mvn verify --fail-never
ADD ./ $HOME
RUN mvn package

voltwatt marked this conversation as resolved.
Show resolved Hide resolved
COPY . ./

voltwatt marked this conversation as resolved.
Show resolved Hide resolved
RUN export APP_ENV=$(grep APP_ENV .env | cut -d '=' -f2) && \
export APP_VERSION=$(grep APP_VERSION .env | cut -d '=' -f2) && \
mvn dependency:go-offline -P${APP_ENV} && \
mvn versions:set-property -Dproperty=project.version -DnewVersion=${APP_VERSION} && \
mvn package -P${APP_ENV}

# Package stage with JRE only
FROM eclipse-temurin:17-jre-jammy
WORKDIR $HOME
COPY --from=maven_build /opt/app/target/online-store-0.0.1-SNAPSHOT.jar /opt/app/online-store-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "/opt/app/online-store-0.0.1-SNAPSHOT.jar" ]

voltwatt marked this conversation as resolved.
Show resolved Hide resolved
WORKDIR /opt/app
COPY --from=maven_build /opt/app/target/*.jar /opt/app/app.jar
ENTRYPOINT ["java", "-jar", "/opt/app/app.jar"]
211 changes: 101 additions & 110 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
<relativePath/>
</parent>

Expand All @@ -25,28 +25,27 @@

<groupId>com.zufar-project</groupId>
<artifactId>online-store</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Online Store</name>
<description>Online-store project for learning purpose</description>

<properties>
<project.version>${project.version}</project.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<slf4j-api.version>2.0.5</slf4j-api.version>
<jackson-databind.version>2.14.0</jackson-databind.version>
<jackson-databind.version>2.15.2</jackson-databind.version>
<jackson-core.version>2.15.2</jackson-core.version>
<modelmapper.version>3.1.1</modelmapper.version>
<junit-jupiter-engine.version>5.4.2</junit-jupiter-engine.version>
<junit-jupiter-api.version>5.4.2</junit-jupiter-api.version>
<junit-platform-commons.version>1.4.0</junit-platform-commons.version>
<junit-platform-launcher.version>1.4.0</junit-platform-launcher.version>
<junit-jupiter.version>5.9.2</junit-jupiter.version>
<jackson-core.version>2.14.0</jackson-core.version>
<springfox.swagger.version>3.0.0</springfox.swagger.version>
<swagger-core-version>2.2.8</swagger-core-version>
<version.keycloak>21.0.1</version.keycloak>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<jacoco-maven-plugin.version>0.8.5</jacoco-maven-plugin.version>
Expand All @@ -56,98 +55,39 @@
<logstash-logback-encoder.version>7.3</logstash-logback-encoder.version>
<spring-boot-maven-plugin.version>3.1.2</spring-boot-maven-plugin.version>
<postgresql.version>42.6.0</postgresql.version>
<liquibase.verson>4.23.0</liquibase.verson>
<liquibase.version>4.23.1</liquibase.version>
<stripe.version>22.15.0</stripe.version>
<apache-commons-lang3.version>3.12.0</apache-commons-lang3.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<lombok.version>1.18.24</lombok.version>
<mapstruct.version>1.5.3.Final</mapstruct.version>
<sonar-maven-plugin.version>3.7.0.1746</sonar-maven-plugin.version>
<instancio-junit.version>3.0.1</instancio-junit.version>
<spring-retry.version>2.0.2</spring-retry.version>
</properties>
<dependencies>

<!-- Swagger dependencies -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-core-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.swagger.version}</version>
</dependency>

<!-- Keycloak dependencies -->
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-authz-client</artifactId>
<version>${version.keycloak}</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-security-adapter</artifactId>
<version>${version.keycloak}</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-test-helper</artifactId>
<version>${version.keycloak}</version>
<scope>test</scope>
</dependency>

<!-- Spring dependencies -->
<!-- common dependencies -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>${spring-retry.version}</version>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>

<!-- JWT dependencies -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
Expand All @@ -163,39 +103,11 @@
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt-jackson.version}</version>
</dependency>

<!-- Utility dependencies -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>${logstash-logback-encoder.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.instancio</groupId>
<artifactId>instancio-junit</artifactId>
<version>${instancio-junit.version}</version>
<scope>test</scope>
</dependency>

<!-- junit dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>

<!-- Jackson dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand All @@ -206,8 +118,6 @@
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
</dependency>

<!-- Database dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -220,24 +130,105 @@
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.verson}</version>
<version>${liquibase.version}</version>
</dependency>

<!-- Stripe Api -->
<dependency>
<groupId>com.stripe</groupId>
<artifactId>stripe-java</artifactId>
<version>${stripe.version}</version>
</dependency>

<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache-commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>${spring-retry.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
voltwatt marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
<dependency>
<groupId>junit</groupId>
voltwatt marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
voltwatt marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<!-- prod dependencies -->
<profile>
<id>prod</id>
<dependencies>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>
</profile>

<!-- dev dependencies -->
<profile>
<id>dev</id>
<activation>
<property>
<name>ENV</name>
<value>dev</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-core-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.swagger.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
</profiles>

<!-- split into environments once extended enough -->
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -284,4 +275,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
23 changes: 23 additions & 0 deletions src/test/java/AppHealthCheckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AppHealthCheckTest {

@Test
public void isAppAlive() throws Exception {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this unit test can help us?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test quickly validates that the application container is running and the web server is reachable, so we know we can start coding against it. While the test is simple, it can catch glaring issues like if the server isn't running or is misconfigured, which saves debugging time later. Although this is a basic test, it serves as a foundation for more complex health checks in the future.

Sunagatov marked this conversation as resolved.
Show resolved Hide resolved
HttpClient client = HttpClient.newHttpClient();
Sunagatov marked this conversation as resolved.
Show resolved Hide resolved
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://localhost:8083/"))
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

assertEquals(403, response.statusCode());
Sunagatov marked this conversation as resolved.
Show resolved Hide resolved
}
}