Skip to Content
Technical Articles

Business Logic App | SAP Cloud Platform – Cloud Foundry

This blog post series is about developing applications in a multicloud environment.

  1. Cloud Foundry, UAA, and XSUAA
  2. Business Logic App
  3. Service Instance (SAP HANA Cloud)
  4. Application Router
  5. Authentication using XSUAA
  6. Authorization using XSUAA
  7. Application Runtime (appendix)

For the blog post about the SAP Cloud Platform multi-cloud environment, see

Questions? Post as comment.

Useful? Give a like and share on social media. Thanks!

/wp-content/uploads/2016/02/sapnwabline_885687.png

Hands-On Tutorial

Developing Secure Applications on the SAP Cloud Platform

In this blog series, we explore developing secure applications in a multi-cloud Cloud Foundry environment. In this blog, we get hands on with app development starting with the core component: the Business Logic App with code sample for Node.js, Python, and Java.

The business logic of these apps is not very sophisticated and we do so deliberately to keep the focus on service instances and security.

The objective is to understand how working with other Cloud Foundry service instances impacts

  • Business logic as defined in our code: server.js, server.py
  • Configuration: package.json, requirements.txt
  • Deployment: application manifest (e.g. manifest.yml)

Sample Code

You can download the sample code from repository

Appendix

For more detailed information about the SAP Cloud Platform trial environment, Cloud Foundry buildpacks, dependencies declarations, attributes, Diego cells and more, see the “appendix” blog

/wp-content/uploads/2016/02/sapnwabline_885687.png

Build an App – Node.js 

Bellow a sample implementation using JavaScript / Node.js for the business logic as documented for the SAP Cloud Platform

Running the App Locally

Generate the application package file expected by Cloud Foundry using the command npm init and install the express module.

mkdir myapp && cd myapp
npm init -y
npm install express

 

The npm init command generates a application manifest file in JSON format, package.json.

With this file we specify the app name, version, description, and some other optional attributes.

In addition, the package.json file also includes the version of the Node engine we wish to use, application dependencies, and the start command.

As we will see, this type of configuration information is different for each runtime.

{
  "name": "myapp",
  "version": "0.0.1",
  "description": "My App",
  "keywords": [
    "tutorial"
  ],
  "author": "sap",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  },
  "engines": {
    "node": "10.x.x"
  },
  "scripts": {
    "start": "node server.js"
  }
}

Business Logic (server.js)

Below the business logic of our app which will return a text message when a URL is called.

// app server
const express = require('express');
const app = express();

//  home page
app.get('/', function (req, res) {
  res.send('Hello World!');
});

// start app server
app.listen(process.env.PORT || 3000, ()=>{})

As there are no service dependencies at this stage, we can run our business app locally.

For a step-by-step explanation of how to build and deploy Node.js apps to the SAP Cloud Platform Cloud Foundry environment, see

For more information about Node Express, see

/wp-content/uploads/2016/02/sapnwabline_885687.png

Build an App – Python

Bellow a sample implementation using Python for the business logic as documented for the SAP Cloud Platform.

Running the App Locally

For Node we used npm as package manager and express as web server. For Python we use pip and Flask; same thing.

To run the app locally, we can use a virtual environment (optional). Upgrade the Python package manager pip to the latest version (best practice) and install Flask.

## activate virtual environment
# python3 -m venv local  
# source local/bin/activate
pip install --upgrade pip
pip install Flask
python server.py
# deactivate

 

Business Logic (server.py)

Below the business logic of our app which will return a text message when a URL is called.

Flask and express work very similarly. Flask uses Werkzeug for console logging so we are getting some output. For Node, we could have added a line with console.log.

# app server
from flask import Flask
app = Flask(__name__)

# home page
@app.route('/')
def hello():
    return "Hello World"

# start app server 
if __name__ == '__main__':
    app.run()

As there are no service dependencies at this stage, we can run our business app locally.

For more information about Flask and a quick start, see

This also documents how to use a virtual environment for local development with platform installation instructions for Linux/macOS and Windows.

For the ins and outs about the Python Package manager pip, see

/wp-content/uploads/2016/02/sapnwabline_885687.png

Deploy an App: Node.js

Push MyApp

To deploy the app to the SAP Cloud Platform Cloud Foundry environment, set the API endpoint, login, and deploy the app.

For more information about how to connect to the SAP Cloud Platform trial environment, see

cf api https://api.cf.eu10.hana.ondemand.com
cf l[ogin]
cf push myapp

 

Whether the deployment succeeds or not depends on whether “myapp” is the only one in the domain cf.eu10.hana.ondemand.com as a unique route (URL) needs to be created and mapped.

To solve this we can provide the random route attribute.

cf push myapp --random-route

Random route is only one of the attributes we can use to deploy an app. There are many others. However, specifying deployment attributes as parameters on the command line is error prone and not suitable for automation; infrastructure-as-code (IaC). 

Application Manifest (manifest.yml)

For this reason, Cloud Foundry proposes an application manifest. This is similar to the Node manifest (package.json) except that it uses YAML and not JSON. It is also specific to Cloud Foundry (the runtime) and not the development language. 

The application manifest below specifies name, route, and path to the app. For more information about application attributes and the manifest file, see

---
applications:
- name: myapp
  path: myapp
  random-route: true

SAP Cloud Platform uses the standard Node.js buildpack provided by Cloud Foundry to deploy Node.js (and JavaScript) applications. This buildpack is automatically used when the package.json file is detected.

For more information about buildpacks, application routes, and other application attributes, see

Use the -f parameter to specify the name and path to the manifest file. This is optional in case the file is named manifest.yml and located in the current directory.

The samples below use the same app name, so delete before proceeding (including the random route with -r).

cf push [-f manifest.yml]
cf a[pps]
cf d[elete] myapp -r -f

/wp-content/uploads/2016/02/sapnwabline_885687.png

Deploy an App: Python

Application Manifest (manifest.yml)

The application manifest for our Python app is almost identical to the one for Node.js. The once exception is the start command. With Node, this command was specified using the application descriptor file (package.json).

---
applications:
- name: myapp
  path: myapp
  random-route: true

To define application dependencies for Python we use a plain text file called requirements.txt. When this file is detected Cloud Foundry will automatically select the Python buildpack to build the application.

Like the node modules installed with npm, we can “vendor” the dependencies for Python and provide them with the upload. This way during staging (app build) we do not need to download the files, which is requirement for some regions. Using requirements.txt and runtime.txt we can specify the versions we want to use, similar to package.json for Node.js. Below we are using a Python version different from the current buildpack default.

The Python buildpack does not provide a default start command. We can add this to the deployment manifest file or use a Procfile. For more information about Procfile, runtime.txt and vendor dependencies, see

/wp-content/uploads/2016/02/sapnwabline_885687.png

Build an App – Java

How to develop Java applications in the Cloud Foundry environment is documented in SAP Cloud Platform guide but the Java section does not include any sample code.

For the sample code below, we used the material from a Spring tutorial.

You may recall from an earlier blog that Cloud Foundry actually originates from Spring. If not, see

Business Logic (application.java)

As with the JavaScript and Python samples, the business logic of this app is not very sophisticated and similarly to Node.js express and Python Flask uses a framework (Spring) to run a web application.

Of course, unlike JavaScript or Python, we need to build our source code first and this requires us not only to install the runtime (Java JRE) but also a compiler (e.g. Maven or Gradle, see the tutorial for the instructions).

mvnw package 

Running the App Locally

To run the app, use the java command.

java -jar target/gs-spring-boot-docker-0.1.0.jar

/wp-content/uploads/2016/02/sapnwabline_885687.png

Deploy an App: Java

Application Manifest (manifest.yml)

The application manifest for our Java app, again, is very similar to the one for Node.js and Python. Instead of a local directory, we point to the compiled JAR file.

---
applications:
- name: myapp
  path: target/gs-spring-boot-docker-0.1.0.jar
  random-route: true

This time the JAR file extension triggers Cloud Foundry to use a Java buildpack (similarly to package.json and requirements.txt).

A JRE is downloaded plus some other dependencies and a start command is constructed.

/wp-content/uploads/2016/02/sapnwabline_885687.png

Build an App for Docker – Java

Running the App Locally

Alternatively, we could also have decide to run the app inside a docker container.

Using Dockerfile:

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Running commands:

docker build -t dvankempen/spring .
docker run -p 8080:8080 dvankempen/spring

We can use this docker build subsequently to deploy our app on Cloud Foundry. Below the commands to push the image to Docker Hub. This is not a requirement: Azure, AWS, GCP could also be used.

docker login --username=dvkempen 
docker images
docker tag <image-id>f dvankempen/spring:cloudfoundry  
docker push dvankempen/spring:cloudfoundry     

/wp-content/uploads/2016/02/sapnwabline_885687.png

Build an App for Docker – Node.js

Running the App Locally

For Node.js, an example Dockerfile would be

FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]

As documented

/wp-content/uploads/2016/02/sapnwabline_885687.png

Deploy an App for Docker – Java

Application Manifest (manifest.yml)

The application manifest for a docker container only requires two docker attributes. The docker repository password comes from an environment variable.

---
applications:
- name: myapp
  random-route: true
  docker:
    image: dvankempen/spring:cloudfoundry
    username: dvkempen

Of course, when using Docker, the manifest would be the same regardless of the runtime used. Note that the staging no longer includes any downloads and building.

For more information about how to work with Docker files, see

/wp-content/uploads/2016/02/sapnwabline_885687.png

Please Proceed

In the next blog we start to connect our business application with Cloud Foundry service instances.

/wp-content/uploads/2016/02/sapnwabline_885687.png

Share and Connect

Questions? Post as comment.

Useful? Give a like and share on social media. Thanks!

If you would like to receive updates, connect with me on

Best,

Denys van Kempen

/wp-content/uploads/2016/02/sapnwabline_885687.png

Be the first to leave a comment
You must be Logged on to comment or reply to a post.