Skip to content

7552-2020C2-grupo5/users-microservice

Repository files navigation

users-microservice

GitHub tag (latest SemVer) CoverageTestsLintersBandit

Users microservice for BookBNB.

Installing the project

This project was built with poetry in mind as the means to manage dependencies. You can follow their install guide to install it.

Having poetry installed, the following command will install the project into a new environment.

poetry install

Keep in mind that this will by default install the base requirements as well as the dev requirements.

To install the testing extras, run:

poetry install -E testing

Remember to commit to the repo the poetry.lock file generated by poetry install.

Dev

Installing pre-commit hooks

We use pre-commit to run several code scans and hooks that make the development cycle easier.

pre-commit install
pre-commit install -t push

Adding new dependencies

Check the full poetry docs, but here goes a quick reminder,

poetry add <dependency> [--dev]

Always remember to commit changes to poetry.lock!

Bumping SemVer version

We use SemVer. To keep things easy, dev dependencies include bump2version . Running poetry run bump2version minor will bump the minor version.

poetry run bump2version <part>

Running nox sessions

In order to bootstrap dependencies and run several actions, we are using nox. This way, dependencies are isolated and you get environment replicability.

To run all sessions,

nox

To run tests session,

nox --sessions tests [-- pylint arguments]

To run linting session,

nox --sessions cop

To run bandit session,

nox --sessions bandit

To run pyreverse session,

nox --sessions pyreverse

Style guide

users-microservice follows PEP8.

If you installed the pre-commit hooks you shouldn't worry too much about style, since they will fix it for you or warn you about styling errors. We use the following hooks:

  • black: An opinionated code formatting tool that ensures consistency across all projects using it
  • flake8: a tool to enforce style guide
  • mypy: a static type checker for Python
  • pylint: a source code, bug and quality checker

Docstrings

We use either numpy style or google style docstring formatting. It's usually good to include the following docstrings:

  • module level docstring giving a general overview of what it does.
  • class dosctrings explaining what it is
  • method/functions to explain what it does and what it's parameters are

Testing

We use the pytest framework to test users-microservice. The easiest way to run tests it through nox with nox --sessions tests.

Docker

Get everything up and running.

cd docker-compose
docker-compose build
docker-compose up

Running locally

First make sure you have the db up to date, and then run locally.

poetry run python users_microservice/manage.py db upgrade
FLASK_APP=$(pwd)/users_microservices/app.py poetry run flask run

Deploy to heroku

You will need to have the heroku cli installed and correctly configured for the following steps.

Prior to the actual deploy, make sure to commit your changes.

heroku create users-microservice
heroku addons:create heroku-postgresql:hobby-dev
heroku stack:set container
git push heroku master
  1. The first step initializes a new heroku app
  2. The second step provisions a postgres addon
  3. The third step sets the app to use a docker image. Instead of using a Procfile, we will use a heroku.yml. Heroku does not yet support a poetry buildpack and exporting a requirements.txt from poetry is pretty cumbersome.
  4. Deploy 🚀

Diagnosing errors

You can fetch logs from the app using heroku logs --tail.

CD

Go to the app on the Heroku Dashboard. On the deploy tab, select "Connect to github" under the "Deployment method" section. Select your repo and you're good to go. Pushes to master will deploy a new version.

Is the app running?

Free dynos sleep after 30 min if no incoming web traffic is received. It might take a while, but you should be able to see the app's swagger the root URL. Use the "Open App" button on the dashboard.

DataDog

The heroku Dockerfile includes the DataDog agent. Create a new DataDog API Key from here. Remember to set the following config vars:

heroku config:set DD_API_KEY=<your_api_key>
heroku config:set DD_DYNO_HOST=false
heroku config:set HEROKU_APP_NAME=bookbnb5-users-microservice
heroku config:set DD_TAGS=service:users-microservice

GitHub Actions

A few pipelines have been set to run on github actions to ensure code quality.

sessions.yml

This workflow runs linters and tests.

Documentation

Swagger

You can visit the swagger docs at 127.0.0.1:5000.

Whole project class diagram

project_classes

Packages

packages