https://github.com/zhiwei2017/fastapi-cookiecutter
A cookiecutter template for FastAPI projects
https://github.com/zhiwei2017/fastapi-cookiecutter
cicd cookiecutter-template fastapi-template oauth2 python3
Last synced: 3 months ago
JSON representation
A cookiecutter template for FastAPI projects
- Host: GitHub
- URL: https://github.com/zhiwei2017/fastapi-cookiecutter
- Owner: zhiwei2017
- License: mit
- Created: 2020-11-25T17:52:35.000Z (almost 5 years ago)
- Default Branch: develop
- Last Pushed: 2024-02-03T21:26:00.000Z (over 1 year ago)
- Last Synced: 2025-05-19T10:07:04.722Z (5 months ago)
- Topics: cicd, cookiecutter-template, fastapi-template, oauth2, python3
- Language: Python
- Homepage: https://zhiwei2017.github.io/FastAPI-Cookiecutter/
- Size: 2.7 MB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Contributing: CONTRIBUTING.rst
- License: LICENSE
Awesome Lists containing this project
README
FastAPI Cookiecutter Template
=============================Introduction
------------
This projects consist of a `cookiecutter`_ template that generates a full structure
for creating a RESTful API service project based on FastAPI following the MVC
(Model-View-Controller) structure in the same way Django projects does.While using this project, you will be asked to provide some inputs such the authors, the name of the project, etc. As result you will obtain the
complete file and folder structure to quickly start to code your project.Prerequisites
-------------
It uses ``Python`` (>=3.7) behind the scenes. Please install the Python package `cookiecutter`_ before using it.Project Generation Options
--------------------------project_name:
Your project's human-readable name, capitals and spaces allowed.project_slug:
Your project's slug without dashes or spaces. Used to name your repo
and in other places where a Python-importable version of your project name
is needed.project_url:
The url of your project hosted in gitlab or github.author:
This is you! The value goes into places like ``README.md`` and such.email:
The email address you want to identify yourself in the project.short_description:
Describes your project briefly and gets used in places like ``README.md`` and ``setup.py``.version:
The version of the project at its inception.use_oauth:
Indicates whether the project should be configured for using OAuth2 authentication. The choices are:1. No
2. Yesuse_database:
Indicates whether the project should be configured for using database. The choices are:1. No
2. Yesci_tool:
Select a CI tool. The choices are:1. GitHub
2. GitLab
3. Bitbucket
4. Noneci_tool:
Select the python version for configuring the created project's CI/CD pipeline. The choices are:1. 3.10
2. 3.11
3. 3.9
4. 3.8
5. 3.7Tutorial
--------
Let's pretend you want to create a FastAPI project called "redditclone".
By using this template based on `cookiecutter`_,
you will be able to quickly setup a runnable FastAPI project.First, get Cookiecutter. Trust me, it's awesome::
$ pip install "cookiecutter>=1.7.0"
Now run it against this repo::
$ cookiecutter https://github.com/zhiwei2017/FastAPI-Cookiecutter.git
You'll be prompted for some values. Provide them, then a FastAPI project will be created for you.
**Warning**: After this point, change 'My Awesome Project', 'John Doe', etc to your own information.
Answer the prompts with your own desired `Project Generation Options <./docs/source/02_prompts.rst>`_. For example::
Cloning into 'fastapi-cookiecutter'...
remote: Enumerating objects: 219, done.
remote: Counting objects: 100% (219/219), done.
remote: Compressing objects: 100% (123/123), done.
remote: Total 219 (delta 83), reused 181 (delta 69), pack-reused 0
Receiving objects: 100% (219/219), 41.09 KiB | 1.71 MiB/s, done.
Resolving deltas: 100% (83/83), done.
project_name [My Awesome Project]: Reddit Clone
project_slug [reddit_clone]: reddit_clone
project_url [https://github.com/example_project]: https://github.com/redditclone
author [John Doe]: John Doe
email [john-doe@example.com]: john.doe@example.com
short_description [Behold My Awesome Project!]: A reddit clone.
version [0.0.1]: 0.1.0
Select use_oauth:
1 - No
2 - Yes
Choose from 1, 2 [1]: 1
Select use_database:
1 - Yes
2 - No
Choose from 1, 2 [1]: 1
Select ci_tool:
1. GitHub
2. GitLab
3. Bitbucket
4. None
Choose from 1, 2 [1]: 1
Select python_version:
1. 3.10
2. 3.11
3. 3.9
4. 3.8
5. 3.7
Choose from 1, 2 [1]: 1Enter the project and take a look around::
$ cd reddit_clone/
$ lsNow take a look at your repo. Don't forget to carefully look at the generated **README**.
Project Structure
-----------------Files related to application are in the ``src`` or ``tests`` directories.
Application components are::{{cookiecutter.project_slug}}
├── docs - sphinx documentastion
├── scripts - scripts
│ └── prestart.sh
├── {{cookiecutter.project_slug}}
│ ├── app
│ │ ├── api - api endpoints
│ │ │ └── base.py - basic endpoints
│ │ ├── application.py - function for FastAPI application creation and configuration
│ │ ├── configs - application configuration
│ │ │ └── base.py - basic configuration class
│ │ ├── constants.py - constants used inside the application
│ │ ├── db - database related stuff
│ │ │ ├── base.py - base sqlalchemy DB model class
│ │ │ ├── models - folder for defining sqlalchemy DB model classes
│ │ │ ├── queries - folder for predefined sqlalchemy queries
│ │ │ └── session.py - a local session instance used inside application
│ │ ├── events - events for startup, shutdown
│ │ │ └── base.py - dummy startup event and shudown event
│ │ ├── globals.py - global variables
│ │ ├── middlewares - middleware stuff
│ │ │ └── logging.py - middleware function related to logging
│ │ ├── schemas - pydantic models for this application
│ │ │ └── base.py - pydantic models for the basic endpoints
│ │ ├── services - logic that is not included in the other folders
│ │ ├── utils - utility stuff
│ │ │ ├── errors.py - customized exception classes
│ │ │ ├── logging.py - logging related utility functions, classes
│ │ │ └── security.py - security related functions, classes
│ │ └── version.py - version information
│ ├── data - data used for this application
│ └── main.py - main function to run the application
├── tests - unit tests
│ ├── conftest.py - fixtures in tests
│ ├── resources - resources used in tests
│ └── ...
├── Dockerfile - docker file for building docker image
├── Makefile - predefined commands
├── README.md - package information
├── requirements - package dependencies
│ ├── base.txt - documentation dependecies
│ ├── doc.txt - documentation dependecies
│ ├── dev.txt - tests dependencies
├── setup.cfg - configurations for mypy, bandit, pytest etc. Centralizing all the configurations to one place.
└── setup.py - package installation configurationAdvanced Usage
--------------
``Gunicorn`` Configuration
**************************
A default ``gunicorn-conf.py`` file is included in the docker image and will be
executed before your service is up. It supports configuration through environment
variables. Please check the section `Environment variables `_
from `uvicorn-gunicorn-docker`_ project
for more detailed information. The suggested approach for defining environment variables
is to use the ``scrtips/prestart.sh`` file.In case you need to customize the ``Gunicorn`` configuration file, please check the
default `gunicorn-conf.py `_ file
and read the section `Custom Gunicorn configuration file `_
from `uvicorn-gunicorn-docker`_ project firstly.Customize ``prestart`` Hook
***************************
If you need to run anything before starting the app, you can add a file ``prestart.sh`` to the directory ``scripts``.
Please check the section `Custom /app/prestart.sh `_
from `uvicorn-gunicorn-docker`_ project for more details.Access Token
************
An access token creation function is provided. However, it's not used by fault.
To use it, please check the `example `_.Contributing Guide
------------------Please check the `Contributing Guide `_ for details.
Core Team
---------* `Zhiwei Zhang `_ - *Author* / *Maintainer* - `zhiwei2017@gmail.com `_
Literature
----------+ `cookiecutter`_
+ `FastAPI `_
+ `Pydantic `_
+ `SQLAlchemy `_
+ `Alembic `_
+ `PyJWT `_
+ `python-jose `_.. _`cookiecutter`: https://github.com/cookiecutter/cookiecutter
.. _uvicorn-gunicorn-docker: https://github.com/tiangolo/uvicorn-gunicorn-docker