Skip to content

Commit

Permalink
docker-compose fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gtiwari333 committed Dec 4, 2023
1 parent ea9e7c9 commit 06adf3d
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 40 deletions.
30 changes: 20 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# spring boot initializes this docker-compose (with local profile) if `spring.docker.compose.profiles.active=local` property is set
version: '3'
services:
emailhog:
Expand All @@ -7,16 +8,25 @@ services:
- 1025:1025
networks:
- note-app-network
# mysql:
# image: 'mysql:8.0.30'
# environment:
# - MYSQL_ROOT_PASSWORD=password
# - MYSQL_DATABASE=seedapp
# ports:
# - 3306:3306
# command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8mb4 --explicit_defaults_for_timestamp
# networks:
# - note-app-network
profiles:
- mailHog
mysql:
image: 'mysql'
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=noteappdb
ports:
- 3306:3306
command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8mb4 --explicit_defaults_for_timestamp
networks:
- note-app-network
labels:
#org.springframework.boot.ignore: true #use this to omit this from initialization
# this will be sent to org.springframework.boot.docker.compose.service.connection.jdbc.JdbcUrlBuilder.build to build jdbc URL
org.springframework.boot.jdbc.parameters: useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false
profiles:
- mysql

volumes:
esdata1:
driver: local
Expand Down
13 changes: 5 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,7 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<!-- GraalVMNativeImage: required at compile time for graalvm native compile -->
<!--target/spring-aot/main/sources/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfiguration__BeanDefinitions.java:4:25
java: package org.h2.server.web does not exist-->
<!--<scope>runtime</scope>-->
<scope>runtime</scope>
</dependency>
</dependencies>
<properties>
Expand All @@ -619,8 +616,8 @@
<id>docker</id>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
Expand All @@ -633,8 +630,8 @@
<id>prod</id>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/gt/app/DataCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

import gt.app.config.AppProperties;
import gt.app.config.Constants;
import gt.app.domain.AppUser;
import gt.app.domain.Authority;
import gt.app.domain.LiteUser;
import gt.app.domain.Note;
import gt.app.domain.*;
import gt.app.modules.note.NoteService;
import gt.app.modules.user.AuthorityService;
import gt.app.modules.user.UserService;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import jakarta.persistence.EntityManager;
import java.io.File;
import java.util.stream.Stream;

@Component
@Profile({Constants.SPRING_PROFILE_DEVELOPMENT, Constants.SPRING_PROFILE_TEST, Constants.SPRING_PROFILE_DOCKER})
Expand All @@ -38,11 +36,22 @@ public void ctxRefreshed(ContextRefreshedEvent evt) {
initData();
}

public void initData(){
public void initData() {
log.info("Context Refreshed !!, Initializing Data... ");

new File(appProperties.fileStorage().uploadFolder() + File.separator +"attachments").mkdirs();

File uploadFolder = new File(appProperties.fileStorage().uploadFolder());
if (!uploadFolder.exists()) {
if (uploadFolder.mkdirs() && Stream.of(ReceivedFile.FileGroup.values()).allMatch(f -> new File(uploadFolder.getAbsolutePath()).mkdir())) {
log.info("Upload folder created successfully");
} else {
log.info("Failure to create upload folder");
}
}

if (userService.existsByUniqueId("system")) {
log.info("DB already initialized !!!");
return;
}
Authority adminAuthority = new Authority();
adminAuthority.setName(Constants.ROLE_ADMIN);
authorityService.save(adminAuthority);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/gt/app/modules/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,7 @@ public AppUser save(AppUser u) {
return userRepository.save(u);
}


public boolean existsByUniqueId(String username) {
return userRepository.existsByUniqueId(username);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application-default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 'default' profile gets selected when we do not specify any profile
spring:
profiles:
active: dev
6 changes: 5 additions & 1 deletion src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ spring:
show-sql: false
datasource:
url: jdbc:h2:mem:testdb
h2:
h2: #dev uses H2 and 'emailhog' docker container from docker-compose
console:
enabled: true #Access from http://localhost:8080/h2-console/
docker:
compose:
profiles:
active: mailHog
20 changes: 14 additions & 6 deletions src/main/resources/application-docker.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# meant to be used locally with docker-compose
#NOTE: do not run docker-compose separately when running app with this profile. kill the containers if they are running

# if you run the app with this `docker` spring profile, `all` docker-compose profile will be selected
# which will initialize MySQL. Also the jdbc url will be automatically created based on the docker-compose.yml file
spring:
#Access from http://localhost:8080/h2-console/, db url: jdbc:h2:mem:testdb, username: sa
h2:
console:
enabled: true
docker:
compose:
profiles:
active:
- mysql
- mailHog

spring.jpa.hibernate.ddl-auto: update


server:
port: 8080
16 changes: 12 additions & 4 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# setup mysql url and credentials
#spring:
# datasource:
# url:
#NOTE: do not run docker-compose separately when running app with this profile. kill the containers if they are running

# if you run the app with this `prod` spring profile, `all` docker-compose profile will be selected
# which will initialize MySQL. Also the jdbc url will be automatically created based on the docker-compose.yml file
spring:
docker:
compose:
profiles.active:
- mysql
- mailHog

#spring.jpa.hibernate.ddl-auto: none #should use liquibase//TODO:
5 changes: 3 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ spring:
password:
main:
lazy-initialization: false

server:
port: 8080

Expand All @@ -39,10 +38,12 @@ wro4j:
filter-url: /wro4j # this is the default, needs to be used in secConfig and the htmls


# custom properties for this project
app-properties:
file-storage:
upload-folder: ${java.io.tmpdir}


springdoc:
spring-doc:
enable-native-support: true
show-actuator: true

0 comments on commit 06adf3d

Please sign in to comment.