Skip to content

Commit

Permalink
Create the Todos REST API (#1)
Browse files Browse the repository at this point in the history
* Establish the properties required to execute

* Setup the Dockerfile and the Compose file

* Create the Entity, Repository and Controller classes

* Create the Style file and the View HTML files

* Fix a few bugs and improve the codebase

* Optimize the Dockerfile and the Compose file

* Add reverse proxy using Nginx Image

* Add some basic documentation to the README.md file
  • Loading branch information
subhatav authored Apr 21, 2023
1 parent 67f48eb commit c008533
Show file tree
Hide file tree
Showing 19 changed files with 879 additions and 9 deletions.
14 changes: 14 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=p4$$w0rd

MYSQLDB_DATABASE=todos-db

MYSQLDB_LOCAL_PORT=4242
MYSQLDB_DOCKER_PORT=3306

SPRING_LOCAL_PORT=6969
SPRING_DOCKER_PORT=8080

NGINX_LOCAL_PORT=99
NGINX_DOCKER_PORT=80
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.m2/

### IntelliJ IDEA ###
.idea
Expand All @@ -6,4 +7,4 @@
*.ipr

### VS Code ###
.vscode/
.vscode/
99 changes: 98 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,98 @@
# todos-box
# Todos Application

This *application* is used to **manage** your *day-to-day* or even *long-term* **todos**!

<hr/>

## Requirements

The **stable** releases of the following *technologies* are used:

| Technology | Version |
| ----------- | ------- |
| JDK | 17+ |
| Spring Boot | 3.0+ |
| Lombok | 1.18+ |
| MySQL | 8.0+ |
| Maven | 3.8+ |
| Thymeleaf | 3.1+ |
| Bootstrap | 4.6+ |
| jQuery | 3.6+ |
| Docker | 20+ |

P.S. For *production* purposes, only **Docker** is *sufficient*.

<hr/>

## Development

The *database* can be **monitored** as follows:

### Usage

**Run** the following *command* for Docker **Container**:

>docker run --detach --env MYSQL_ROOT_PASSWORD=dev-root --env MYSQL_USER=dev-user --env MYSQL_PASSWORD=dev-pass --env MYSQL_DATABASE=dev-todos --name dev-db --publish 3306:3306 mysql:8-oracle
**Use** the following *properties* for **Spring** in the "*application.properties*" file:

```
spring.datasource.url=jdbc:mysql://localhost:3306/dev-todos
spring.datasource.username=dev-user
spring.datasource.password=dev-pass
```

**Run** the "*TodosApplication*" class with this *command*:

>mvn spring-boot:run
Go to this *URL* to **use** the *application*:

>http://localhost:8080/todos
### Monitoring

**Connect** to the MySQL **DB** at the following *URL*:

>jdbc:mysql://localhost:3306/dev-todos
... with these **credentials**:

>dev-user:dev-pass
<hr/>

## Production

The *application* can be **deployed** as follows:


### Usage

**Run** the following *command* for Docker **Compose**:

>docker-compose up
Go to this *URL* to **use** the *application*:

>http://localhost:6969/todos
**Stop** the following *command* for Docker **Compose**:

>docker-compose down
### Monitoring

**Connect** to the MySQL **DB** at the following *URL*:

>jdbc:mysql://localhost:4242/todos-db
... with these **credentials**:

>root:p4$$w0rd
<hr/>

**Thank** you for *using* it! :D

<hr/>
70 changes: 70 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

version: '3.9'

services:

data:

image: mysql:8-oracle
container_name: 'todos-data'

restart: unless-stopped

env_file: ./.env
ports:
- $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT

environment:
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE

volumes:
- db:/var/lib/mysql

app:

depends_on:
- data

build: ./todos-app

image: 'todos-app:1.0.0'
container_name: 'todos-app'

restart: on-failure

env_file: ./.env
ports:
- $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT

environment:
SPRING_APPLICATION_JSON: '{
"spring.datasource.url" : "jdbc:mysql://data:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE",
"spring.datasource.username" : "$MYSQLDB_USER",
"spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
"spring.jpa.hibernate.ddl-auto" : "update",
"spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQLDialect"
}'

stdin_open: true
tty: true

proxy:

depends_on:
- app

image: nginx:1.24-alpine
container_name: 'todos-proxy'

restart: always

env_file: ./.env
ports:
- $NGINX_LOCAL_PORT:$NGINX_DOCKER_PORT

volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro

volumes:
db:
18 changes: 18 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

events {}

http {

server {

listen 80;

location / {
proxy_pass http://app:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
8 changes: 8 additions & 0 deletions todos-app/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Ignore all files in the root directory
*
# Include the src directory
!src/
# Include the pom.xml file
!pom.xml
# Exclude the target directory and its contents
target/
3 changes: 1 addition & 2 deletions todos-app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

target/

!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
!**/src/test/**/target/
27 changes: 27 additions & 0 deletions todos-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

# Cache the Dependencies

FROM maven:3.9.1-eclipse-temurin-17-alpine AS builder

WORKDIR /todos-app
COPY pom.xml .

RUN mvn dependency:go-offline

# Package the Application

COPY src/ ./src/
RUN mvn package -DskipTests

# Create a Slim Image

FROM eclipse-temurin:17-jre-alpine AS runner

LABEL version="1.0.0"
LABEL author="Ph4nToM"
LABEL maintainer="subhatav@gmail.com"

ENV SPRING_PROFILES_ACTIVE=production
COPY --from=builder /todos-app/target/todos-app-1.0.0-SNAPSHOT.jar app.jar

ENTRYPOINT ["java", "-jar", "/app.jar"]
45 changes: 42 additions & 3 deletions todos-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<version>3.0.6</version>
<relativePath/>
</parent>

<groupId>com.ph4ntom.of.codes</groupId>
<artifactId>todos-app</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>todos-app</name>
<description>todos-app</description>
<name>Todos Application</name>
<description>Manage your Todos!</description>

<properties>
<java.version>17</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -37,11 +38,49 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.webjars/bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<!--<version>5.2.3</version>-->
<version>4.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/jquery -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/webjars-locator-core -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
<version>0.52</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootApplication
public class TodosApplication {

public static void main(final String[] args) {
public static void main(final String[] arguments) {

SpringApplication.run(TodosApplication.class, args);
SpringApplication.run(TodosApplication.class, arguments);
}
}
Loading

0 comments on commit c008533

Please sign in to comment.