Giter VIP home page Giter VIP logo

template-py-rest-microservice's Introduction

Template Python REST Microservice

Pylint CodeQL License

The repository is a project template for REST microservice built using FastAPI (Python). The latest version supports Python 3.8 and above.

Prerequisite

Required

Optional

Features

List of features that comes with default template

  • Use FastAPI as base framework to build the REST microservice.
  • Use Poetry as a tool for dependency management and packaging in Python.
  • Predefined project scaffolding like files and directories, event handlings, routers, middlewares etc.
  • Comes with default configurations for hostname, port, environment etc. Each of these configuration can be customize as per microservice needs.
  • Predefined common logger for application logging.
  • Preconfigured special routes /info and /health.
  • Use Docker to make it easy to run the app on container and shift it.
  • Predefined GitHub Actions for workflows for PyLint and CodeQL.

Code Structure

Feel free to modify the layout of the repo as much as you want but the given structure is as follows:

app/
├── __init__.py
├── main.py
├── api.py
├── metadata.py
├── configs/
│   └── development.py
│   └── production.py
│   └── stage.py
├── core/
│   └── common_handlers.py
├── endpoints/
│   └── health.py
│   └── info.py
│   └── router.py
│   └── users.py
├── middlewares/
│   └── validation.py
└── models/
    └── users.py
├── test_main.py
  • __init__.py defines and initializes the app configuration.

  • main.py defines the FastAPI application, adds middleware, includes routers, and creates the Mangum handler.

Setup

Environment Variables

export <Name>=<Value>

For example:
export BEAVER_API_SYS_INS_TYPE="DEVELOPMENT"

⚠️ No space before and after = sign.

Name Purpose Possible Values
BEAVER_API_SYS_INS_TYPE Help to identify system instance type on which the app service is running DEVELOPMENT, STAGE and PRODUCTION

Development

export BEAVER_API_SYS_INS_TYPE="DEVELOPMENT"

Install all dependencies using Pip

pip install -r requirements.txt

Run the service

uvicorn "app.main:app" --host="0.0.0.0" --port=8000 --reload

Build & Run the service using Docker

docker build -t pyrest .
docker run -d -p 8000:8000 pyrest

Run the tests

pytest

Run the Swagger API Docs

http://localhost:<PORT>/api/v1/docs

Run the lint

Run pylint before committing the changes and ensure code quality at least 9.30/10

pylint --rcfile .pylintrc $(git ls-files '*.py')

Run the formatter

Run black & isort before committing the changes

black app
isort **/*.py

Stage

export BEAVER_API_SYS_INS_TYPE="STAGE"

Install only dependencies (exclude dev-dependencies) using Poetry

poetry install --no-dev

Run the service

uvicorn "app.main:app" --host="<STAGE_HOST_IP>" --port=<STAGE_PORT> --workers 2

Run the Swagger API Docs

http://<STAGE_BASE_URL>:<STAGE_PORT>/api/v1/docss

Production

export BEAVER_API_SYS_INS_TYPE="PRODUCTION"

Install only dependencies (exclude dev-dependencies) using Poetry

poetry install --no-dev

Run the service

uvicorn "app.main:app" --host="<PRODUCTION_HOST_IP>" --port=<PRODUCTION_PORT> --workers 4

Run the Swagger API Docs

http://<PRODUCTION_BASE_URL>:<PRODUCTION_PORT>/api/v1/docs

References

template-py-rest-microservice's People

Contributors

hegdeashwin avatar dependabot[bot] 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.