Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
dvankempen
Product and Topic Expert
Product and Topic Expert
2,764






This blog post series is about developing applications in a multi-cloud environment.

For the end-to-end hands-on tutorial example, see

For the step-by-step series, visit

  1. Cloud Foundry, UAA, and XSUAA

  2. Business Logic App  <<<  this post

  3. Service Instance (SAP HANA Cloud)

  4. Application Router

  5. Authentication using XSUAA

  6. Authorization using XSUAA

  7. Multi-target applications (MTA)

  8. Application Runtime (Appendix)


Questions? Post as comment.

Useful? Give us a like and share on social media.

Thanks!



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)


Business Logic | Local Development


In this second video of the series, we explore the "business logic app". This will be a simple Hello World app, using Node.js and Python, covering local development, dependencies, and start commands. We also show how we can bundle our app inside a Docker container.

https://youtu.be/APbvu4MjB0M

Business Logic | Cloud Deployment


In this third video of the series, we explore our options for deploying our "business logic app" to the cloud and specifically the Cloud Foundry environment. We look at deployment attributes, the manifest.yml attribute deployment file, differences between deploying different development languages (Node, Python, Java) and containers (Diego, Docker).

https://youtu.be/P9G0pgfsgb0

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


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


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


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



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




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



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.



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


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


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



Please Proceed


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


Share and Connect


Questions? Please post as comment.

Useful? Give us a like and share on social media.

Thanks!

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

For the author page of SAP PRESS, visit








Over the years, for the SAP HANA Academy, SAP’s Partner Innovation Lab, and à titre personnel, I have written a little over 300 posts here for the SAP Community. Some articles only reached a few readers. Others attracted quite a few more.

For your reading pleasure and convenience, here is a curated list of posts which somehow managed to pass the 10k-view mile stone and, as sign of current interest, still tickle the counters each month.