diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..74fc102 --- /dev/null +++ b/.env.sample @@ -0,0 +1,11 @@ +MYSQL_USER +MYSQL_PASSWORD +MYSQL_ROOT_PASSWORD +MYSQL_DB +MYSQL_LOCAL_PORT +MYSQL_DOCKER_PORT +SPRING_LOCAL_PORT +SPRING_DOCKER_PORT +DEBUG_PORT +TOKEN_SECRET +TOKEN_EXPIRATION \ No newline at end of file diff --git a/.gitignore b/.gitignore index 549e00a..914ee0a 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ build/ ### VS Code ### .vscode/ + +!.env.sample +.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c260eed --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM eclipse-temurin:21-jdk AS builder +WORKDIR /app +ARG JAR_FILE=target/*.jar +COPY ${JAR_FILE} application.jar +RUN java -Djarmode=layertools -jar application.jar extract + +FROM eclipse-temurin:17-jre-focal +WORKDIR /app +COPY --from=builder /app/dependencies/ ./ +COPY --from=builder /app/spring-boot-loader/ ./ +COPY --from=builder /app/snapshot-dependencies/ ./ +COPY --from=builder /app/application/ ./ + +RUN groupadd -r bookstore && useradd -r -g bookstore bookstore +USER bookstore + +EXPOSE 8080 + +ENTRYPOINT ["java", "-Dspring.profiles.active=prod", "org.springframework.boot.loader.launch.JarLauncher"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f637b89 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,45 @@ +services: + mysqldb: + image: mysql:latest + container_name: bookstore-mysql + restart: unless-stopped + env_file: ./.env + environment: + - MYSQL_USER=$MYSQL_USER + - MYSQL_PASSWORD=$MYSQL_PASSWORD + - MYSQL_DATABASE=$MYSQL_DB + - MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD + ports: + - $MYSQL_LOCAL_PORT:$MYSQL_DOCKER_PORT + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p$$MYSQL_ROOT_PASSWORD"] + interval: 10s + timeout: 5s + retries: 5 + start_period: 30s + + app: + container_name: bookstore-app + depends_on: + mysqldb: + condition: service_healthy + restart: on-failure + image: bookstore-app:latest + build: + context: . + dockerfile: Dockerfile + env_file: ./.env + ports: + - $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT + - $DEBUG_PORT:$DEBUG_PORT + environment: + SPRING_APPLICATION_JSON: '{ + "spring.datasource.url": "jdbc:mysql://mysqldb:$MYSQL_DOCKER_PORT/$MYSQL_DB", + "spring.datasource.username": "$MYSQL_USER", + "spring.datasource.password": "$MYSQL_PASSWORD", + "spring.datasource.driver-class-name": "com.mysql.cj.jdbc.Driver", + "spring.jpa.hibernate.ddl-auto": "validate", + "spring.jpa.open-in-view": "false", + "token.expiration": "$TOKEN_EXPIRATION", + "token.secret": "$TOKEN_SECRET" + }' \ No newline at end of file diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 7da1843..56b6c57 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -1,11 +1,3 @@ spring.application.name=book-store -spring.datasource.url=jdbc:mysql://localhost:3306/mate -spring.datasource.username=root -spring.datasource.password=1111 -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.jpa.hibernate.ddl-auto=validate -spring.jpa.show-sql=true spring.jackson.deserialization.fail-on-unknown-properties=true -spring.jpa.open-in-view=false -token.expiration=1200000 -token.secret=123very_confidential_token1234567890 +springdoc.swagger-ui.path=/swagger-ui.html