Skip to content

IBM-Swift-Sunset/Bookstore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

86 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚫 This project is no longer maintained.

Bookstore

A simple Kitura application that uses a PostgreSQL database and allows customers to search and buy new books.

Build Status

Getting started

You may be interested in reading the corresponding tutorial, Creating a Bookstore application: Interface to SQL, that steps you through the code.

  1. Requires PostgreSQL and PostgreSQL client library

On macOS: brew install postgresql On Linux: sudo apt-get install libpq-dev

  1. Create database

createdb bookstoredb

  1. Load the schema

Load psql: psql -d bookstoredb

Load the schema: \i Database/schema.sql

  1. Replace userName in Config.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.

  1. Compile:

swift build

  1. Run:

.build/debug/bookstore

  1. Test:

curl localhost:8080

SQL queries

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

Deploying to Bluemix

Manually

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.

  1. Get an account for Bluemix

  2. 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.

  3. Create your Compose for PostgreSQL service

cf create-service compose-for-postgresql Standard Bookstore-postgresql
  1. Run cf push

    *Note This step will take 3-5 minutes

    1 of 1 instances running 
    
    App started
    
  2. 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"
     			]
     		}]
     	}
     },
  3. 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.

Deploying Docker to IBM Bluemix Container

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.

  1. Install the Cloud Foundry CLI tool and the IBM Containers plugin for CF with the following
./config.sh install-tools
  1. Ensure you are logged in with
./config.sh login
  1. 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>
  1. Push created Docker container to Bluemix
./config.sh push-docker <imageName>
  1. Create a bridge CF application to later bind to your container
./config.sh create-bridge
  1. Create the Compose for PostgreSQL service and bind to your bridge CF application.
./config.sh create-db
  1. 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>
  1. 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.

About

🚫 This project is no longer maintained. Example of using PostgreSQL and Kitura

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •