A microservice skeleton

So far in this chapter, we've looked at how Flask works, and at most of the built-in features it provides--and we will be using them throughout this book.

One topic we have not covered yet is how to organize the code in your projects, and how to instantiate your Flask app. Every example so far used a single Python module and the app.run() call to run the service.

Having everything in a module is, of course, a terrible idea unless your code is just a few lines. And since we will want to release and deploy the code, it's better to have it inside a Python package so that we can use standard packaging tools like Pip and Setuptools.

It's also a good idea to organize views into blueprints, and have one module per blueprint.

Lastly, the run() call can be removed from the code, since Flask provides a generic runner that looks for an app variable given a module pointed by the FLASK_APP environment variable. Using that runner offers extra options like the ability to configure the host and port that will be used to run the app.

The microservice project on GitHub (https://github.com/Runnerly/microservice) was created for this book, and is a generic Flask project that you can use to start a microservice. It implements a simple layout, which works well for building microservices.

You can install it and run it, then modify it.

This project uses Flakon (https://github.com/Runnerly/flakon), which is a minimalistic helper that takes care of configuring and instantiating a Flask application with an INI file and a default JSON behavior.
Flakon was also created for this book to let you focus on building microservices with the minimal amount of boilerplate code.
Flakon is opinionated, so if its decisions do not suit you, you can just remove it from your project, and build your function that creates an app; or use one of the existing open source projects that provide this kind of feature.

The microservice project skeleton contains the following structure:

  • setup.py: Distutils' setup file, which is used to install and release the project
  • Makefile: A Makefile that contains a few useful targets to make, build, and run the project
  • settings.ini: The application default settings in the INI file
  • requirements.txt: The project dependencies following the pip format
  • myservices/: The actual package
    • __init__.py
    • app.py: The app module, which contains the app itself
    • views/: A directory containing the views organized in blueprints
      • __init__.py
      • home.py: The home blueprint, which serves the root endpoint
    • tests: The directory containing all the tests
      • __init__.py
      • test_home.py: Tests for the home blueprint views

In the following code, the app.py file instantiates a Flask app using Flakon's create_app helper; that takes a few options like a list of blueprints, which get registered:

    import os 
from flakon import create_app
from myservice.views import blueprints

_HERE = os.path.dirname(__file__)
_SETTINGS = os.path.join(_HERE, '..', 'settings.ini')

app = create_app(blueprints=blueprints, settings=_SETTINGS)

The home.py view uses Flakon's JsonBlueprint class, which implements the error handling we've seen in the previous section. It also automatically calls jsonify() on the object returned by the view if its a dictionary, like how the Bottle framework does:

    from flakon import JsonBlueprint

home = JsonBlueprint('home', __name__)

@home.route('/')
def index():
"""Home view.

This view will return an empty JSON mapping.
"""
return {}

This example application can run via Flask's built-in command line, using the package name:

$ FLASK_APP=myservice flask run 
* Serving Flask app "myservice"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

From there, building JSON views for your microservice consists of adding modules in microservice/views, and their corresponding tests.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset