let's walk through a realistic micro service step by step so what we're going to do
in looking at this project is see that there's an application file for a flask
application there's a test file an installation requirements.txt and a makefile so to run this
and get this set up i'm going to scroll up to the feature called codespaces in github and i'm going
to build a new codespace so this is very similar to let's say visual studio code or aws cloud 9
or google cloud shell or azure cloud shell it's a place where i can execute code in a
docker container so i'm going to select new code space and go here and look for that flask change
microservice and go ahead and create a new repository so again this can work on any cloud
platform or even your local laptop just for this particular demo i'll be using github code spaces
okay so now that this code space is set up what i'm going to do is look through each of the
parts of the application first up here i've got a application right here and inside this application
you can see that it does a very specific task so i have from flask import flask i have jsonify
and it has a change piece of code that's pretty straightforward to look at it all it does is take
an amount in and gives us the proper change and so in fact i can even test this out and it returns
the result here by using my terminal here to test interactively the code and notice that it created
a virtual environment for me so i'm going to go ahead and source that i'll say source
python 3 virtual environment activate great okay so that's been installed for me and so what i'm
going to do is say make install and notice that this make install command all it does is look in
my requirements file and installs these packages so let's go ahead and do that make install
great okay those are all set up so the next thing that i'm going to do
is interactively query this thing so i'm going to install pip install of ipython
and this will allow me to basically test out this function first to make sure i understand
what it does so i'll type in ipython and going from app import change and you'll be able to see
what this does uh if i put in 1.34 cents it just gives me back the correct change if i put in some
other amount we'll say two dollars and 56 cents you can see it gives me back a different amount
right so it's a high level api that all it does just like a normal micro service is it does one
thing make change and then it returns back this value so let's test out how this service works
if i scroll down here you can see that i create two routes the first route is just a hello world
route and the way we do this is we say app.route and have a slash and then hello here and notice
that it says i can make change of this route and then app.route change dollar cents this takes two
parameters a dollar parameter and a cent parameter and it just passes that into that function
that i tested earlier right and it converts it to a float and then it returns back my result
so i can test this out locally here so how do i do this well i can just say python app.py
and this will run this application in foreground mode on port 8080. notice that this in particular
development environment allows me to test it out so i'm going to go ahead and say open browser
and it's going to forward this to this location there we go we can see that it says change so now
if i want to test it out i'll say change and i'll put in the same thing 1 and then the parameter 34
and we can see that it in fact made the correct change and returned it back as a json
payload right so it's a fully functioning api now if i want to try a new value
let's say five dollars and uh 89 cents it gives back a different result so in a nutshell you can
easily test out microservices by you know this is only let's say less than 50 lines of code here
by using both ipython to test out the pieces of code and then also running it locally now
one thing i'll also show you as i build a small test here and this small test here just ensures
that the core logic of my code works so i can go ahead and run that i can say make test and
you can see that i've got test coverage and if i say make lint we can see that i've also passed
the lint so i've got all these things working by using this really simple microservice architecture