This the back-end of the Arborator-Grew redevelopement of the arborator-server.
Before starting to dive deep in the code, you should refer to these following important resources :
- Flask-RESTx : How to build a restful server with python
- Some Flask good practives : How to use static typing and testing procedure with flask.
Create a .flaskenv file with the following config
FLASK_ENV=dev|prod|test
To run the backend we use the docker, the steps of the docker installation are here
- To ensure the communication between the containers of the backend and the frontend we need to create network
docker network create external-network
- Run the container of the backend using docker-compose
docker-compose up --build
Ask an admin of Arborator the keys.zip and the auth_config.py, and locate them in (from root)
app/auth/auth_config.py
keys/
If first time, initialize the database
python manage.py seed_db
Type "Y" to accept the message (which is just there to prevent you accidentally deleting things -- it's just a local SQLite database)
An migration is set using Flask-Migrate. For the moment this migration should only be done on dev mode.
To enable the migrations run the following commands after the git pull:
flask db init
The register it and enabling automatic migrations by setting a name (here it is "access migration")
flask db migrate -m "access migration."
Apply the migration
flask db upgrade
This migration adds a table alembic_version
in the database in order to track migration history.
To display migration history please run flask db history
.
For adding a superadmin, run the following command
python manage.py add_super_admin --username $username
For removing a superadmin
python manage.py remove_super_admin --username $username
In .flaskenv, set the FLASK_ENV
to dev and FLASK_APP
to wsgi.py
FLASK_ENV=dev
FLASK_APP=wsgi.py
Then, you can run the app with flask and a secured certificate protocole.
flask run --cert=adhoc
We need to specify cert
because we use Google and Github credentials login and it required an https connection.
To run the test suite, simply pip install it and run from the root directory like so
pip install pytest
pytest
- ssh to server
- go to backend folder
- pull latests changes :
git pull origin main
- restart service :
sudo systemctl restart arborator-backend.service
On the server, create the project folder
mkdir /var/www/flask_api_example
In the project folder, create a python virtual environment
python -m venv ven
Install the python packages requirements
pip install -r requirements.txt
In .flaskenv, set FLASK_ENV
to prod
FLASK_ENV=prod
Initialize the DB (or paste an existant one)
python migrate.py seed_db
If needed to, allow the folder access to the server user that will have control the app
sudo chown -Rf <admin>:<group> /var/www/flask_api_example/
To see the arborator logs :
tail -f /var/log/arborator-backend/arborator-backend.log
To see tge nginx logs :
sudo tail -f /var/log/nginx/access.log
To see all last commands run on the server :
history
Navigate to the posted URL in your terminal to be greeted with Swagger, where you can test out the API.