Skip to content

Development ‐ Backend

Linda Zhu edited this page Apr 27, 2024 · 6 revisions

Griddle's backend is an OpenAPI-enabled HTTP API written in python using FastAPI.

Development

Recommended IDE Setup

Requirements

  • Python 3.10
    • recommend using pyenv for managing python versions
  • pipenv
    • (python -m pip install --user pipenv)
  • Docker
  • (if on Unix system) postgres libraries

Initial setup

We use FastAPI for our backend. To get started, cd into this backend folder, and create a virtualenv with our dependencies using pipenv install.

Running a development server

Make sure to spin up our development docker-compose.yml. To start up both a local postgres database and localstack (s3 simulator) run:

docker-compose up -d

Enter a shell using pipenv shell.

To start a dev server, run:

uvicorn main:app --reload

Troubleshooting

After the initial run of the commands above, you might encounter issues with dependency modules missing. Try running the commands below: (Make sure you are in virtualenv!)

pipenv requirements > requirements.txt
pip install -r requirements.txt

If there're still erros with modules such as uvicorn and bcrypt, pip install these packages individually, and then try starting the server.

Contributing

We use a couple libraries to streamline the creation of our backend API.

  • FastAPI to build routes and handlers. This automatically generates OpenAPI docs which can be found at localhost:8000/docs. Most things you'd want to do are covered in their docs.

  • SQLAlchemy to model and access our database.

  • Alembic to auto-generate and run database migrations. These are kept in the migrations folder.

  • boto3 to access S3 (or in our deployed case Cloudflare R2).

  • sqladmin to view the DB with a nice web GUI. Can be found at localhost:8000/admin.

Here are some key files to get started:

Migrations

When you're ready to make a full migration (change in the DB models) run the following in your virtualenv:

alembic revision --autogenerate

Then, commit the resulting autogenerated file in migrations.