GithubHelp home page GithubHelp logo

freelancer / esapyi Goto Github PK

View Code? Open in Web Editor NEW
7.0 7.0 0.0 648 KB

A dockerized and production ready python API template with no setup required.

License: GNU Lesser General Public License v3.0

Python 83.89% Dockerfile 2.49% Mako 1.01% Shell 12.61%
python-template flask-api python3 docker

esapyi's Introduction

esapyi · CI Tests

A dockerized and production ready python API template with no setup required.

This project aims to provide a pre-configured python API server template based on the Flask python framework.

Table of Contents
Quickstart Guide
The Hitchhicker's Guide to api_boilerplate
Developer Guide

Quickstart guide

  1. Ensure that you have docker installed on your system.
  2. Clone this repository using git clone https://github.com/freelancer/esapyi.git
  3. (Optional) Pick a new name for your project and run ./project_rename.sh "my_project_name"
  4. Start the python server using ./run.sh dev

The Hitchhicker's Guide to api_boilerplate

This section will take you through the following

Project Structure

api_boilerplate
├── app.py
├── config
│   ├── __init__.py
│   ├── local_development.py
│   ├── prod.py
│   └── testing.py
├── exceptions
├── handlers
├── healthcheck
├── models
├── utils
└── v1
    ├── exceptions
    ├── handlers
    ├── input_schemas.py
    ├── output_schemas.py

Developer Guide

This section goes through the various tools and procedures a developer would need when developing using this setup.

Contents

Commands Quickstart

All of the common development tasks that need to be performed are done through the run.sh script located at the root of the project. run.sh has the following commands

  • run.sh dev - start the local db and python development server at port 8080
  • run.sh prod - start the local db and the production uWSGI python server at port 8080
  • run.sh lint - lint the entire project
  • run.sh test - run the entire test suite for the project
  • run.sh check - run the lint and test command in succession
  • run.sh dev db - spin up and connect to the local dev database
  • run.sh alembic - proxy to the alembic cli

Linting

This project uses 2 main linting programs

  • pyright - a static type-checker
  • ruff - a code quality checker

If you want to customize the lint configuration, modify the following files

Testing

This project uses the pytest framework for writing and maintaining unit tests.

  • In order to configure pytest, use the pytest.ini file

The unit tests also spin up a dockerized MySQL database. Every run.sh test call will kill and re-create the database to ensure tests are running in a fresh environment.

There are 3 base test classes which you should extend when writing tests.

Database Migrations

This project uses alembic as a database migrations tool. This tool essentially acts as a version control for your database schema. Any changes that need to be made should be tracked and done through alembic.

Example: Adding a new column

Say we want to add a new column to our users table - first_name.
To do so, follow these steps

  1. Modify the user model and add the first_name column
first_name = Column(Text, nullable=False)
  1. Run the alembic command to autogenerate a revision
    • This command creates a new file under the migrations/versions folder which contains code to create this new column in the db
./run.sh alembic revision --autogenerate -m"add_fname_to_user"
  1. Run the alembic command to upgrade the database
./run.sh alembic upgrade head
  1. That's it! The user table in the local docker database now has a first_name column. Remember to commit the generated migration file to git.

Production Deployment

It is recommended that you deploy this API as a docker container on your production server. In order to help build this container there is a production ready dockerfile provided.

In production, the python app is run using uWSGI. The configuration for this uWSGI server is in uwsgi_prod_app_config.ini.

esapyi's People

Contributors

adirishi avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.