Skip to content

cnr-ibba/SMARTER-backend

Repository files navigation

SMARTER-backend

SMARTER Backend API

Build Status Coverage Status Documentation Status

Setting the .env file

docker-compose can read variables from a .env placed in the working directory. Here we will define all variables useful for our containers, like database password. Edit a new .env file in working directory and set passwords for such environment variables:

MONGODB_ROOT_USER=<root user>
MONGODB_ROOT_PASS=<root pass>
MONGODB_SMARTER_USER=<smarter user>
MONGODB_SMARTER_PASS=<smarter pass>
JWT_SECRET_KEY=<secret key>

TODO: manage sensitive data using secret in docker-compose, as described here and here

Genereate the JWT secret key

Here you can find hints on how to define a secret key with python:

import string
import secrets
print(''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(50)))

Setting the proper permissions

Set mongodb-home folder permissions with:

chmod 777 mongodb-home/
chmod o+t mongodb-home/

Fix flask-data permissions:

docker-compose run --no-deps --rm uwsgi sh -c 'chgrp -R www-data .'
cd flask-data/
find . -type f -iname "*.py" -exec chmod g-w {} \;

Add a smarter user

Add a smarter user by calling a flask script:

docker-compose run --rm uwsgi flask users create smarter

Connect to mongodb

docker-compose run --rm --user mongodb mongo sh -c 'mongo --host mongo --username="${MONGO_INITDB_ROOT_USERNAME}" --password="${MONGO_INITDB_ROOT_PASSWORD}"'

Import data into database

Execute a mongodump of an instance of the SMARTER-database project, for example:

docker-compose run --rm --user mongodb mongo sh -c 'DATE=$(date +%Y-%m-%d); mongodump --host mongo --username="${MONGO_INITDB_ROOT_USERNAME}" --password="${MONGO_INITDB_ROOT_PASSWORD}" --authenticationDatabase admin --db=smarter --gzip --archive=/home/mongodb/${DATE}\_smarter.archive.gz'

Then copy (or move) the dump file into mongodb-home folder of this project. You can restore the database using mongorestore, for example:

docker-compose run --rm --user mongodb mongo sh -c 'mongorestore --host mongo --username="${MONGO_INITDB_ROOT_USERNAME}" --password="${MONGO_INITDB_ROOT_PASSWORD}" --authenticationDatabase admin --db=smarter --drop --preserveUUID --gzip --archive=/home/mongodb/2021-06-18_smarter.archive.gz'

Monitoring UWSGI processes

Enter inside uwsgi container (with docker-compose exec), then monitor uwsgi with uwsgitop:

docker-compose exec uwsgi bash
uwsgitop /tmp/smarter-stats.sock

Type q to exit from monitoring process

Test application

Some useful commands to test the application:

# open the flask shell
docker-compose run --rm uwsgi flask shell

# test and exit when a issue is found. Call first the failed test on successive calls
docker-compose run --rm uwsgi pytest --verbosity=2 --exitfirst --failed-first --showlocals

# test with coverage
docker-compose run --rm uwsgi coverage run --source='.' -m py.test

# generate coverage report locally
docker-compose run --rm uwsgi coverage html

# check code with flake8
docker-compose run --rm uwsgi flake8

# test like CI
docker-compose run --no-deps --rm uwsgi sh -c 'coverage run --source='.' -m py.test && flake8'