Skip to content

Latest commit

 

History

History
116 lines (74 loc) · 3.2 KB

README.md

File metadata and controls

116 lines (74 loc) · 3.2 KB

fastapi_template

Features

  • FastAPI
  • React Admin
  • SQLAlchemy and Alembic
  • Pre-commit hooks (black, autoflake, isort, flake8, prettier)
  • Github Action
  • Dependabot config
  • Docker images

Good to know

The frontend of this project uses React Admin. Follow the quick tutorial to understand how React Admin works.

Step 1: Getting started

Start a local development instance with docker-compose

docker-compose up -d

# Run database migration
docker-compose exec backend alembic upgrade head

Now you can navigate to the following URLs:

Step 2: Setup pre-commit hooks and database

Keep your code clean by using the configured pre-commit hooks. Follow the instructions here to install pre-commit. Once pre-commit is installed, run this command to install the hooks into your git repository:

pre-commit install

Local development

The backend setup of docker-compose is set to automatically reload the app whenever code is updated. However, for frontend it's easier to develop locally.

docker-compose stop frontend
cd frontend
yarn
yarn start

If you want to develop against something other than the default host, localhost:8000, you can set the REACT_APP_API_BASE environment variable:

export REACT_APP_API_BASE=http://mydomain.name:8000
yarn start

Don't forget to edit the .env file and update the BACKEND_CORS_ORIGINS value (add http://mydomain:3000 to the allowed origins).

Rebuilding containers

If you add a dependency, you'll need to rebuild your containers like this:

docker-compose up -d --build

Regenerate front-end API package

Instead of writing frontend API client manually, OpenAPI Generator is used. Typescript bindings for the backend API can be recreated with this command:

yarn genapi

Database migrations

These two are the most used commands when working with alembic. For more info, follow through Alembic's tutorial.

# Auto generate a revision
docker-compose exec backend alembic revision --autogenerate -m 'message'

# Apply latest changes
docker-compose exec backend alembic upgrade head

Backend tests

The Backend service uses a hardcoded database named apptest. First, ensure that it's created

docker-compose exec postgres createdb apptest -U postgres

Then you can run tests with this command:

docker-compose run backend pytest --cov --cov-report term-missing

Single docker image

There's a monolith/single docker image that uses FastAPI to serve static assets. You can use this image to deploy directly to Heroku, Fly.io or anywhere where you can run a Dockerfile without having to build a complicated setup out of separate frontend and backend images.

Recipes

Build and upload docker images to a repository

Configure the build-push-action in .github/workflows/test.yaml.

Credits

Created with FastAPI Starter