/ngr

FastAPI example to use on AWS Lambda

Primary LanguagePythonMIT LicenseMIT

SVG Blob from Blobmaker

NGR

master codecov Code style: black Imports: isort

FastAPI project example made for deployment with AWS Lambda.

Run locally 💻

  1. Install dev dependencies with Makefile

This will install dependencies found at requirements-dev.txt and requirements.txt.

make install-dev
  1. Install an ASGI server, for example Uvicorn:
pip install uvicorn
  1. Run the server locally:

This will run the API locally and if you change anything in the code reload it.

uvicorn ngr.main:app --reload

Structure 📁

.
├── ngr/
  ├── api/
    ├── v1/
      ├── bodies.py
  ├── db/
      ├── data.json
      ├── db.py
  ├── main.py
├── tests/
  ├── api/
  ├── conftest.py  
  • ngr/: Contains all the FastAPI source code.
    • api/: Contains the API routes, within the folder v1 contains the files for the routes of the API's first version.
    • db/: Contains handler for the fake DB. All data is contained on data.json generated with JSON-Generator.
  • tests/: Contains all the tests for the source code, it mimics the structure of ngr.

Commands 🛠

This project uses a Makefile to easily run commands for installation, format, lint, etc.

  • make venv: Generates a Python venv so you can install dependencies without making Python go crazy. Note that after running you need to run source venv/bin/activate to activate it.
  • make install: Installs all core dependencies of the project located in requirements.txt.
  • make install-dev: Installs all dependencies required to run project locally and run the tests, as well as core dependencies.
  • make test: Installs dependencies, lints and runs tests for the project.
  • make format: Formats all the files on the project using isort and black.
  • make lint: Lints project using flake8 and checks format.
  • make clean: Cleans all cached files and folders.

Deploy to Lambda 🚀

Follow my tutorial on Medium.