Microservice project with Python with Flask
A microservices is an individual component of an application that follows the microservice architecture - an architectural style that structures an application as a collection of loosely coupled services, which implement business capabilities. The microservice exposes a RESTful API matching a Swagger definition.
- If you don't already have it, install Python
- Install IBM Cloud Developer Tools on your machine
- Install the plugin with:
bx plugin install dev -r bluemix
IBM Cloud DevOps services provides toolchains as a set of tool integrations that support development, deployment, and operations tasks inside IBM Cloud. The "Create Toolchain" button creates a DevOps toolchain and acts as a single-click deploy to IBM Cloud including provisioning all required services.
*Note you must publish your project to Github for this to work.
The project contains IBM Cloud specific files that are used to deploy the application as part of an IBM Cloud DevOps flow. The .bluemix
directory contains files used to define the IBM Cloud toolchain and pipeline for your application. The manifest.yml
file specifies the name of your application in IBM Cloud, the timeout value during deployment, and which services to bind to.
Credentials are either taken from the VCAP_SERVICES environment variable if in IBM Cloud, or from a config file if running locally.
The IBM Cloud development plugin makes it easy to compile and run your application if you do not have all of the tools installed on your computer yet. Your application will be compiled with Docker containers. To compile and run your app, run:
bx dev build
bx dev run
Running flask applications has been simplified with a manage.py
file to avoid dealing with configuring environment variables to run your app.
python manage.py subcommand [ipaddress]
manage.py
offers a variety of different run commands to match the proper situation:
start
: starts a server in a production setting usinggunicorn
.run
: starts a native flask development server. This includes backend reloading upon file saves and the Werkzeug stack-trace debugger for diagnosing runtime failures in-browser.livereload
: starts a development server via thelivereload
package. This includes backend reloading as well as dynamic frontend browser reloading. The Werkzeug stack-trace debugger will be disabled, so this is only recommended when working on frontend development.debug
: starts a native flask development server, but with the native reloader/tracer disabled. This leaves the debug port exposed to be attached to an IDE (such as PyCharm'sAttach to Local Process
)
There are also a few utility commands:
build
: compiles.py
files within the project directory into.pyc
filestest
: runs all unit tests inside of the project'stest
directory
Your application is running at: http://localhost:3000/
in your browser.
- Your Swagger UI is running on:
/explorer
- Your Swagger definition is running on:
/swagger/api
- Health endpoint:
/health
To build and debug your app, run:
bx dev build --debug
bx dev debug
There are two different options for debugging a flask
project:
- Run
python manage.py runserver
to start a native flask development server. This comes with the Werkzeug stack-trace debugger, which will present runtime failure stack-traces in-browser with the ability to inspect objects at any point in the trace. For more information, see Werkzeug documentation. - Run
python manage.py debug
to run a flask development server with debug exposed, but the native debugger/reloader turned off. This grants access for an IDE to attach itself to the process (i.e. in PyCharm, useRun
->Attach to Local Process
)