The todo-backend quickstart demonstrates how to implement a backend that exposes a HTTP API with Jakarta REST
to manage a list of ToDo which are persisted in a database with JPA.
This quickstart shows how to deploy a JBoss EAP application on OpenShift that connects to a PostgreSQL database also hosted on OpenShift.
The todo-backend quickstart demonstrates how to implement a backend that exposes a HTTP API with Jakarta REST
to manage a list of ToDo which are persisted in a database with JPA.
-
The backend exposes a HTTP API to manage a list of todos that complies with the specs defined at todobackend.com.
-
It requires a connection to a PostgreSQL database to persist the todos.
-
It can be build with JBoss EAP S2I images for cloud deployment
-
It is deployed on OpenShift using the Helm Chart for JBoss EAP.
The application this project produces is designed to be run on Red Hat JBoss Enterprise Application Platform 8.1 or later.
All you need to build this project is Java SE 17.0 or later, and Maven 3.6.0 or later. See Configure Maven to Build and Deploy the Quickstarts to make sure you are configured correctly for testing the quickstarts.
This backend is built using JBoss EAP S2I Builder and Runtime images.
When the image is built, org.jboss.eap.plugins:eap-maven-plugin plugin provisions the JBoss EAP application server and all the feature packs it needs for its features.
The layers are defined in the pom.xml file in the <configuration> section of the org.jboss.eap.plugins:eap-maven-plugin plugin:
<layers>
<layer>cloud-server</layer>
<layer>postgresql-datasource</layer>
</layers>The cloud-server layer provides everything needed to run the backend on OpenShift. This also includes access to
Jakarta EE APIs such as CDI, Jakarta REST, JPA, etc. These two layers comes from the JBoss EAP feature pack provided in the
JBoss EAP S2I builder image.
The postgresql-datasource layer provides a JDBC driver and DataSource to connect to a PostgreSQL database. It is also provided by
the org.jboss.eap:eap-datasources-galleon-pack feature pack.
The Git repository for this feature pack is hosted at https://github.com/jbossas/eap-datasources-galleon-pack.
It provides JDBC drivers and datasources for different databases but for this quickstart, we will only need the postgresql-datasource.
As mentioned, the JDBC drivers and datasource configuration that the backend uses to connect to the PostgreSQL database
is provided by the org.jboss.eap:eap-datasources-galleon-pack feature pack.
By default, it exposes a single datasource.
In the backend, the name of this datasource is ToDos and is specified in the persistence.xml to configure JPA:
<persistence-unit name="primary">
<jta-data-source>java:jboss/datasources/ToDos</jta-data-source>
</persistence-unit>At runtime, we only need a few environment variables to establish the connection from JBoss EAP to the external PostgreSQL database:
-
POSTGRESQL_DATABASE- the name of the database (that will be calledtodos) -
POSTGRESQL_SERVICE_HOST- the host to connect to the database -
POSTGRESQL_SERVICE_PORT- The port to connect to the database -
POSTGRESQL_USER&POSTGRESQL_PASSWORD- the credentials to connect to the database -
POSTGRESQL_DATASOURCE- The name of the datasources (as mentioned above, it will beToDos)
The Web frontend for this quickstart uses JavaScript calls to query the backend’s HTTP API.
We must enable Cross-Origin Resource Sharing (CORS) filters in the undertow subsystem of JBoss EAP to allow
these HTTP requests to succeed.
As we use S2I to provision the server and build the application, we provide a CLI script that contains all the commands to create and configure the CORS filters in Undertow. This script is located in the src/scripts/cors_filters.cli.
This script is executed at build time and will provide the following HTTP headers to enabled CORS:
-
Access-Control-Allow-Origin: * -
Access-Control-Allow-Methods: GET, POST, OPTION, PUT, DELETE, PATCH -
Access-Control-Allow-Headers: accept, authorization, content-type, x-requested-with -
Access-Control-Allow-Credentials: true -
Access-Control-Max-Age: 1
By default, the backend accepts requests from any origin (*). This is only simplicity. It is possible to restrict
the allowed origin using the environment variable CORS_ORIGIN at runtime.
The backend is packaged and deployed on a provisioned server:
$ mvn clean packageBefore running the backend locally, we need to have a local PostgreSQL database that we can connect to.
We use the postgresql docker image to create one:
$ docker run --name todo-backend-db -e POSTGRES_USER=todos -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 postgresThis will create a database named todos that we can connect to on localhost:5432 with the credentials todos / mysecretpassword.
With the PostgreSQL database running, we can start the backend by passing the required environment variables to connect to the database:
$ ./target/server/bin/standalone.sh -Denv.POSTGRESQL_DATABASE=todos -Denv.POSTGRESQL_DATASOURCE=ToDos -Denv.POSTGRESQL_SERVICE_HOST=localhost -Denv.POSTGRESQL_SERVICE_PORT=5432 -Denv.POSTGRESQL_USER=todos -Denv.POSTGRESQL_PASSWORD=mysecretpasswordThe backend is running, and we can use the HTTP API to manage a list of todos:
# get a list of todos
$ curl http://localhost:8080/todo-backend
[]
# create a todo with the title "This is my first todo item!"
$ curl -X POST -H "Content-Type: application/json" -d '{"title": "This is my first todo item!"}' http://localhost:8080/todo-backend
{"completed":false,"id":1,"order":0,"title":"This is my first todo item!","url":"https://localhost:8080/1"}%
# get a list of todos with the one that was just created
$ curl http://localhost:8080/todo-backend
[{"completed":false,"id":1,"order":0,"title":"This is my first todo item!","url":"https://localhost:8080/1"}]Please note that the quickstart includes integration tests, which may be executed using the following command:
$ mvn verify -Pintegration-testingOn OpenShift, the S2I build with Apache Maven uses an openshift Maven profile to provision a JBoss EAP server, deploy and run the quickstart in OpenShift environment.
The server provisioning functionality is provided by the EAP Maven Plugin, and you may find its configuration in the quickstart pom.xml:
<profile>
<id>openshift</id>
<build>
<plugins>
<plugin>
<groupId>org.jboss.eap.plugins</groupId>
<artifactId>eap-maven-plugin</artifactId>
<configuration>
...
<feature-packs>
<feature-pack>
<location>org.jboss.eap:wildfly-ee-galleon-pack</location>
</feature-pack>
<feature-pack>
<location>org.jboss.eap.cloud:eap-cloud-galleon-pack</location>
</feature-pack>
</feature-packs>
<layers>...</layers>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</profile>You may note that it uses the cloud feature pack which enables a configuration tuned for the OpenShift environment.
This section contains the basic instructions to build and deploy this quickstart to JBoss EAP for OpenShift or JBoss EAP for OpenShift Online using Helm Charts.
-
You must be logged in OpenShift and have an
occlient to connect to OpenShift -
Helm must be installed to deploy the backend on OpenShift.
Once you have installed Helm, you need to add the repository that provides Helm Charts for JBoss EAP.
$ helm repo add jboss-eap https://jbossas.github.io/eap-charts/
"jboss-eap" has been added to your repositories
$ helm search repo jboss-eap
NAME CHART VERSION APP VERSION DESCRIPTION
jboss-eap/eap81 ... ... A Helm chart to build and deploy EAP 8.1 applicationsLog in to your OpenShift instance using the oc login command.
The backend will be built and deployed on OpenShift with a Helm Chart for JBoss EAP.
Navigate to the root directory of this quickstart and run the following command:
$ helm install todo-backend charts --wait --timeout=10m0s
NAME: todo-backend
...
STATUS: deployed
REVISION: 1This command will return once the application has successfully deployed. In case of a timeout, you can check the status of the application with the following command in another terminal:
oc get deployment todo-backendThe Helm Chart for this quickstart contains all the information to build an image from the source code using S2I on Java 17:
apiVersion: v2
name: todo-backend-chart
description: A Helm chart to deploy a JBoss EAP todo-backend application and its Postgresql database
type: application
version: 1.0.0
appVersion: 8.1
dependencies:
- name: postgresql
repository: https://charts.bitnami.com/bitnami
version: 13.1.5
- name: eap81
repository: https://jbossas.github.io/eap-charts/
version: 1.0.1This will create a new deployment on OpenShift and deploy the application.
If you want to see all the configuration elements to customize your deployment you can use the following command:
$ helm show readme jboss-eap/eap81Get the URL of the route to the deployment.
$ oc get route todo-backend -o jsonpath="{.spec.host}"Access the application in your web browser using the displayed URL.
The integration tests included with this quickstart, which verify that the quickstart runs correctly, may also be run with the quickstart running on OpenShift.
|
Note
|
The integration tests expect a deployed application, so make sure you have deployed the quickstart on OpenShift before you begin. |
Run the integration tests using the following command to run the verify goal with the integration-testing profile activated and the proper URL:
$ mvn verify -Pintegration-testing -Dserver.host=https://$(oc get route todo-backend --template='{{ .spec.host }}') |
Note
|
The tests are using SSL to connect to the quickstart running on OpenShift. So you need the certificates to be trusted by the machine the tests are run from. |
$ helm uninstall todo-backendThe Helm Chart also contains the environment variables required to connect to the PostgreSQL database.
For OpenShift, we rely on secrets so that the credentials are never copied outside OpenShift:
deploy:
env:
- name: POSTGRESQL_PASSWORD
valueFrom:
secretKeyRef:
key: database-password
name: todo-backend-dbWhen the application is deployed, the value for the POSTGRESQL_PASSWORD will be taken from the key database-password
in the secret todo-backend-db.
Once the backend is deployed on OpenShift, it can be accessed from the route todo-backend.
Let’s find the host that we can use to connect to this backend:
$ oc get route todo-backend -o jsonpath="{.spec.host}"
todo-backend-jmesnil1-dev.apps.sandbox.x8i5.p1.openshiftapps.comThis value will be different for every installation of the backend.
To be able to connect to the backend from the ToDo Backend Specs or Client, then prepend the host with https://, and append the relative web context /todo-backend. For the previous example host this would be https://todo-backend-jmesnil1-dev.apps.sandbox.x8i5.p1.openshiftapps.com/todo-backend.
We can verify that this application is properly working as a ToDo Backend by running its specs on it.
Once all tests passed, we can use the todobackend client to have a Web application connected to the backend.
|
Note
|
todobackend.com is an external service used to showcase this quickstart. It might not always be functional but does not impact the availability of this backend. |
This quickstart shows how the datasource feature pack provided by JBoss EAP simplifies the deployment of a JBoss EAP Jakarta EE backend on OpenShift to connect to an external database and exposes an HTTP API.
The use of a Server Provisioned deployment makes it seamless to move from a local deployment for development to a deployment on cloud platforms such as OpenShift and Kubernetes.