dimmg / Flusk
Programming Languages
Projects that are alternatives of or similar to Flusk
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.
Blueprints
Blueprints helps to split large application in small modular packages (one would say - similar to django apps
).
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 HTTPrequest
andresponse
. 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.
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
-
SSH into the running
api
container and start the development serverdocker 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. -
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
- make changes to your models when needed
- create a migration
- check the migration script and modify it as needed
- 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.