Microservices using Flask — creating a basic web flask application

Saurav Samantray
3 min readDec 6, 2020

--

First part of the Microservices using Flask series where we will explore not only how to create a micro web application using Flask but also understand what makes the application robust and production ready.

For the purpose of the article let’s name our microservice as hello-world-service

App structure

The folder structure/content of hello-world-service will look like below. Although I talked about making application production ready, this structure might now adhere to certain production ready standards, but this article is to have a quick understanding on flask micro application. Future articles will have a more production ready touch.

hello-world-service
├── app.py
├── config.py
└── requirements.txt

app.py: The main file that will be used to initialize and start the application

config.py: encapsulates different settings you might want to change depending on the application environment like toggling the debug mode, setting the secret key, and other such environment-specific things.

requirements.txt: list of all dependencies required for the application.

Let’s start coding!

requirements.txt

Flask==1.1.1

As this just an introduction application, we have just the Flask dependency. All future dependencies should also go into this same file.

config.py

Usually articles show you a simple config.py or settings.py to load configuration, which works pretty well but you later realize that you need different configuration in different environment (development, testing, production etc.). This configuration will help you address that challenge. It has basic configurations listed under Config class which can be overridden by child classes specific to environment. Later we will know how to specify which class to use for configuration while creating the environment.

app.py

Line number 4 specifies that the configuration is loaded based on an environment variable CONFIG_SOURCE, which can have values like config.DevelopmentConfig to load development environment related configurations or config.ProductionConfig for production environment specific configuration

app.run() initiates the application which utilized the built-in Werkzeug app servers (only for development).

This application right now only handles the default route “/” and uses the hello() method to serve response(a static string “Hello World!”)

Creating environment and running the application

Specify the environment for the application to load configuration accordingly.

On macOS and Linux:
export CONFIG_SOURCE= config.DevelopmentConfig
On Windows:
SET CONFIG_SOURCE=config.DevelopmentConfig

Create a virtual environment

python -m venv env

Activate the virtual environment

On macOS and Linux:
source env/bin/activate
On Windows:
.\env\Scripts\activate

Install requirements

pip install -r requirements.txt

Starting the application

python app.py

You should get logs like below

* Restarting with stat* Debugger is active!* Debugger PIN: 227-585-361* Running on http://0.0.0.0:8001/ (Press CTRL+C to quit)

You can validate by access the page http://localhost:8001/

All the related code can be found below

Next: Containerizing your flask application using docker and making it production ready with Gunicorn and nginx

--

--

No responses yet