Code Monkey home page Code Monkey logo

flask-microservice-sqlalchemy-marshmallow's Introduction

flask-microservice-sqlalchemy-marshmallow

Quite a mouthful. This is a starter project for a flask microservice. It uses SQLAlchemy to interact with a database, and Marshmallow for serialization of objects.

The flask app itself is written using the application factory pattern, to allow simple integration with WSGI servers such as Gunicorn.

Application Configuration

Dynaconf has been used for simple configuration, a settings.yml file is provided for development purposes and to help document the configurable values. However dynaconf allows us to set these values as environment variables instead which is usually preferable (see The 12 Factor app)

Flask config values can be set here, as well as our own custom configuration values. See the dynaconf documentation

Setting up your environment

As per the Dockerfile, this project is using Python 3.8 (disclaimer: this will inevitably fall out of date, double check the dockerfile) so you'll need a Python 3.8 environment.

To ensure code quality is maintained, and to ensure your PR pipeline passes you will need to set up pre-commit.

pip install -r requirements-dev.txt

The first time you do this you'll also need to run: pre-commit install. This will set up the autoformatting hooks.

For running the application locally (i.e. not in a docker container) you'll need to run pip install -r requirements.txt

Running for development (debugging)

development_wsgi.py is provided to simplify running of the application. flask run with the appropriate arguments could also be used, but the development_wsgi file makes it simple to start the debugger in PyCharm community edition, which I why I've included it.

Running tests

Assuming your development environment is set up, it's as simple as running tox. To run the integration tests you'll need docker installed & running, as well as a working internet connection (to pull docker images).

flask-microservice-sqlalchemy-marshmallow's People

Contributors

mgough avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

flask-microservice-sqlalchemy-marshmallow's Issues

Add Dockerfile & Docker-compose file

I need to add a Dockerfile with best practices:

  • Caching dependencies
  • Running as non root user

A docker-compose file is also needed to show the use of setting dynaconf using environment variables, and to start up a postgres instance.

Consider updating `development_wsgi` to approach suggested by Flask author

I made a controversial and opinionated decision to make development_wsgi.py call app.run but this isn't the approach recommended by the creator of Flask. His approach is to change it to the following:

app = create_app()

then to run
flask run
with FLASK_APP=development_wsgi.py set as an environment variable.

My only real (lazy) reason for not doing this is because I'm yet to set this up to run in PyCharm community edition for debugging.

Add CI pipeline

This is just to illustrate how a pipeline can be run performing tests & building the docker image. Could also run a linter at this point (black?)

Also worth considering pre-commit hooks to include in the repo to ensure that the build should pass before a push is made

Add healthcheck endpoint

This should check our SQL connection is alive, as well as serving the version of the application to aid in debugging.

Version will be provided as a Docker ARG which is set by the build, when not set (i.e. development) we'll set it using f'development-{uuid.uuid4()}'

Add docker-compose configuration

A docker-compose file is also needed to show the use of setting dynaconf using environment variables, and to start up a postgres instance.

Add unit tests

The flask services need unit tests! Will be run using tox.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.