Skip to content

Instantly share code, notes, and snippets.

@asowder3943
Last active September 12, 2022 13:38
Show Gist options
  • Save asowder3943/b0652cda0040fe9def32d3d9dd36d58f to your computer and use it in GitHub Desktop.
Save asowder3943/b0652cda0040fe9def32d3d9dd36d58f to your computer and use it in GitHub Desktop.
fastapi cookiecutter walkthrough

fastapi cookiecutter walkthrough

This is a walkthrough of the steps taken to write a small application in fastapi using the fastapi cookiecutter

Tools Used

  1. cookicutter For Project templating
  2. cookiecutter-fastapi For project templating
  3. poetry For dependency management
  4. black for code formatting
  5. isort python import sorting and formatting

Running the Cookiecutter

Downloading cookiecutter dependencies

pip install cookiecutter black isort

Run the cookiecutter and Select Project Options image

As you can see there are a lot of options to choose from so lets back up and explain each one:

  • project_name Is the common name of your project
  • project_slug Is the name of your project as you would see it in a python import statement or URL with no special punctuation save underscores and hyphens
  • project_description Be specific but also brief. This will be used to initialize your README.md
  • author That's You! make sure to include an email you can be reached at by fellow contributors and users
  • copyright_year Choose the current year
  • license I always select MIT for public open source projects, but for a quick explanation of the differences see this explanation from attorney Joseph Morris
  • Selecting a Database is a much more important choice and will influence the way you build the rest of your application. The cookiecutter exposes two options to you tortoise and beanie. Tortoise is a traditional ORM for relational databases similar to the Django ORM, but with async support. This allows you to quickly map records in a SQL database to python objects. Whereas beanie is used with document-style databases such as MongoDB. I will be going forward with tortoise because I plan to use PostgreSQL later on in this example.
  • use_sentry Sentry is used for error monitoring and I will set up an example configuration later in this article
  • mail_service the cookiecutter includes templating for sending emails via SMTP. If you are an amazon SES user feel free to select this option but for this example, we will be using GMail
  • render_html Optionally use Jinja for HTML rendering. We will not be selecting this for our purposes. Spoiler we will be building a Vue frontend application for this API.
  • use_docker We will be using docker for our production deployment. This will containerize our application and make and standardize the environment in which our application will run.
  • use_heroku For our initial production deployments, we will be using Heroku

Exploring the Project template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment