clouddatabases-redis-helloworld-python is a sample IBM Cloud application which shows you how to connect to an IBM Cloud Databases for Redis service to a IBM Cloud Foundry application written in Python 3.6.4.
-
If you do not already have an IBM Cloud account, sign up here
-
Download and install IBM Cloud CLI.
The IBM Cloud CLI tool tool is what you'll use to communicate with IBM Cloud from your terminal or command line.
-
Connect to IBM Cloud in the command line tool and follow the prompts to log in.
ibmcloud login
Note: If you have a federated user ID, use the
ibmcloud login --sso
command to log in with your single sign on ID. -
Create your database service.
The database can be created from the command line using the
ibmcloud resource service-instance-create
command. This takes a service instance name, a service name, plan name and location. For example, if we wished to create a database service named "example-redis" and we wanted it to be a "databases-for-redis" deployment on the standard plan running in the us-south region, the command would look like this:ibmcloud resource service-instance-create example-redis databases-for-redis standard us-south
Remember the database service instance name.
-
Make sure you are targeting the correct IBM Cloud Cloud Foundry org and space.
ibmcloud target --cf
Choose from the options provided.
-
Create a Cloud Foundry alias for the database service.
ibmcloud resource service-alias-create alias-name --instance-name instance-name
The alias name can be the same as the database service instance name. So, for our database created in step 4, we could do:
ibmcloud resource service-alias-create example-redis --instance-name example-redis
-
Clone the app to your local environment from your terminal using the following command:
git clone https://github.com/IBM-Cloud/clouddatabases-redis-helloworld-python.git
-
cd
into this newly created directory. The code for connecting to the service, and reading from and updating the database can be found inserver.py
. See Code Structure and the code comments for information on the app's functions. There's alsotemplate
andstatic
directories, which contain the html, style sheets and javascript for the web app. For now, the only file you need to update is the application manifest. -
Make sure that you store a local copy of the databases's self-signed certificate. You'll need to go into the file
server.py
and add the location of your self-signed certificate tossl_ca_certs
when connecting to the database. Install and use the IBM Cloud Databases plugin with the IBM Cloud CLI tool to get your self-signed certificate with the following command:
ibmcloud cdb cacert <database deployment name>
- Update the
manifest.yml
file.
- Change the
name
value. The value you choose will be the name of the app as it appears in your IBM Cloud dashboard. - Change the
route
value to something unique. This will make be the base URL of your application. It should end with.mybluemix.net
. For exampleexample-helloworld-python.mybluemix.net
.
Update the service
value in manifest.yml
to match the name of your database service instance name.
- Push the app to IBM Cloud. When you push the app it will automatically be bound to the service.
ibmcloud cf push
Your application is now running at host you entered as the value for the route
in manifest.yml
.
The python-redis-helloworld app displays the contents of an examples database. To demonstrate that the app is connected to your service, add some words to the database. The words are displayed as you add them, with the most recently added words displayed first.
File | Description |
---|---|
server.py | Establishes a connection to the Redis database using credentials from VCAP_ENV and handles create and read operations on the database. |
main.js | Handles user input for a PUT command and parses the results of a GET command to output the contents of the Redis database. |
The app uses a PUT and a GET operation:
-
PUT
- takes user input from main.js
- uses the
client.query
method to add the user input to a hashed set in the Redis keyspace called words.
-
GET
- uses
client.query
method to retrieve the contents of the hashed set called words in the Redis keyspace. - returns the response of the database command to main.js
- uses