Hi, Denys van Kempen, SAP welcome back,
you are watching video number two of a short tutorial series where are going to take a
look at how we can develop secure applications in a multi-cloud environment.
In the previous video, we introduced the blue print here,
The different components and microservices.
In this video, we will cover the core piece: the business app.
Now, normally this would be a lot of code, doing all sort of things, but for this series
we are not all that interested in what the business logic does,
we care about all the other parts, the service instances, the app router, and in particular,
the components that take care of all the security for us, the XSUAA service.
As code samples, for the business app, I have brought some Java, JavaScript, and Python
with me.
Mostly JavaScript, or Node.js, to be precise.
This is fairly easy to read, even when you are not familiar with the language.
With Java, or Go, or .NET, we would first have to compile our code, and the syntax is
a bit harder to digest.
We do have put in one Java sample though, but basically only to show that from a Cloud
Foundry perspective it works exactly the same with Java as it does with Python or Node.
If you are already familiar with how to run Node and Python locally, how to build a Docker
container and how to see what’s inside you can skip video and move on to the next.
This is just a gentle introduction, To help you keep the JSONs and the YAMLs apart.
Which piece does what?
Things tend to get a bit complicated with microservices fairly quickly and if you not
in a hurry, you might want join me to see how this all works locally before we push
it to the cloud.
The business logic is about as simple as it gets.
On purpose, as mentioned, keeping it simple, as our focus will be on the infrastructure.
Our app has a dependency.
We require express.
For those not familiar with Node, express serves as a lightweight web application server.
We could have used the built-in http module with Node.
we could have used other frameworks, but express is pretty common, probably the one most used.
Then we define what happens when we access the home page, the slash: we send a message,
check for a port in the environment and otherwise use 3000 and ask the server to listen for
incoming request.
That’s it.
While we are at it, let’s take a look at the Python code sample as well.
Same thing, pretty much.
Instead of express, our dependency is on Flask, the rest is the same, in slightly different
syntax.
Again, the business logic is not the point here.
To run this code locally, we would need to install Node and Python first.
Then we could decide to install express and Flask, which our app has defined as dependencies.
However, this is not how this is done, of course.
Because express and Flask have dependencies as well.
And those dependencies have other dependencies and this all gets pretty complex, pretty soon.
So, we need a package manager, to take care of this.
For node we have npm, node package manager For python we have pip.
Node and Python installations usually come with the package manager installed but otherwise
you would have to install npm or pip first.
New node projects we would need to initialise first with the npm init command and what this
does is create an application metadata file called package.json.
JSON stands for JavaScript Object Notation and this is what this looks like.
curly brackets with key/value pairs between quotes, a colon, square brackets for arrays.
Defines the name of the application, version, maybe a description, but also the dependencies,
here we have express, and the command to start the application
Ok, run npm install express When look at the node modules, we can see
that actually about 50 packages were installed, and that’s why we are using a package manager,
of course.
Then to start the app we enter the start command node server.js
We could have put an extra line of code to log a message to console, saying the server
has started on port something but if we open a new terminal window and curl the URL, assuming
port 3000 we get an HTTP 200 message, meaning all is
well.
Powered by express text as html
with our message.
Fine How does this work with Python?
On macOS, python comes as part of the operating system; it is an old version,
now we don’t want to fiddle with the core operating system and install packages.
What we want is a virtual environment, a container of some sort, as this is exactly what we will
be doing in the next video when we deploy our business logic to a cloud platform
Here’s the command to create virtual environment and what this does is create a local runtime,
with pip included So now we can use a more recent version
pip install Flask again dependencies are add
not as many as for node, but the concept is exactly the same
There is no package.json for Python (JSON JavaScript)
Instead, Python uses a requirements.text file and to install the dependencies we could also
have used the command pip install -r requirements.txt
The file name is a convention, any name will do.
The start command is the same python server.py Flask does some console logging automatically
We now use port 5000 and we get our HTTP 200 and message
same result in a browser, of course So we have our business app up and running
locally.
We talked about the application metadata file for Node.js, package.json
we talked about the package manager, npm and pip, dependencies and the requirements file
for Python.
A start command, a that’s pretty much it.
Now you may have heard about Docker containers, this enables us to bundle our app with only
the runtime.
Here we have a Dockerfile Instructs Docker to get a node image
defines a working directory copies package.json
runs npm install to install the dependencies then copies over the code
opens a port and runs the start command to build a docker container, we run the docker
build command give a name or a tag rather, -t
use current directory, the dot, as source.
Then to run the docker container, we use the docker run command, mapping port 8080, curl
the URL and we have our HTTP 200 OK, powered by express, with our message
Now using dive, we can actually look inside the container.
Here we have the FROM instruction that got us a node image: 100 megs
couple of apt-get commands, that’s a Linux package manager to install some additional
packages.
groupadd: setting up the user long command, but here we are getting the
latest node version add yarn, yet-another package manager, a more
recent version of npm here we are creating the work directory
copy package.json run npm install to add the modules, just like
we did a minute ago and finally copy our business app
That’s what’s inside container, there is no magic.
In the next video, we are going to see how we can do this with Cloud Foundry.
I am Denys van Kempen, SAP, Thanks for watching.