Skip to content

Demo for the Call for Code Code Engine hackathon. This example provides two REST endpoints for HTTP POST and GET methods that are mapped to corresponding post-cat and get-cat IBM Code Engine applications.

License

Notifications You must be signed in to change notification settings

Call-for-Code/Code-Engine-Scalable-APIs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Code Engine Scalable APIs

Demo for the Call for Code Code Engine hackathon. This example provides two REST endpoints for HTTP POST and GET methods that are mapped to corresponding post-cat and get-cat IBM Code Engine applications representing a RESTful API for a web or mobile backend that implements the architecture below.

By packaging each method independently, each method can scale on its own. For example, an API is likely to receive many more requests to its GET method than its DELETE method. This sample shows GET and POST methods supported by a simple Node.js HTTP server that uses a Cloudant package to connect to a bound database.

Scalable APIs

Benefits of scalable APIs

Scalable APIs

Benefits of scalable methods

Scalable APIs

Getting started

Note: The Code Engine command line has shorthand for commands and parameter flags. This tutorial spells them out using the longer form for better readability. For example application vs app and --name vs -n.

  1. Set up Code Engine environment
  2. Create Code Engine apps
  3. Invoke the REST endpoints
  4. View logs
  5. Clean up
  6. Learn more

1. Set up Code Engine environment

Install or update your command line tools

Note: We won't use the IBM Cloud Shell service for this demo, as it doesn't include the Docker tool needed to build our images.

Download and install the following tools and plugins:

If you installed the Code Engine plugin before, please make you have the latest versions:

ibmcloud plugin update --all --force

Clone this repository

This repository has the source code for the applications we're going to deploy.

git clone https://github.com/Call-for-Code/Code-Engine-Scalable-APIs
cd Code-Engine-Scalable-APIs

Set up an IBM Cloud resource group and project

# Log in with your standard account, can't be a Lite account
ibmcloud login
export DOCKERHUB_USERNAME=[YOUR DOCKERHUB USERNAME]
export DOCKERHUB_PASSWORD=[YOUR DOCKERHUB PASSWORD]

# Create a resource group for your projects, or reuse an existing one
ibmcloud resource group-create call-for-code
ibmcloud target -g call-for-code

# Create a project for your applications, and a registry entry for the place to store images
ibmcloud ce project create --name scalable-apis
ibmcloud ce registry create \
         --name dockerhub \
         --server https://index.docker.io/v1/ \
         --username $DOCKERHUB_USERNAME \
         --password $DOCKERHUB_PASSWORD
ibmcloud ce project select --name scalable-apis

Create a backing data store with Cloudant

# Create an instance of Cloudant, if you don't have one
# Set the region to something other than `us-south` if needed
ibmcloud resource service-instance-create cats-database \
         cloudantnosqldb lite us-south \
         --parameters '{"legacyCredentials": false}'

# Confirm that the project and service are up
ibmcloud resource service-instances

2. Create Code Engine apps

Create an action to create a cat entity

# Change to the post-cat directory
cd post-cat

# Build the image. Make sure the Docker daemon is running.
docker build --no-cache -t $DOCKERHUB_USERNAME/post-cat .

# And push it
docker push $DOCKERHUB_USERNAME/post-cat

# Create the app
ibmcloud ce application create --name post-cat --image $DOCKERHUB_USERNAME/post-cat

# Get app details, if needed
ibmcloud ce application get --name post-cat

# Bind the Cloudant service credentials to the app
ibmcloud ce application bind --name post-cat --service-instance cats-database

# Get the URL of the app for later use
POST_URL=$(ibmcloud ce application get --name post-cat --output url)

Create an action to retrieve a cat entity

# Change to the get-cat directory
cd ../get-cat

# Build the image
docker build --no-cache -t $DOCKERHUB_USERNAME/get-cat .

# And push it
docker push $DOCKERHUB_USERNAME/get-cat

# Create the app
ibmcloud ce application create --name get-cat --image $DOCKERHUB_USERNAME/get-cat

# Get app details, if needed
ibmcloud ce application get --name get-cat

# Bind the Cloudant service credentials to the app
ibmcloud ce application bind --name get-cat --service-instance cats-database

# Get the URL of the app for later use
GET_URL=$(ibmcloud ce application get --name get-cat --output url)

3. Invoke the REST endpoints

Using curl we can now send HTTP requests to the REST APIs.

# POST /v1/cat {"name": "Tahoma", "color": "Tabby"}
curl -X POST -H 'Content-Type: application/json' \
             -d '{"name": "Tahoma", "color": "Tabby"}' $POST_URL

# GET /v1/cat?id=1
curl $GET_URL

4. View logs

You can now use a few other tools to view your applications that represent your independent REST endpoints.

You have two options:

For a quick look at logs and events, use this command:

ibmcloud ce application logs --name post-cat --tail 10
ibmcloud ce application logs --name get-cat --tail 10

To dig into various parts of the application, including the logs:

Logging

5. Clean up

Remove the applications

# Delete apps
ibmcloud ce application delete --name get-cat --force
ibmcloud ce application delete --name post-cat --force
ibmcloud ce project delete --name scalable-apis --force --hard

# Delete database
ibmcloud resource service-instance-delete cats-database --force --recursive

6. Learn more

Check out this repo of additional sample applications to explore other use cases.

License

Apache 2.0

About

Demo for the Call for Code Code Engine hackathon. This example provides two REST endpoints for HTTP POST and GET methods that are mapped to corresponding post-cat and get-cat IBM Code Engine applications.

Topics

Resources

License

Stars

Watchers

Forks