I am trying to develop an API with different "submodules". For example there is one called todo and another one called newsletter.
What I want to do is, I'd like to be able to run the modules themselves during development and I'd like to be able to run the whole API with it's submodules on the server.
My current folder structure looks like that
api
- api
- todo
- todo
- __init__.py
- routes.py
- utils.py
- ...
- README.md
- requirements.txt
- newsletter
- ...
- run.py
The aim is to be able to run this during development:
cd api/todo/todo
python3 run.py
And this on the server
python3 run.py
The routes should be relative:
During the production the path for one submodule should be like that
/add
But on the server they should always include the name of submodule
/todo/add
Moreover I'd like to be able to put the code of every submodule in one GitHub repository, so that other persons will be able to run them on themselves.
I am currently stuck on importing the modules. Can I fix the path problem by using Flasks Blueprints?
I would appreciate any help very much!
You can use the following project structure:
app
api
-todo
-__init__.py
-other_apis.py
-newsletter
-__init__.py
-other_apis.py
__init__.py
In the app.api.todo.__init__.py
:
from flask import Blueprint
todo = Blueprint("todo", __name__)
In the app.api.newsletter.__init__.py
:
from flask import Blueprint
newsletter = Blueprint("newsletter", __name__)
In the app.__init__.py
:
from flask import FLask
app = Flask(__name__)
from app.api.todo import todo
from app.api.newsletter import newsletter
app.register_blueprint(todo, url_prefix="/api/todo")
app.register_blueprint(newsletter, url_prefix="/api/newsletter")
if __name__ == "__main__":
app.run()
And you can change which blueprint is registered and what url prefix bound to blueprint with environment variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With