From ffd58e34e7468ae5205fd0350bedcc86c0166918 Mon Sep 17 00:00:00 2001 From: Manuel Aude Morales Date: Fri, 2 Jul 2021 00:26:55 -0500 Subject: [PATCH] Added the todo-api quarkus application. --- todo-api/.dockerignore | 5 + todo-api/.gitignore | 39 +++ todo-api/README.md | 50 ++++ todo-api/pom.xml | 171 +++++++++++++ todo-api/src/main/docker/Dockerfile.fast-jar | 54 ++++ todo-api/src/main/docker/Dockerfile.jvm | 51 ++++ todo-api/src/main/docker/Dockerfile.native | 27 ++ .../training/openshift/todoapi/Item.java | 12 + .../openshift/todoapi/ItemResource.java | 19 ++ todo-api/src/main/jkube/.gitkeep | 0 .../resources/META-INF/resources/index.html | 242 ++++++++++++++++++ .../src/main/resources/application.properties | 5 + 12 files changed, 675 insertions(+) create mode 100644 todo-api/.dockerignore create mode 100644 todo-api/.gitignore create mode 100644 todo-api/README.md create mode 100644 todo-api/pom.xml create mode 100644 todo-api/src/main/docker/Dockerfile.fast-jar create mode 100644 todo-api/src/main/docker/Dockerfile.jvm create mode 100644 todo-api/src/main/docker/Dockerfile.native create mode 100644 todo-api/src/main/java/com/redhat/training/openshift/todoapi/Item.java create mode 100644 todo-api/src/main/java/com/redhat/training/openshift/todoapi/ItemResource.java create mode 100644 todo-api/src/main/jkube/.gitkeep create mode 100644 todo-api/src/main/resources/META-INF/resources/index.html create mode 100644 todo-api/src/main/resources/application.properties diff --git a/todo-api/.dockerignore b/todo-api/.dockerignore new file mode 100644 index 0000000..94810d0 --- /dev/null +++ b/todo-api/.dockerignore @@ -0,0 +1,5 @@ +* +!target/*-runner +!target/*-runner.jar +!target/lib/* +!target/quarkus-app/* \ No newline at end of file diff --git a/todo-api/.gitignore b/todo-api/.gitignore new file mode 100644 index 0000000..bdf57ce --- /dev/null +++ b/todo-api/.gitignore @@ -0,0 +1,39 @@ +#Maven +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +release.properties + +# Eclipse +.project +.classpath +.settings/ +bin/ + +# IntelliJ +.idea +*.ipr +*.iml +*.iws + +# NetBeans +nb-configuration.xml + +# Visual Studio Code +.vscode +.factorypath + +# OSX +.DS_Store + +# Vim +*.swp +*.swo + +# patch +*.orig +*.rej + +# Local environment +.env diff --git a/todo-api/README.md b/todo-api/README.md new file mode 100644 index 0000000..1d457ee --- /dev/null +++ b/todo-api/README.md @@ -0,0 +1,50 @@ +# todo-api project + +This project uses Quarkus, the Supersonic Subatomic Java Framework. + +If you want to learn more about Quarkus, please visit its website: https://quarkus.io/ . + +## Running the application in dev mode + +You can run your application in dev mode that enables live coding using: +```shell script +./mvnw compile quarkus:dev +``` + +## Packaging and running the application + +The application can be packaged using: +```shell script +./mvnw package +``` +It produces the `todo-api-1.0.0-SNAPSHOT-runner.jar` file in the `/target` directory. +Be aware that it’s not an _über-jar_ as the dependencies are copied into the `target/lib` directory. + +If you want to build an _über-jar_, execute the following command: +```shell script +./mvnw package -Dquarkus.package.type=uber-jar +``` + +The application is now runnable using `java -jar target/todo-api-1.0.0-SNAPSHOT-runner.jar`. + +## Creating a native executable + +You can create a native executable using: +```shell script +./mvnw package -Pnative +``` + +Or, if you don't have GraalVM installed, you can run the native executable build in a container using: +```shell script +./mvnw package -Pnative -Dquarkus.native.container-build=true +``` + +You can then execute your native executable with: `./target/todo-api-1.0.0-SNAPSHOT-runner` + +If you want to learn more about building native executables, please consult https://quarkus.io/guides/maven-tooling.html. + +# RESTEasy JAX-RS + +

A Hello World RESTEasy resource

+ +Guide: https://quarkus.io/guides/rest-json diff --git a/todo-api/pom.xml b/todo-api/pom.xml new file mode 100644 index 0000000..97ad7d1 --- /dev/null +++ b/todo-api/pom.xml @@ -0,0 +1,171 @@ + + + 4.0.0 + com.redhat.training.openshift.todoapi + todo-api + 1.0.0-SNAPSHOT + + 3.8.1 + true + 11 + 11 + UTF-8 + UTF-8 + 1.11.7.Final-redhat-00009 + quarkus-universe-bom + com.redhat.quarkus + 1.11.7.Final-redhat-00009 + 3.0.0-M5 + true + + + + + ${quarkus.platform.group-id} + ${quarkus.platform.artifact-id} + ${quarkus.platform.version} + pom + import + + + + + + io.quarkus + quarkus-arc + + + io.quarkus + quarkus-resteasy + + + io.quarkus + quarkus-junit5 + test + + + io.rest-assured + rest-assured + test + + + io.quarkus + quarkus-resteasy-jsonb + + + io.quarkus + quarkus-hibernate-orm-panache + + + io.quarkus + quarkus-hibernate-orm + + + io.quarkus + quarkus-jdbc-mysql + + + io.quarkus + quarkus-hibernate-orm-rest-data-panache + + + + + + true + + + false + + redhat + https://maven.repository.redhat.com/ga + + + + + + true + + + false + + redhat + https://maven.repository.redhat.com/ga + + + + + + io.quarkus + quarkus-maven-plugin + ${quarkus-plugin.version} + true + + + + build + generate-code + generate-code-tests + + + + + + maven-compiler-plugin + ${compiler-plugin.version} + + + maven-surefire-plugin + ${surefire-plugin.version} + + + org.jboss.logmanager.LogManager + ${maven.home} + + + + + org.eclipse.jkube + openshift-maven-plugin + 1.2.0 + + + + + + native + + + native + + + + + + maven-failsafe-plugin + ${surefire-plugin.version} + + + + integration-test + verify + + + + ${project.build.directory}/${project.build.finalName}-runner + org.jboss.logmanager.LogManager + ${maven.home} + + + + + + + + + native + + + + diff --git a/todo-api/src/main/docker/Dockerfile.fast-jar b/todo-api/src/main/docker/Dockerfile.fast-jar new file mode 100644 index 0000000..e9334af --- /dev/null +++ b/todo-api/src/main/docker/Dockerfile.fast-jar @@ -0,0 +1,54 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the container image run: +# +# ./mvnw package -Dquarkus.package.type=fast-jar +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.fast-jar -t quarkus/todo-api-fast-jar . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/todo-api-fast-jar +# +# If you want to include the debug port into your docker image +# you will have to expose the debug port (default 5005) like this : EXPOSE 8080 5050 +# +# Then run the container using : +# +# docker run -i --rm -p 8080:8080 -p 5005:5005 -e JAVA_ENABLE_DEBUG="true" quarkus/todo-api-fast-jar +# +### +FROM registry.access.redhat.com/ubi8/ubi-minimal:8.3 + +ARG JAVA_PACKAGE=java-11-openjdk-headless +ARG RUN_JAVA_VERSION=1.3.8 +ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' +# Install java and the run-java script +# Also set up permissions for user `1001` +RUN microdnf install curl ca-certificates ${JAVA_PACKAGE} \ + && microdnf update \ + && microdnf clean all \ + && mkdir /deployments \ + && chown 1001 /deployments \ + && chmod "g+rwX" /deployments \ + && chown 1001:root /deployments \ + && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \ + && chown 1001 /deployments/run-java.sh \ + && chmod 540 /deployments/run-java.sh \ + && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/conf/security/java.security + +# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size. +ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" +# We make four distinct layers so if there are application changes the library layers can be re-used +COPY --chown=1001 target/quarkus-app/lib/ /deployments/lib/ +COPY --chown=1001 target/quarkus-app/*.jar /deployments/ +COPY --chown=1001 target/quarkus-app/app/ /deployments/app/ +COPY --chown=1001 target/quarkus-app/quarkus/ /deployments/quarkus/ + +EXPOSE 8080 +USER 1001 + +ENTRYPOINT [ "/deployments/run-java.sh" ] diff --git a/todo-api/src/main/docker/Dockerfile.jvm b/todo-api/src/main/docker/Dockerfile.jvm new file mode 100644 index 0000000..58972e9 --- /dev/null +++ b/todo-api/src/main/docker/Dockerfile.jvm @@ -0,0 +1,51 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the container image run: +# +# ./mvnw package +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/todo-api-jvm . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/todo-api-jvm +# +# If you want to include the debug port into your docker image +# you will have to expose the debug port (default 5005) like this : EXPOSE 8080 5050 +# +# Then run the container using : +# +# docker run -i --rm -p 8080:8080 -p 5005:5005 -e JAVA_ENABLE_DEBUG="true" quarkus/todo-api-jvm +# +### +FROM registry.access.redhat.com/ubi8/ubi-minimal:8.3 + +ARG JAVA_PACKAGE=java-11-openjdk-headless +ARG RUN_JAVA_VERSION=1.3.8 +ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' +# Install java and the run-java script +# Also set up permissions for user `1001` +RUN microdnf install curl ca-certificates ${JAVA_PACKAGE} \ + && microdnf update \ + && microdnf clean all \ + && mkdir /deployments \ + && chown 1001 /deployments \ + && chmod "g+rwX" /deployments \ + && chown 1001:root /deployments \ + && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \ + && chown 1001 /deployments/run-java.sh \ + && chmod 540 /deployments/run-java.sh \ + && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/conf/security/java.security + +# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size. +ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" +COPY target/lib/* /deployments/lib/ +COPY target/*-runner.jar /deployments/app.jar + +EXPOSE 8080 +USER 1001 + +ENTRYPOINT [ "/deployments/run-java.sh" ] diff --git a/todo-api/src/main/docker/Dockerfile.native b/todo-api/src/main/docker/Dockerfile.native new file mode 100644 index 0000000..35fa2f4 --- /dev/null +++ b/todo-api/src/main/docker/Dockerfile.native @@ -0,0 +1,27 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode +# +# Before building the container image run: +# +# ./mvnw package -Pnative +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.native -t quarkus/todo-api . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/todo-api +# +### +FROM registry.access.redhat.com/ubi8/ubi-minimal:8.3 +WORKDIR /work/ +RUN chown 1001 /work \ + && chmod "g+rwX" /work \ + && chown 1001:root /work +COPY --chown=1001:root target/*-runner /work/application + +EXPOSE 8080 +USER 1001 + +CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] diff --git a/todo-api/src/main/java/com/redhat/training/openshift/todoapi/Item.java b/todo-api/src/main/java/com/redhat/training/openshift/todoapi/Item.java new file mode 100644 index 0000000..e424ca6 --- /dev/null +++ b/todo-api/src/main/java/com/redhat/training/openshift/todoapi/Item.java @@ -0,0 +1,12 @@ +package com.redhat.training.openshift.todoapi; + +import javax.persistence.Entity; + +import io.quarkus.hibernate.orm.panache.PanacheEntity; + +@Entity +public class Item extends PanacheEntity { + public String description; + public boolean done; + +} diff --git a/todo-api/src/main/java/com/redhat/training/openshift/todoapi/ItemResource.java b/todo-api/src/main/java/com/redhat/training/openshift/todoapi/ItemResource.java new file mode 100644 index 0000000..e69f959 --- /dev/null +++ b/todo-api/src/main/java/com/redhat/training/openshift/todoapi/ItemResource.java @@ -0,0 +1,19 @@ +package com.redhat.training.openshift.todoapi; + + +import io.quarkus.hibernate.orm.rest.data.panache.PanacheEntityResource; +import io.quarkus.rest.data.panache.MethodProperties; +import io.quarkus.rest.data.panache.ResourceProperties; + + +@ResourceProperties(path = "/todo/api/items") +public interface ItemResource extends PanacheEntityResource { + @MethodProperties(exposed = false) + Item add(Item item); + + @MethodProperties(exposed = false) + Item update(Long id, Item existingItem); + + @MethodProperties(exposed = false) + boolean delete(Long id); +} \ No newline at end of file diff --git a/todo-api/src/main/jkube/.gitkeep b/todo-api/src/main/jkube/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/todo-api/src/main/resources/META-INF/resources/index.html b/todo-api/src/main/resources/META-INF/resources/index.html new file mode 100644 index 0000000..18fb455 --- /dev/null +++ b/todo-api/src/main/resources/META-INF/resources/index.html @@ -0,0 +1,242 @@ + + + + + todo-api - 1.0.0-SNAPSHOT + + + + + + +
+
+

Congratulations, you have created a new Quarkus cloud application.

+ +

Why do you see this?

+ +

This page is served by Quarkus. The source is in + src/main/resources/META-INF/resources/index.html.

+ +

What can I do from here?

+ +

If not already done, run the application in dev mode using: ./mvnw compile quarkus:dev. +

+
    +
  • Play with your example code in src/main/java: +
    +
    +
    +

    RESTEasy JAX-RS

    + Guide +
    +
    +

    A Hello World RESTEasy resource

    + +
    + +
    + +
    +
  • +
  • Your static assets are located in src/main/resources/META-INF/resources.
  • +
  • Configure your application in src/main/resources/application.properties.
  • +
+

Do you like Quarkus?

+

Go give it a star on GitHub.

+
+
+
+

Application

+
    +
  • GroupId: com.redhat.training.openshift.todoapi
  • +
  • ArtifactId: todo-api
  • +
  • Version: 1.0.0-SNAPSHOT
  • +
  • Quarkus Version: 1.11.7.Final-redhat-00009
  • +
+
+ +
+
+ + \ No newline at end of file diff --git a/todo-api/src/main/resources/application.properties b/todo-api/src/main/resources/application.properties new file mode 100644 index 0000000..5de5412 --- /dev/null +++ b/todo-api/src/main/resources/application.properties @@ -0,0 +1,5 @@ + +quarkus.datasource.db-kind = mysql +quarkus.datasource.username = ${DATABASE_USER} +quarkus.datasource.password = ${DATABASE_PASSWORD} +quarkus.datasource.jdbc.url = jdbc:mysql://${DATABASE_SVC_HOSTNAME}:3306/${DATABASE_NAME}