Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Microservices Development – 2nd edition

You're reading from   Python Microservices Development – 2nd edition Build efficient and lightweight microservices using the Python tooling ecosystem

Arrow left icon
Product type Paperback
Published in Sep 2021
Publisher Packt
ISBN-13 9781801076302
Length 310 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Tarek Ziadé Tarek Ziadé
Author Profile Icon Tarek Ziadé
Tarek Ziadé
Simon Fraser Simon Fraser
Author Profile Icon Simon Fraser
Simon Fraser
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Understanding Microservices 2. Discovering Quart FREE CHAPTER 3. Coding, Testing, and Documentation: the Virtuous Cycle 4. Designing Jeeves 5. Splitting the Monolith 6. Interacting with Other Services 7. Securing Your Services 8. Making a Dashboard 9. Packaging and Running Python 10. Deploying on AWS 11. What's Next? 12. Other Books You May Enjoy
13. Index

A microservice skeleton

So far in this chapter, we have looked at how Quart works, and at most of the built-in features it provides—all of which we will be using throughout this book. One topic we have not yet covered is how to organize the code in your projects, and how to instantiate your Quart app. Every example so far has used a single Python module and the app.run() call to run the service.

Having everything in a module is possible, but will create a lot of headaches unless your code is just a few lines. 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 such as pip and setuptools.

It is also a good idea to organize views into blueprints, and have one module per blueprint. This lets us keep better track of what each bit of code does, and re-use code whenever possible.

Lastly, the run() call can be removed from the code since Quart provides a generic run command that looks for an application using information from the QUART_APP environment variable. Using that runner offers extra options, such as the ability to configure the host and port that will be used to run the app without going into the settings each time.

The microservice project on GitHub was created for this book and is a generic Quart project that you can use to start a microservice. It implements a simple layout, which works well for building microservices. You can install and run, and then modify it. The project can be found at https://github.com/PythonMicroservices/microservice-skeleton.

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.yml: The application default settings in a YAML file.
  • requirements.txt: The project dependencies following the pip format produced by pip freeze.
  • 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 Quart app using a helper function called create_app to register the blueprints and update the settings:

import os
from myservice.views import blueprints
from quart import Quart
_HERE = os.path.dirname(__file__)
_SETTINGS = os.path.join(_HERE, "settings.ini")
def create_app(name=__name__, blueprints=None, settings=None):
    app = Quart(name)
    # load configuration
    settings = os.environ.get("QUART_SETTINGS", settings)
    if settings is not None:
        app.config.from_pyfile(settings)
    # register blueprints
    if blueprints is not None:
        for bp in blueprints:
            app.register_blueprint(bp)
    return app
app = create_app(blueprints=blueprints, settings=_SETTINGS)

The home.py view uses a blueprint to create a simple route that doesn't return anything:

from quart import Blueprint
home = Blueprint("home", __name__)
@home.route("/")
def index():
    """Home view.
    This view will return an empty JSON mapping.
    """
    return {}

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

$ QUART_APP=myservice quart run
 * Serving Quart app 'myservice.app'
 * Environment: production
 * Please use an ASGI server (e.g. Hypercorn) directly in production
 * Debug mode: False
 * Running on http://localhost:5000 (CTRL + C to quit)
[2020-12-06 20:17:28,203] Running on http://127.0.0.1:5000 (CTRL + C to quit)

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

You have been reading a chapter from
Python Microservices Development – 2nd edition - Second Edition
Published in: Sep 2021
Publisher: Packt
ISBN-13: 9781801076302
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image