• Stars
    star
    211
  • Rank 186,867 (Top 4 %)
  • Language
    Python
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Boilerplate API on how to structure big Flask applications (includes SQLAlchemy, Docker, nginx)

Flusk

Flask - SQLAlchemy's declarative base - Docker - custom middleware.

Specifications

Application factory

Factories helps in creating many instances of the application. In this project, testing environment creates a new app instance whenever tests are ran.

Read More

Blueprints

Blueprints helps to split large application in small modular packages (one would say - similar to django apps).

Read More

Logic separation

The logic is splitted in the following layers:

  • backend (persistence layer) - where resides the code to read/write/delete records to disk or database
  • domain (domain layer) - where resides the bussines logic and external api integrations, i/o operations for a blueprint
  • views (presentation layer) - knows only about HTTP request and response. Its duty is to process a request and pass data to lower layers and to return responses.
  • models (data model layer) - where all blueprint models are defined
Middleware

The application tends to use middlewares instead of decorators or custom functions across the project.

  • application/json requests - ensures that all incoming requests are of application/json Content-Type
  • schema validation - validates the request payload against a JSON Schema
  • cors - allow cors requests for consumer apps
  • json exceptions - custom exception handler used to raise JSON exceptions
  • json responses - custom response handler used to return JSON objects
Extensions

The project tends to use the framework agnostic extensions over the flask ones, because they are usually wrappers and besides that, they may add additional functionality that you don't actually need (e.g. managers)

Docker

Ensures that the application you develop on local machine, behaves exactly in production.

Official Site

Directory layout

.
โ”œโ”€โ”€ core                                   # main codebase for the application
โ”‚ย ย  โ”œโ”€โ”€ api                                # API specific codebase
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ common                         # shared logic used by the application
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ database.py                # common database logic
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ exceptions.py              # custom exception classes
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ __init__.py
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ middleware                 # application middleware
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ __init__.py            # define application middlewares
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ request.py             # `request` related middleware
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ response.py            # `response` related middleware
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ serializers.py             # custom defined serializers
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ validation.py              # JSON schema validation logic
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ conftest.py                    # pytest configurations and custom fixtures
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ foss                           # flask blueprint
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ backend.py                 # logic related to database queries
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ domain.py                  # business logic and external integrations
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ __init__.py                # blueprint config
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ models.py                  # blueprint models
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ tests                      # blueprint tests
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ __init__.py
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ test_unit.py           # unit tests
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ test_integration.py    # integration tests
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ test_models.py         # database models tests
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ views.py                   # logic related to request -> response
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ __init__.py                    # app factory, blueprints, errorhandler and middleware registration
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ specifications                 # API specifications, RAML files and JSON schemas
โ”‚ย ย  โ”‚ย ย      โ””โ”€โ”€ schemas                    # JSON schemas folder
โ”‚ย ย  โ”‚ย ย          โ””โ”€โ”€ foss                   # schemas for a specific blueprint
โ”‚ย ย  โ”‚ย ย              โ”œโ”€โ”€ create_foss.json   # endpoint/view/route schema
โ”‚ย ย  โ”‚ย ย              โ””โ”€โ”€ update_foss.json   # endpoint/view/route schema
โ”‚ย ย  โ”œโ”€โ”€ Dockerfile                         # Dockerfile for the flask application
โ”‚ย ย  โ”œโ”€โ”€ requirements.txt                   # application dependencies
โ”‚ย ย  โ””โ”€โ”€ run.py                             # application creation and running
โ”œโ”€โ”€ docker-compose.yml                     # Dockerfiles manager
โ”œโ”€โ”€ Makefile                               # set of useful tasks (make `targets`)
โ”œโ”€โ”€ nginx                                  # nginx docker image related information
โ”‚ย ย  โ”œโ”€โ”€ Dockerfile                         # Dockerfile for the nginx web server
โ”‚ย ย  โ””โ”€โ”€ sites-enabled
โ”‚ย ย      โ””โ”€โ”€ nginx.conf                     # nginx configuration
โ””โ”€โ”€ README.md

Prerequisites

  • Python 3.5
  • Docker

Installation

Clone the repository

git clone https://github.com/dimmg/flusk.git

Build and run docker images

make dcompose-start

Run application

  • Development

    SSH into the running api container and start the development server

    docker exec -it flusk_api_1 bash
    python run.py
    

    By having a running server, execute

    docker inspect flusk_nginx_1
    

    where IPAddress it is the address of the running application.

  • Production

    Change docker-compose.yml file as follows:

    command:
        gunicorn -w 1 -b 0.0.0.0:5000 run:wsgi
        # tail -f /dev/null
    

    Rebuild the images via

    make dcompose-restart
    

    After rebuilding, the gunicorn wsgi server is running in background.

    To get the address of the running web server container run

    docker inspect flusk_nginx_1
    

Migrations

Migrations are done using the alembic migration tool.

Flow
  1. make changes to your models when needed
  2. create a migration
  3. check the migration script and modify it as needed
  4. apply the migration
Commands
  • create migration
make db-revision msg=<..message..>
  • apply the last migration
make db-upgrade
  • get the raw SQL for the last migration
make db-upgrade-sql

Note that these are the basic migration commands. To get the most from alembic, use the original $ alembic runner.