Skip to content

Commit ccb72a4

Browse files
Added building inside one docker command and fixed sometimes db not
ready.
1 parent 87a5627 commit ccb72a4

File tree

4 files changed

+74
-54
lines changed

4 files changed

+74
-54
lines changed

Dockerfile

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
FROM openjdk:17
2-
add target/*.jar rest-demo.jar
3-
ENTRYPOINT ["java", "-jar", "rest-demo.jar"]
1+
# Has Maven.
2+
FROM maven:3.8.8-eclipse-temurin-17 AS build
43

4+
WORKDIR /app
5+
6+
COPY pom.xml .
7+
COPY src ./src
8+
9+
# Run maven.
10+
RUN mvn clean package -DskipTests
11+
12+
# Next, run the application.
13+
FROM openjdk:17-jdk-slim
14+
15+
WORKDIR /app
16+
17+
COPY --from=build /app/target/*.jar rest-demo.jar
18+
19+
ENTRYPOINT ["java", "-jar", "rest-demo.jar"]

README.md

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<img id="codemen" src="codemen.png">
44
</p>
55

6-
## Backend task Eng
6+
## Backend task Eng.
77

88
Back End should include API's compliant with REST. It should include following actions:
99
Adding user,
@@ -19,7 +19,7 @@ Description of data from API:
1919

2020
https://jsonplaceholder.typicode.com/users
2121

22-
## Backend task Fin
22+
## Backend task Fin.
2323

2424
Back Endissä tulisi olla REST rajapinnat käyttäjien lisäämiseen, poistamiseen ja muokkaamiseen, kaikkien käyttäjien hakemiseen siivutettuna sekä vielä käyttäjien hakemiseen nimen perusteella.
2525

@@ -30,73 +30,72 @@ Rajapinnan datasta:
3030

3131
https://jsonplaceholder.typicode.com/users
3232

33-
### Checklist
33+
### Checklist.
3434

3535
- ~~Spring API documentation.~~ ✔️
36-
[Open-API](https://springdoc.org/#getting-started) was used, because Swagger current version didn't support the newest Spring Boot. To look documentation, navigate to `http://localhost:8080/swagger-ui/index.html`
37-
- ~~Look for example [Swagger](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)~~
38-
- ~~Some database~~ ✔️
39-
MySQL
36+
[Open-API](https://springdoc.org/#getting-started) was used, because Swagger current version didn't support the newest Spring Boot. To look documentation, navigate to `http://localhost:8080/swagger-ui/index.html`.
37+
- ~~Look for example [Swagger](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)~~.
38+
- ~~Some database~~. ✔️
39+
MySQL.
4040
- ~~Some way to initialize random users.~~ ✔️
41-
`@PostConstruct`
42-
- ~~For example MySQL or Spring Boot initializing way~~
43-
- ~~Creating database startup mechanism~~ ✔️
44-
Hibernate takes care of creating database and table
45-
- ~~Make instructions `.md`~~ ✔️
46-
- ~~CRUD operations~~ ✔️
47-
- ~~Add~~ ✔️
41+
`@PostConstruct`.
42+
- ~~For example MySQL or Spring Boot initializing way~~.
43+
- ~~Creating database startup mechanism~~. ✔️
44+
Hibernate takes care of creating database and table.
45+
- ~~Make instructions `.md`~~. ✔️
46+
- ~~CRUD operations~~. ✔️
47+
- ~~Add~~. ✔️
4848
Adding user is done making **POST** message to `/api/v1/users`.
49-
- ~~Remove~~ ✔️
50-
Removing user is done making **DELETE** message to `/api/v1/users/{userId}`
51-
- ~~Modify **PUT**(PUT/POST/PATCH)~~ ✔️
52-
Modifying user is done making **PUT** message to `/api/v1/users`
53-
- ~~Read~~ ✔️
54-
Return all users when making **GET** request to `/api/v1/users`
55-
- ~~Return all as paged~~ ✔️
49+
- ~~Remove~~. ✔️
50+
Removing user is done making **DELETE** message to `/api/v1/users/{userId}`.
51+
- ~~Modify **PUT**(PUT/POST/PATCH)~~. ✔️
52+
Modifying user is done making **PUT** message to `/api/v1/users`.
53+
- ~~Read~~. ✔️
54+
Return all users when making **GET** request to `/api/v1/users`.
55+
- ~~Return all as paged~~. ✔️
5656
Returns wanted amount of users, when perform **GET** message `/api/v1/users?pageSize=10` where `pageSize` URL parameter is present.
57-
- ~~Simple paging~~
58-
- ~~[Possible paging](https://docs.spring.io/spring-data/rest/docs/2.0.0.M1/reference/html/paging-chapter.html)~~
59-
- ~~Return users based on name~~ ✔️
57+
- ~~Simple paging~~.
58+
- ~~[Possible paging](https://docs.spring.io/spring-data/rest/docs/2.0.0.M1/reference/html/paging-chapter.html)~~.
59+
- ~~Return users based on name~~. ✔️
6060
Returns all users based on name, when perform **GET** message to`/api/v1/users?name=pekka` here `name` URL parameter is present.
61-
- ~~Tests~~ ✔️
61+
- ~~Tests~~. ✔️
6262
Service level tests.
6363
- ~~Following error were introduced when using plain old Eclipse `error java.lang.NoSuchMethodError: 'java.util.Set`.~~ ✔️
64-
This was fixed using `Spring Tools 4 for Eclipse`
64+
This was fixed using `Spring Tools 4 for Eclipse`.
6565
- Unit tests were not introduced since business logic was fairly simple.
6666
- ~~Option to add docker to tests, so test db and production db data won't get mixed up.~~ ✔️
67-
- Other bonuses would be to implement sort, offset and indexes into query logic. (BONUS)
67+
- Other bonuses would be to implement sort, offset and indexes into query logic. (BONUS).
6868
- For the sake of time authentication was not introduced. If there would be such a time, usage of `JWT Bearer Token to Swagger` would be an option to go.
69-
- ~~Get user with specified userId with GET message. (BONUS)~~ ✔️
70-
Do **GET** request for following address `/api/v1/users/{userId}`
71-
- ~~Add support for saving multiple users at same time. This is for `saveAll()`. [BatchInserts](https://www.baeldung.com/spring-data-jpa-batch-inserts) (BONUS)~~ ✔️
69+
- ~~Get user with specified userId with GET message. (BONUS)~~. ✔️
70+
Do **GET** request for following address `/api/v1/users/{userId}`.
71+
- ~~Add support for saving multiple users at same time. This is for `saveAll()` [BatchInserts](https://www.baeldung.com/spring-data-jpa-batch-inserts) (BONUS)~~. ✔️
7272

73-
# How to run (Recommended)
73+
# How to run (Recommended).
7474

75-
> Requirements for running locally
76-
> 1. Have Docker. Tested with [Docker Desktop 4.25.2](https://www.docker.com/)
75+
> Requirements for running locally:
76+
> 1. Have Docker. Tested with [Docker Desktop 4.25.2](https://www.docker.com/).
7777
78-
1. Pull repository
79-
2. Go to the project folder
80-
3. Execute `./mvnw package` to build `.jar` file
81-
4. Execute with Docker `docker-compose up --build`
82-
5. Go to documentation and start using `http://localhost:8080/swagger-ui/index.html`
78+
1. Pull repository.
79+
2. Go to the project folder.
80+
3. Execute with Docker `docker-compose up --build`.
81+
4. Go to documentation and start using `http://localhost:8080/swagger-ui/index.html`.
8382

84-
- **Only starting inside IDE**. Starting in Eclipse IDE for debugging purposes. We can start db separately and then start debug version from Eclipse IDE. Starting db from docker independently `docker run -d -p 3307:3306 --name mysqldb -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=user_rest_demo mysql`
83+
- **Only starting inside IDE**. Starting in Eclipse IDE for debugging purposes. We can start db separately and then start debug version from Eclipse IDE. Starting db from docker independently `docker run -d -p 3307:3306 --name mysqldb -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=user_rest_demo mysql`.
8584

86-
- For the future, if there is time, there should be a way to build all in one command and set this process up.
85+
- ~~For the future, if there is time, there should be a way to build all in one command and set this process up.~~
8786

88-
# How to run (Optional, old way)
87+
# How to run (Optional, old way).
8988

90-
> Requirements for running locally
91-
> 1. Java JDK 17 minimum. Tested with [OpenJDK21](https://jdk.java.net/21/)
92-
> 2. MySQL server running. Tested with [MySQL 8.x.xx](https://dev.mysql.com/downloads/installer/). Server should be running in port `3306` and root password **root** and root user **root**
89+
> Requirements for running locally.
90+
> 1. Java JDK 17 minimum. Tested with [OpenJDK21](https://jdk.java.net/21/).
91+
> 2. MySQL server running. Tested with [MySQL 8.x.xx](https://dev.mysql.com/downloads/installer/). Server should be running in port `3306` and root password **root** and root user **root**.
9392
94-
> Maven should not be needed since using Maven wrapper `mvnw`
95-
> Docker is needed to run tests
93+
> Maven should not be needed since using Maven wrapper `mvnw`.
94+
> Docker is needed to run tests.
9695
97-
1. Pull repository
98-
2. Go to the project folder and build the `.jar` file and execute. `./mvnw spring-boot:run`
99-
3. Go to documentation and start using `http://localhost:8080/swagger-ui/index.html`
96+
1. Pull repository.
97+
2. Go to the project folder and build the `.jar` file and execute. `./mvnw spring-boot:run`.
98+
3. Go to documentation and start using `http://localhost:8080/swagger-ui/index.html`.
10099

101100
- This has been tested. ✔️
102101
~~For the future, if there is time, there should be a docker image for setting this process up.~~

docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ services:
1212
MYSQL_USER: root
1313
MYSQL_PASSWORD: root
1414
MYSQL_PORT: 3306
15+
depends_on: # Ensure db running first before springboot app
16+
- mysqldb
17+
1518
mysqldb: # When same name, docker will create under same network
1619
container_name: mysqldb # When same name, docker will create under same network
1720
image: mysql

src/main/resources/application.properties

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
#spring.jpa.hibernate.ddl-auto=update
21
#spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/user_rest_demo?createDatabaseIfNotExist=true
32
#spring.datasource.username=${MYSQL_USER:root}
43
#spring.datasource.password=${MYSQL_PASSWORD:root}
54

6-
spring.datasource.url=jdbc:mysql://localhost:3306/user_rest_demo?createDatabaseIfNotExist=true
5+
# Below should be used if db start from Docker container!
6+
spring.datasource.url=jdbc:mysql://mysqldb:3306/user_rest_demo?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true
7+
# Below should be used if db start locally!
8+
# spring.datasource.url=jdbc:mysql://localhost:3306/user_rest_demo?createDatabaseIfNotExist=true
9+
710
spring.datasource.username=root
811
spring.datasource.password=root
912

0 commit comments

Comments
 (0)