Hello everyone, as we know, micro services is paradigm for software architecture that awesome, because less dependency about one into another services. Python flask have one that is blueprint, that is we can create blueprint of our micro services.
First, let decide what thing that we will create, imagine that we will create content management system that need two module, login and register. Here the structure of project.
From there we separated two service, login and register. That is different service but same implementation, so I just explain in one service, for example we use login_service.
First, we create app.py to return our app, fyi, I use docker compose to run this project, so I need return the apps to run the app, so here the code.
from login_service.modules import app
Then, let create initialisation for our module ini __init__.py file.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from login_service.modules import settings
app = Flask(__name__)
db = SQLAlchemy(app)
from login_service.modules.login.views import login
In here, we register our views into blueprint, in here, we have submodule login to handle login process. And settings.py is our setting for function that need configuration like connect into database or else.
Then what happen in views? here the code views.py
from flask import Blueprint, jsonify, json
from flask_restful import Api, Resource
login = Blueprint(
'login', #name of module
template_folder='templates' # templates folder
api = Api(login)
// here login process happen
There is views of login submodule, we create new Blueprint in views, then create api from flask_restful. Then, we create new method that run login process. After that, we need add resource the class and register the endpoint.
Oya! If you ask about the model, that is optional, to get data from database.
So, we can call our new api using this path
Get it? Okay I will explain more about correlation with micro service. In that process, we can create every endpoint with less dependency with another services. and we can register our new endpoint using
api.add_resource in every views. Then in
__init__.py we can register the module, without knowing what endpoint that added in views.
Aaaaand, this implementation will more powerful when implement using docker-compose. I think I will explain in the next article.
If you want learn the code, you can check here.