Run on App Platform
It is easy to run DB2Rest on DigitalOcean App Platform using the Docker image. It is also possible to run DB2Rest on droplets or virtual machines(VM).
DigitalOcean managed database
DigitalOcean provides managed database service for :
- PostgreSQL
- MySQL
- MongoDB
- Redis
DB2Rest currently supports PostgreSQL
and MySQL
.
Create database
Follow the tutorials below to launch either PostgreSQL
or MySQL
database service.
The example below shows the dashboard of a managed PostgreSQL database created on DigitalOcean cloud.
Create Table
Next, create a table using any database tool. Our favorite tool is Dbeaver.
The connection details are available on the database dashboard.
CREATE TABLE employee (
id serial4 NOT NULL,
first_name varchar(50) NOT NULL,
last_name varchar(50) NOT NULL,
email varchar(255) NOT NULL,
created_on timestamp NOT NULL,
CONSTRAINT employee_email_key UNIQUE (email),
CONSTRAINT employee_pkey PRIMARY KEY (id)
);
Create app service
DB2Rest can now be deployed as an app service on Digital Ocean's App Platform and connect to your database created in the previous step. In order to get started, click on the buttons as shown in the image below:
This will open the Create App
form.
In this form set the values as listed below.
- Service Provider - Select
Docker Hub
- Repository - kdhrubo/db2rest
Click on the Next
button to load the Resources
form.
Now click on Edit Plan
button to set the number of Containers
to 1 to reduce cost.
Click on Back
to go back to the Resources
form. Then click Next
.
In the Environment
screen, set the values for the following environment variables:
- DB_URL
- DB_USER
- DB_PASSWORD
Click on Save
to save the environment variables. Then click on the Next
button twice to reach the Review
page.
On the Review
page, click on Create Resources
to provision DB2Rest as an app service.
The values needed for the environment are available in the database dashboard.
Test DB2Rest App Service
Once the deployment is successfully completed, it is time to test the application service.
Check DB2Rest Health
It is very easy to verify DB2Rest application service with the actuator endpoint as shown below:
- cURL
- HTTPie
curl --request GET \
--url 'https://seashell-app-47ctm.ondigitalocean.app/actuator/health?=' \
--header 'User-Agent: insomnia/8.6.1'
http GET 'https://seashell-app-47ctm.ondigitalocean.app/actuator/health?=' \
User-Agent:insomnia/8.6.1
The actuator health check service in DB2Rest will return the following response:
HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
{
"status": "UP",
"groups": [
"liveness",
"readiness"
]
}
The status
attribute shows that the application service is ready to handle database operations.
Insert Row
- cURL
- HTTPie
curl --request POST \
--url https://seashell-app-47ctm.ondigitalocean.app/employee \
--header 'Content-Type: application/json' \
--header 'User-Agent: insomnia/8.6.1' \
--data '{
"first_name" : "Salman",
"last_name" : "Khan",
"email" : "[email protected]",
"created_on" : "2015-04-14T11:07:36.639Z"
}'
echo '{
"first_name" : "Salman",
"last_name" : "Khan",
"email" : "[email protected]",
"created_on" : "2015-04-14T11:07:36.639Z"
}' | \
http POST https://seashell-app-47ctm.ondigitalocean.app/employee \
Content-Type:application/json \
User-Agent:insomnia/8.6.1
HTTP Response
{
"row": 1,
"keys": {
"id": 3
}
}
Read Row
- cURL
- HTTPie
curl --request GET \
--url https://seashell-app-47ctm.ondigitalocean.app/employee \
--header 'User-Agent: insomnia/8.6.1'
http GET https://seashell-app-47ctm.ondigitalocean.app/employee \
User-Agent:insomnia/8.6.1
HTTP Response
[
{
"id": 1,
"first_name": "Salman",
"last_name": "Khan",
"email": "[email protected]",
"created_on": "2015-04-14T11:07:36.639+00:00"
}
]
Secure the deployment
In this deployment, the app service and database are not in the same Virtual Private Cloud or VPC. It is not secure connectivity even though SSL is turned on.
It is imperative to restrict access to the database from external sources. The database dashboard already shows this warning.
Click on the link Secure this database cluster by restricting access
to go the settings page (also available in the Settings
tab)
to set the Trusted sources
. Click on Edit
and select the app service as the only service to connect to the database.
Once the DB2Rest service (named seashell-app
by DigitalOcean) is selected, click Save
.
Now the database instance is safe and will only allow connection requests from configured trusted sources.
Finally, refer to the DB2Rest documentation for further learning and exploring API features.
For help, visit us on Discord or our GitHub Discussions