Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

005 - feat: Publish API methods #4

Open
wants to merge 13 commits into
base: feat-code-coverage
Choose a base branch
from
8 changes: 5 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ jobs:
java-version: "17"
cache: "maven"
- name: Compile
run: mvn compile
- name: Test
run: mvn verify
run: mvn clean compile
- name: Unit Test
run: mvn test
- name: Integration API Test
run: mvn verify -Pintegration-test
- uses: dorny/test-reporter@v1
with:
name: Test Results
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/test-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ on:
workflow_call:
workflow_dispatch:
workflow_run:
workflows: [ 'CI' ] # runs after CI workflow
types:
- completed
workflows: 'CI' # runs after CI workflow

jobs:
report:
runs-on: ubuntu-latest
Expand Down
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM maven:3.8.4-jdk-11
MAINTAINER ricardogarfe

RUN adduser --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password jetty
USER jetty
WORKDIR /home/jetty

EXPOSE 8080
# Run subsequent commands as jetty user
USER jetty

ADD pom.xml /home/jetty
ADD src /home/jetty/src

RUN mvn package
# Run script
CMD mvn jetty:run
73 changes: 71 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ Create a service to wrap those methods
* Divide: divide two numbers and return result
* divide by zero should return error message

Create a REST service to expose those methods with URLs in browser:

- /api/calculator/ping
- /api/calculator/add?x=8&y=26
- /api/calculator/sub?x=12&y=8
- /api/calculator/mul?x=11&y=8
- /api/calculator/div?x=12&y=12

## System requirements

* JDK-11
* Maven 3.8.4

## 1. Manually Build, Test, and Deploy By Maven

### 1.2 Run JUnit Test
### 1.1 Run JUnit Test

Maven execution:

Expand All @@ -55,6 +63,29 @@ Execute (linux/macos mvnw or windows mvnw.cmd):
$ ./mvnw clean test
```

### 1.2 Run Integration Test

```console
$ mvn clean integration-test
```

### 1.3 Run Locally

```console
$ mvn jetty:run
[INFO] Started Jetty Server
```

By default, the jetty port is 8080, so you should visit following urls in browser:

- http://localhost:8080/calculator/api/calculator/ping
- http://localhost:8080/calculator/api/calculator/add?x=8&y=26
- http://localhost:8080/calculator/api/calculator/sub?x=12&y=8
- http://localhost:8080/calculator/api/calculator/mul?x=11&y=8
- http://localhost:8080/calculator/api/calculator/div?x=12&y=12

To run in a different port, `mvn jetty:run -Djetty.port=<Your-Port>`.

## 2. Automatically Build and Test

[Github action ci](.github/workflows/ci.yml) step definition:
Expand All @@ -78,4 +109,42 @@ steps:

## 3. Containerize Your Web App

**TODO**
### 3.1. Build a docker image using Dockerfile:

```console
$ docker build -t myjetty --no-cache -f Dockerfile .
[+] Building 18.9s (11/11) FINISHED
```

### 3.2. Run docker image locally

```console
$ docker run --rm -p 8080:8080 myjetty
```

> Explain: --rm means delete the container after stopping it.

Access the web app at http://localhost:8080/api/calculator/ping in browser.

Press Control-C to stop and remove the container.

### 3.3. Run docker-compose environment

Define a service to use repository Docker image:

```yaml
version: '3.8'
services:
calculator:
build: .
ports:
- "8080:8080"
```

Run docker-compose environment:

```console
$ docker-compse up
```

Access the web app at http://localhost:8080/api/calculator/ping in browser.
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3.8'
services:
calculator:
build: .
ports:
- "8080:8080"
Loading