/workout-app-helidon

Helidon backend for random workout generator app

Primary LanguageTSQL

Random Workout Generator

Helidon MP project that includes REST operations to obtain exercises from a MySQL database.

React frontend repository and live site.

Build and run

docker-compose up

Exercise the application

curl -X GET http://localhost:8080/workout
{
    "name":"Generated Workout",
    "exercises":["Pullups","Flutter Kicks","Lunge","Plank knees to elbows","Burpees","Single Arm Swings","Reverse Crunch","Pushups","Plank","Wide Pushups"]
}

curl -X GET http://localhost:8080/workout?type=arms,back&limit=3
{
    "name":"Generated Workout",
    "exercises":["Superman Hold","Curls","Rows"]
}

Deploying to Heroku

For more information see the Heroku Dev Center Documentation

In the Dockerfile, change the CMD to

CMD java -Dserver.port=$PORT -jar helidon-quickstart-mp.jar

Log in to Container Registry:

heroku container:login

Create a Heroku app:

heroku create

Note that commands after this may need suffixed with --app where is the Heroku provided name.

Build the image:

docker build -t helidon-quickstart-mp .

Push to Container Registry:

heroku container:push web

Release image to your app:

heroku container:release web

Open the app in your browser

heroku open

Note that there are command line tools to provision the MySQL add-on.

Provision ClearDB MySQL add-on from the Heroku dashboard: Screenshot of Heroku Dashboard showing ClearDB MySQL being provisioned to a personal app

Retrieve database URL:

heroku config | grep CLEARDB_DATABASE_URL

Copy this URL to microprofile-config.properties filling in the generated username, password, and database name:

javax.sql.DataSource.example.dataSourceClassName=com.mysql.cj.jdbc.MysqlDataSource
javax.sql.DataSource.example.dataSource.url=jdbc:mysql://<username>:<password>@us-cdbr-east-02.cleardb.com/<heroku_db>?reconnect=true
javax.sql.DataSource.example.dataSource.user=<username>
javax.sql.DataSource.example.dataSource.password=<password>

Note that if you have another frontend you can add it like so:

cors.paths.0.allow-origins = https://random-workout-generator.netlify.app

To populate the database you could use a tool like MySQL Workbench.

You can connect using the same credentials from microprofile-config.properties.

You can use the init_script.sql to create the schema and populate the tables.

To query the database all references to workout.exercises in WorkoutResource.java should be changed to <heroku_db>.exercises

Finally, to deploy to Heroku, you can use the same commands listed above.

How the initial SQL Dump was created

Connect to MySQL

docker container run --rm -d -p 3306:3306 \
    --env MYSQL_ROOT_PASSWORD=password \
    --name mysql \
    mysql:8

Run MySQL commands inside container

docker exec -it mysql bash
mysql -p

Paste initialization script

init_script.sql

Create SQL dump

mysqldump -p --databases workout > dump.sql

Move dump out of container

cat dump.sql

Copy and paste into /docker/data/dump.sql