This is a distributed ToDo-app implementation based on several Spin Apps:
- HTTP API: Exposing API Endpoints for creating, listing and toggling tasks
- Migrations: A Job to initialize database, seed data and perform migrations
- Stats Generator: A CronJob for generating simple stats using all tasks in the database
The HTTP API exposes the following endpoints:
GET /tasks
: Retrieve a list of all tasksPOST /tasks
: Create a new taskGET /tasks/:id
: Retrieve a single task using its identifierPOST /tasks/toggle/:id
: Toggle the state of a particular task (open|done) using its identifierGET /stats
: Retrieve all stats
docker
CLI must be installed (local DB is hosted in a container)spin
CLI must be installed- The Spin CLI
trigger-command
plugin (canary) must be installed - The ports
8080
and3000
may not be allocated on your system- You can use different ports by updating
./run-local.sh
and./local.toml
- You can use different ports by updating
You can find two scripts in this folder (next to README.md
) called run-local.sh
and run-cron-local.sh
.
The database is served locally using a docker container. To create the database, seed sample data and start the API execute the following:
./run-local.sh
To run the cron job on your local machine, run the following command:
./run-cron-local.sh
All apps read and write from the same SQLite database (which will be created in this folder upon starting the app on your local machine for the first time) -> ./sqlite_db.db
. All Spin Apps leverage the same runtime configuration file local.toml
to do so.
For each execution of the cron job, new stats are written to the Stats
table and available at GET localhost:3000/stats
.
curl -iX GET localhost:3000/stats
HTTP/1.1 200 OK
content-type: application/json
transfer-encoding: chunked
date: Tue, 29 Oct 2024 10:12:36 GMT
[
{
"date": "2024-10-29 10:12:33",
"open_tasks": 11,
"done_tasks": 14
}
]
- You must have access to a Kubernetes Cluster
kubectl
must be installed and must point to your Kubernetes cluster- SpinKube must be deployed to your Kubernetes cluster
Before deploying the different Spin Apps to your Kubernetes cluster, you must package and distribute the Spin Apps as OCI artifacts. To do so, you can use the distribute-apps.sh
script as shown here:
./distribute.sh
NOTE: The OCI artifacts are stored in
ttl.sh
an anonymous and ephemeral OCI compliant registry. According to the tags specified indistribute-apps.sh
and in Kubernetes deployment manifests, those artifacts remain available for 24 hours
- Deploy the Database to Kubernetes
kubectl apply -f ./kubernetes/db.yaml
- Wait for the DB to be ready
kubectl wait --for=condition=Ready pod -l app=postgres
- Deploy the Migrations Job to Kubernetes
kubectl apply -f ./kubernetes/migrations.yaml
- Deploy the HTTP API to Kubernetes
kubectl apply -f ./kubernetes/api.yaml
- Deploy the CronJob to Kubernetes
kubectl apply -f ./kubernetes/stats-generator.yaml