Flask Api¶
Flask-api is a small API project for creating users and files (Microsoft Word and PDF). These files contain data about users registered in the project.
The project is developed in Python 3.7 and use next main libraries:
Flask: microframework.
Sqlite: SQL database engine.
Peewee: simple and small ORM.
Celery: asynchronous task queue/job.
RabbitMQ: message broker.
NGINX: web server, reverse proxy, etc.
UWSGI: Web Server Gateway Interface (WSGI) server implementation.
Flower: monitoring and administrating Celery clusters.
Supervisor: client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.
Installation¶
1. Linux packages¶
These packages are required for the project installation:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install autoconf build-essential cmake libcap-dev libffi-dev libpcre3-dev librabbitmq-dev libreoffice-writer libtool libxml2-dev libxslt1-dev libxslt1.1 pkg-config magic nginx python3-distutils python3.7 python3.7-dev python3.7-venv rabbitmq-server uuid-dev uwsgi uwsgi-src
sudo reboot
2. RabbitMQ configuration¶
Required plugins for monitoring our brokers in Flower:
sudo rabbitmq-plugins enable rabbitmq_management
sudo service rabbitmq-server restart
3. Python dependencies¶
Install Python dependencies:
python3.7 -m venv venv
source venv/bin/activate
pip install -r requirements.txt --no-cache-dir
5. Environment configuration¶
Create a new .env file based on .env.example file.
6. uWSGI configuration¶
sudo usermod -a -G www-data username
7. Nginx configuration¶
sudo mv docs/examples/flask_api /etc/nginx/sites-available
sudo ln -s /etc/nginx/sites-available/flask_api /etc/nginx/sites-enabled/flask_api
sudo systemctl restart nginx
8. Supervisor configuration¶
8.1 Main configuration¶
8.2 Other configurations¶
Create a new directory named supervisor in the root path and create next files based on docs/examples/supervisor example files:
celery.conf
flower.conf
uwsgi.conf
username and path variables must to be replaced with appropriate values.
10. Supervisor systemd unit file¶
sudo systemctl enable flask_api_supervisor.service
sudo systemctl daemon-reload
sudo systemctl start flask_api_supervisor.service
sudo systemctl status flask_api_supervisor.service
For restart all processes in command line:
sudo systemctl restart flask_api_supervisor.service
This command reread the supervisor configuration files, stop all processes and start them again.
How to usage¶
The setup is finished, we only need to create the database tables and fill them with fake data. We open a terminal in the root project and run next commands:
./venv/bin/flask init-db
./venv/bin/flask migrate
./venv/bin/flask seed
Optional installation¶
This project use logrotate for logging configuration. The config file is already defined you only need to do these steps:
Create new flask_api.logrotate file based on docs/examples/flask_api.logrotate.example.
path, username and usergroup variables must to be filled with appropiate values.
Move flask_api_logrotate to /etc/logrotate.d:
sudo mv docs/examples/flask_api.logrotate /etc/logrotate.d
Restart logrotate service:
sudo service log rotate restart
A new log file will be created every day.
Skeleton app structure¶
The project structure looks like this:
flask_api
├── /app
│ ├── /blueprints
│ │ └── ...
│ ├── /celery
│ │ └── ...
│ ├── /models
│ │ └── ...
│ ├── /templates
│ │ └── ...
│ ├── /utils
│ │ └── ...
│ ├── __init__.py
│ ├── extensions.py
│ └── middleware.py
├── /database
│ ├── factories
│ │ └── ...
│ ├── migrations
│ │ └── ...
│ ├── seeds
│ │ └── ...
│ └── __init__.py
├── /log
│ └── ...
├── /storage
│ └── ...
├── /tests
│ └── ...
├── config.py
├── manage.py
└── requirements.txt
Package for building a Flask application. |
|
Package for managing the database. |
|
Package for testing the application. |
|
Module loads the application’s configuration. |
Flask command line¶
Flask command line allow run scripts for managing database, start up task queues, etc.
You don’t need to start up the server for running these scripts but you must activate your virtual environment.
Changelog¶
All notable changes to this project will be documented in this file. See standard-version for commit guidelines.
2.0.0 (2020-10-27)¶
⚠ BREAKING CHANGES¶
order field in search requests is a list of dicts.
Features¶
Code Refactoring¶
Build System¶
add .versionrc that shows build/perf/refactor/revert (0017d66)
add sphinx-click configuration to Sphinx and create new file for showing Click documentation (bb41b7b)
add sphinx-click for showing Click documentation in Sphinx (c0a8f55)
pip: remove cerberus package (4a4fe72)
pip: split python packages in two requirements local and production (f39a2a5)
1.0.0 (2020-05-17)¶
⚠ BREAKING CHANGES¶
pip: update Python dependencies
Features¶
celery: add basic installation (147dd2c)
db: add pewee migrations (231696c)
documents: add document logic (1eb7ec1)
emails: add send emails after creation an user (7c2cfe0)
log: add support for logrotate (09925e1)
users: add created_by column in user model (8a3d013)
users: add Excel and PDF users export to background processes (781e091)
users: add recovery password feature (e1e916e)
0.7.0 (2020-04-23)¶
0.6.1 (2020-04-23)¶
⚠ BREAKING CHANGES¶
update python dependencies
Features¶
Bug Fixes¶
commitizen: fixed problem with the process of commitizen tags (1d3677d)
docs_to_pdf: fixed problem about convert a docx file to a pdf file with uWSGI (aabbc2d)
peewee: fixed problem about a connection already opened error (6279470)
peewee: problem with database connection already opened (e6c07c9)
users: update user endpoint cannot update data (9dfc4cc)
request search fields in search users, export PDF and export Excel endpoints (2ae7ab7)
Note¶
If you find any bugs, odd behavior, or have an idea for a new feature please don’t hesitate to open an issue on GitHub.