hegdeashwin / langchain-demo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

License:MIT License


Languages

Language:Python 92.7%Language:Dockerfile 7.3%