diff --git a/react-java-mysql/README.md b/react-java-mysql/README.md index 7fccc44..2b3bf1d 100644 --- a/react-java-mysql/README.md +++ b/react-java-mysql/README.md @@ -23,17 +23,17 @@ services: build: backend ... db: - image: mysql:5.7 + image: mysql:8.0.19 ... frontend: build: frontend ports: - - 80:9000 + - 9000:9000 ... ``` The compose file defines an application with three services `frontend`, `backend` and `db`. -When deploying the application, docker-compose maps port 80 of the frontend service container to port 9000 of the host as specified in the file. -Make sure port 80 on the host is not already being in use. +When deploying the application, docker-compose maps port 9000 of the frontend service container to port 9000 of the host as specified in the file. +Make sure port 9000 on the host is not already being in use. ## Deploy with docker-compose @@ -41,7 +41,7 @@ Make sure port 80 on the host is not already being in use. $ docker-compose up -d Creating network "react-java-mysql_default" with the default driver Building backend -Step 1/10 : FROM maven:3.5-jdk-9 AS build +Step 1/17 : FROM maven:3.6.3-jdk-11 AS builder ... Successfully tagged react-java-mysql_frontend:latest WARNING: Image for service frontend was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. @@ -57,11 +57,11 @@ Listing containers must show three containers running and the port mapping as be $ docker ps ONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a63dee74d79e react-java-mysql_backend "java -Djava.securit…" 39 seconds ago Up 37 seconds react-java-mysql_backend_1 -6a7364c0812e react-java-mysql_frontend "docker-entrypoint.s…" 39 seconds ago Up 33 seconds 0.0.0.0:80->9000/tcp react-java-mysql_frontend_1 -b176b18fbec4 mysql:5.7 "docker-entrypoint.s…" 39 seconds ago Up 37 seconds 3306/tcp, 33060/tcp react-java-mysql_db_1 +6a7364c0812e react-java-mysql_frontend "docker-entrypoint.s…" 39 seconds ago Up 33 seconds 0.0.0.0:9000->9000/tcp react-java-mysql_frontend_1 +b176b18fbec4 mysql:8.0.19 "docker-entrypoint.s…" 39 seconds ago Up 37 seconds 3306/tcp, 33060/tcp react-java-mysql_db_1 ``` -After the application starts, navigate to `http://localhost:80` in your web browser to get a colorful message. +After the application starts, navigate to `http://localhost:9000` in your web browser to get a colorful message. ``` My New React App ``` diff --git a/react-java-mysql/backend/Dockerfile b/react-java-mysql/backend/Dockerfile index 265d758..949dd0a 100755 --- a/react-java-mysql/backend/Dockerfile +++ b/react-java-mysql/backend/Dockerfile @@ -1,11 +1,20 @@ -FROM maven:3.5-jdk-9 AS build -COPY pom.xml . -RUN mvn --batch-mode dependency:resolve -COPY . . -RUN mvn --batch-mode package -RUN cp target/*jar target/app.jar +FROM maven:3.6.3-jdk-11 AS builder +WORKDIR /workdir/server +COPY pom.xml /workdir/server/pom.xml +RUN mvn dependency:go-offline -FROM openjdk:9-jre +COPY src /workdir/server/src +RUN mvn install +RUN mkdir -p target/depency +WORKDIR /workdir/server/target/dependency +RUN jar -xf ../*.jar + +FROM openjdk:11-jre-slim + +EXPOSE 8080 VOLUME /tmp -COPY --from=build target/app.jar app.jar -ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] +ARG DEPENDENCY=/workdir/server/target/dependency +COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib +COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF +COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app +ENTRYPOINT ["java","-cp","app:app/lib/*","com.company.project.Application"] \ No newline at end of file diff --git a/react-java-mysql/backend/pom.xml b/react-java-mysql/backend/pom.xml index aeef767..edd21fa 100755 --- a/react-java-mysql/backend/pom.xml +++ b/react-java-mysql/backend/pom.xml @@ -14,52 +14,61 @@ org.springframework.boot spring-boot-starter-parent - 2.0.3.RELEASE + 2.2.5.RELEASE UTF-8 UTF-8 - 1.8 + 11 org.springframework.boot - spring-boot-starter-jersey + spring-boot-starter-web org.springframework.boot - spring-boot-starter-web + spring-boot-starter-jersey - org.springframework.session - spring-session-core + org.springframework.boot + spring-boot-starter-jdbc org.springframework.boot - spring-boot-devtools + spring-boot-starter-data-jpa org.springframework.boot - spring-boot-starter-freemarker + spring-boot-starter-data-jpa - org.springframework.boot - spring-boot-starter-test - test + mysql + mysql-connector-java + runtime - org.projectlombok - lombok + org.springframework.boot + spring-boot-devtools + runtime + true - javax.xml.bind - jaxb-api - 2.3.0 + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + diff --git a/react-java-mysql/backend/src/main/java/com/company/project/controllers/HomeController.java b/react-java-mysql/backend/src/main/java/com/company/project/controllers/HomeController.java index 966839d..083e340 100755 --- a/react-java-mysql/backend/src/main/java/com/company/project/controllers/HomeController.java +++ b/react-java-mysql/backend/src/main/java/com/company/project/controllers/HomeController.java @@ -1,16 +1,21 @@ package com.company.project.controllers; -import org.springframework.stereotype.Controller; +import com.company.project.entity.Greeting; +import com.company.project.repository.GreetingRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; -@Controller +@RestController public class HomeController { + @Autowired + private GreetingRepository repository; + @GetMapping("/") - public String showHome(String name, Model model) { - return "home"; + public Greeting showHome(String name, Model model) { + return repository.findById(1).orElse(new Greeting("Not Found 😕")); } } diff --git a/react-java-mysql/backend/src/main/java/com/company/project/entity/Greeting.java b/react-java-mysql/backend/src/main/java/com/company/project/entity/Greeting.java new file mode 100644 index 0000000..1b9fb21 --- /dev/null +++ b/react-java-mysql/backend/src/main/java/com/company/project/entity/Greeting.java @@ -0,0 +1,61 @@ +package com.company.project.entity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Column; +import javax.persistence.GeneratedValue; + +@Entity +@Table(name = "greetings") +public class Greeting { + + @Id + @GeneratedValue + private int id; + @Column(nullable = false) + private String name; + + public Greeting() { + } + + public Greeting(String name) { + this.name = name; + } + + public Greeting(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Greeting greeting = (Greeting) o; + + return name.equals(greeting.name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } +} diff --git a/react-java-mysql/backend/src/main/java/com/company/project/repository/GreetingRepository.java b/react-java-mysql/backend/src/main/java/com/company/project/repository/GreetingRepository.java new file mode 100644 index 0000000..9c5ab6f --- /dev/null +++ b/react-java-mysql/backend/src/main/java/com/company/project/repository/GreetingRepository.java @@ -0,0 +1,9 @@ +package com.company.project.repository; + +import com.company.project.entity.Greeting; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface GreetingRepository extends CrudRepository { +} \ No newline at end of file diff --git a/react-java-mysql/backend/src/main/resources/application.properties b/react-java-mysql/backend/src/main/resources/application.properties index 8b13789..6aa9b40 100755 --- a/react-java-mysql/backend/src/main/resources/application.properties +++ b/react-java-mysql/backend/src/main/resources/application.properties @@ -1 +1,12 @@ +spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect +spring.jpa.hibernate.ddl-auto=none +spring.jpa.hibernate.show-sql=true + +spring.datasource.url=jdbc:mysql://${MYSQL_HOST:db}:3306/example +spring.datasource.username=root +spring.datasource.password=${MYSQL_PASSWORD:db-57xsl} +spring.datasource.initialization-mode=always +spring.datasource.initialize=true +spring.datasource.schema=classpath:/schema.sql +spring.datasource.continue-on-error=true diff --git a/react-java-mysql/backend/src/main/resources/data.sql b/react-java-mysql/backend/src/main/resources/data.sql new file mode 100644 index 0000000..71a06ba --- /dev/null +++ b/react-java-mysql/backend/src/main/resources/data.sql @@ -0,0 +1 @@ +INSERT INTO example.greetings(name) values ('Docker'); diff --git a/react-java-mysql/backend/src/main/resources/schema.sql b/react-java-mysql/backend/src/main/resources/schema.sql new file mode 100644 index 0000000..6901a3a --- /dev/null +++ b/react-java-mysql/backend/src/main/resources/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS example.greetings ( + id INTEGER AUTO_INCREMENT, + name varchar(50) NOT NULL, + PRIMARY KEY (id) +); diff --git a/react-java-mysql/backend/src/main/resources/templates/home.ftl b/react-java-mysql/backend/src/main/resources/templates/home.ftl deleted file mode 100755 index daba182..0000000 --- a/react-java-mysql/backend/src/main/resources/templates/home.ftl +++ /dev/null @@ -1,9 +0,0 @@ - - - - Getting Started: Serving Web Content - - - -

Hello from Docker!

- diff --git a/react-java-mysql/docker-compose.yaml b/react-java-mysql/docker-compose.yaml index f2621d6..33f89ea 100644 --- a/react-java-mysql/docker-compose.yaml +++ b/react-java-mysql/docker-compose.yaml @@ -2,25 +2,41 @@ version: "3.7" services: backend: build: backend + environment: + MYSQL_HOST: db + networks: + - react-spring + - spring-mysql + depends_on: + - db db: environment: MYSQL_DATABASE: example MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password - image: mysql:5.7 + image: mysql:8.0.19 restart: always secrets: - - db-password + - db-password volumes: - - db-data:/var/lib/mysql + - db-data:/var/lib/mysql + networks: + - spring-mysql frontend: build: frontend ports: - - 80:9000 + - 9000:9000 volumes: - - ./frontend:/project - - /project/node_modules + - ./frontend:/project + - /project/node_modules + networks: + - react-spring + depends_on: + - backend volumes: db-data: {} secrets: db-password: file: db/password.txt +networks: + react-spring: {} + spring-mysql: {} \ No newline at end of file