A simple Kitura application that uses a PostgreSQL database and allows customers to search and buy new books.
You may be interested in reading the corresponding tutorial, Creating a Bookstore application: Interface to SQL, that steps you through the code.
- Requires PostgreSQL and PostgreSQL client library
On macOS: brew install postgresql
On Linux: sudo apt-get install libpq-dev
- Create database
createdb bookstoredb
- Load the schema
Load psql: psql -d bookstoredb
Load the schema: \i Database/schema.sql
- Replace
userName
inConfig.swift
For local development, make sure to use the username for your PostgreSQL server. To discover your own default username, do psql --help
. It will be listed under connection options.
- Compile:
swift build
- Run:
.build/debug/bookstore
- Test:
curl localhost:8080
Get cart data
select * from books, carts where books.book_id=carts.book_id and user_id=1;
Get books with authors
select * from books where books.author IS NOT NULL
Bluemix is a hosting platform from IBM that makes it easy to deploy your app to the cloud. Bluemix also provides various popular databases. Compose for PostgreSQL is an offering that is compatible with a PostgreSQL database, but provides additional features.
-
Get an account for Bluemix
-
Download and install the Cloud Foundry tools:
cf api https://api.ng.bluemix.net cf login
Be sure to run this in the directory where the manifest.yml file is located.
-
Create your Compose for PostgreSQL service
cf create-service compose-for-postgresql Standard Bookstore-postgresql
-
Run
cf push
*Note This step will take 3-5 minutes
1 of 1 instances running App started
-
Get the credential information:
cf env Bookstore
Note you will see something similar to the following, note the hostname, username, and password:
{ "VCAP_SERVICES": { "compose-for-postgresql": [{ "credentials": { "ca_certificate_base64": "<base64_string>", "db_type": "postgresql", "deployment_id": "584afe35475bb60013000023", "name": "bmix_dal_yp_77871416_ece6_4ebb_8aca_b1e02a39b7b1", "uri": "postgres://<user>:<password>@bluemix-sandbox-dal-9-portal.0.dblayer.com:19971/compose", "uri_cli": "psql \"sslmode=require host=bluemix-sandbox-dal-9-portal.0.dblayer.com port=19971 dbname=compose user=<user>\"" }, "label": "compose-for-postgresql", "name": "Bookstore-postgresql", "plan": "Standard", "provider": null, "syslog_drain_url": null, "tags": [ "big_data", "data_management", "ibm_created" ] }] } },
-
Setup your database
From the output above, extract the
url_cli
, strip out escaping\
characters and run a command like the following, replacing<user>
with your user:cat Database/schema.sql | psql "sslmode=require host=bluemix-sandbox-dal-9-portal.0.dblayer.com port=19971 dbname=compose user=<user>"
If asked for a password, use the value in the <password>
space from the cf env
output above.
For the following instructions, we will be using our Bash Script located in the root directory. You can attempt to complete the whole process with the following command:
./config.sh all <imageName>
Or, you can follow the step-by-step instructions below.
- Install the Cloud Foundry CLI tool and the IBM Containers plugin for CF with the following
./config.sh install-tools
- Ensure you are logged in with
./config.sh login
- Build and run a Docker container with the following
./config.sh build <imageName>
To test out created Docker image, use
./config.sh run <imageName>
./config.sh stop <imageName>
- Push created Docker container to Bluemix
./config.sh push-docker <imageName>
- Create a bridge CF application to later bind to your container
./config.sh create-bridge
- Create the Compose for PostgreSQL service and bind to your bridge CF application.
./config.sh create-db
- Create a Bluemix container group where your app will live, binding it to your bridge CF application in the process
./config.sh deploy <imageName>
Afterwards, you can ensure PostgreSQL was bound correctly by viewing all credentials for your group
cf ic group inspect <imageName>
- Lastly, we need to setup our database with some data
./config.sh populate-db
Once you run that command, you are done! Accessing your apps route with the path /api/v1/books
should return a list of books.