Skip to content

Commit 84f2452

Browse files
committed
initial commit
0 parents  commit 84f2452

45 files changed

Lines changed: 3331 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
APPLICATION_HOST=127.0.0.1
2+
APPLICATION_PORT=3000
3+
4+
APPLICATION_ROOT=

.gitignore

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/

.pre-commit-config.yaml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
repos:
2+
- repo: https://github.com/python-poetry/poetry
3+
rev: "1.7.0"
4+
hooks:
5+
- id: poetry-check
6+
args: [--directory=backend]
7+
- id: poetry-lock
8+
args: [--directory=backend]
9+
- id: poetry-install
10+
args: [--directory=backend]
11+
- repo: https://github.com/pre-commit/pre-commit-hooks
12+
rev: v4.5.0
13+
hooks:
14+
- id: trailing-whitespace
15+
- id: end-of-file-fixer
16+
exclude: ^.*\.egg-info/
17+
- id: check-merge-conflict
18+
- id: check-case-conflict
19+
- id: check-json
20+
- id: check-toml
21+
exclude: tests/fixtures/invalid_lock/poetry\.lock
22+
- id: check-yaml
23+
- id: check-ast
24+
- id: debug-statements
25+
- id: check-docstring-first
26+
27+
- repo: https://github.com/psf/black
28+
rev: 23.12.0
29+
hooks:
30+
- id: black
31+
language_version: python3.9
32+
args:
33+
[
34+
--line-length=88,
35+
--target-version=py311,
36+
--exclude=\.venv/,
37+
--exclude=\.nox/,
38+
--exclude=\.tox/,
39+
--exclude=\.eggs/,
40+
--exclude=build/,
41+
--exclude=dist/,
42+
--exclude=docs/,
43+
--exclude=\.mypy_cache/,
44+
--exclude=\.pytest_cache/,
45+
--exclude=\.git/,
46+
--exclude=\.github/,
47+
--exclude=\.vscode/,
48+
--exclude=\.pyright/,
49+
--exclude=\.pylint_cache/,
50+
--exclude=\.hypothesis/,
51+
--exclude=\.mypy/,
52+
--exclude=\.coverage/,
53+
]
54+
55+
- repo: https://github.com/pycqa/flake8
56+
rev: 6.1.0
57+
hooks:
58+
- id: flake8
59+
args: [--max-line-length=88, --ignore=E203]
60+
61+
- repo: https://github.com/myint/autoflake
62+
rev: v2.2.1
63+
hooks:
64+
- id: autoflake
65+
args:
66+
[
67+
"--in-place",
68+
"--remove-all-unused-imports",
69+
"--remove-unused-variables",
70+
"--remove-duplicate-keys",
71+
".",
72+
]
73+
74+
- repo: https://github.com/pycqa/isort
75+
rev: 5.13.2
76+
hooks:
77+
- id: isort
78+
args: [--filter-files, --profile=black, --src-path=src, --gitignore]
79+
80+
- repo: https://github.com/pre-commit/mirrors-prettier
81+
rev: v4.0.0-alpha.6
82+
hooks:
83+
- id: prettier
84+
additional_dependencies:
85+
- prettier@2.8.1
86+
87+
- repo: https://github.com/pre-commit/pre-commit
88+
rev: v3.6.0
89+
hooks:
90+
- id: validate_manifest
91+
92+
- repo: https://github.com/astral-sh/ruff-pre-commit
93+
rev: v0.1.8
94+
hooks:
95+
- id: ruff
96+
97+
- repo: local
98+
hooks:
99+
- id: export-requirements
100+
name: Export requeriments.txt
101+
language: system
102+
pass_filenames: false
103+
entry: poetry export --without-hashes -o requirements.txt -f requirements.txt
104+
files: ^(pyproject\.toml|poetry\.lock)$

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.9.18

Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
ifneq ("$(wildcard .env)","")
2+
include .env
3+
export
4+
endif
5+
6+
7+
.PHONY: run
8+
run: ## Run the project.
9+
poetry run python -m api
10+
11+
.PHONY: install
12+
install: ## Install Python requirements.
13+
python -m pip install --upgrade pip setuptools wheel poetry
14+
poetry lock
15+
poetry install --no-root
16+
poetry run pre-commit install
17+
18+
.PHONY: test
19+
test: ## Run tests.
20+
ENVIRONMENT=test poetry run pytest --cov
21+
22+
.PHONY: pre-commit
23+
pre-commit: ## Run pre-commit checks.
24+
poetry run pre-commit run --config ./.pre-commit-config.yaml
25+
26+
.PHONY: patch
27+
patch: ## Bump project version to next patch (bugfix release/chores).
28+
poetry version patch
29+
30+
.PHONY: minor
31+
minor: ## Bump project version to next minor (feature release).
32+
poetry version minor
33+
34+
.PHONY: clean
35+
clean: ## Clean project's temporary files.
36+
find . -name '__pycache__' -exec rm -rf {} +
37+
find . -name '*.pyc' -exec rm -f {} +
38+
find . -name '*.log' -exec rm -f {} +
39+
40+
.DEFAULT_GOAL := help
41+
help:
42+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sed 's/Makefile://g' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# fastapi-backend-template
2+
3+
A FastAPI backend template for a head start on new projects.
4+
5+
See `Makefile` for examples on how to run the project.

api/__init__.py

Whitespace-only changes.

api/__main__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import uvicorn
2+
from api.config import Config
3+
4+
5+
def main() -> None:
6+
"""Entrypoint of the application."""
7+
uvicorn.run(
8+
"api.app:get_app",
9+
workers=Config.WORKERS_COUNT,
10+
host=Config.HOST,
11+
port=Config.PORT,
12+
reload=Config.RELOAD,
13+
log_level=Config.LOG_LEVEL.lower(),
14+
factory=True,
15+
)
16+
17+
18+
if __name__ == "__main__":
19+
main()

api/app.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from pathlib import Path
2+
3+
from api.entrypoints.router import api_router
4+
from fastapi import FastAPI
5+
from fastapi.middleware.cors import CORSMiddleware
6+
from fastapi.responses import JSONResponse
7+
from fastapi_cache import FastAPICache
8+
from fastapi_cache.backends.inmemory import InMemoryBackend
9+
10+
APP_ROOT = Path(__file__).parent
11+
12+
13+
def get_app() -> FastAPI:
14+
"""
15+
Get FastAPI application.
16+
17+
This is the main constructor of an application.
18+
19+
:return: application.
20+
"""
21+
_app = FastAPI(
22+
title="fastapi-backend-template",
23+
default_response_class=JSONResponse,
24+
)
25+
26+
_app.add_middleware(
27+
CORSMiddleware,
28+
allow_origins=["*"],
29+
allow_credentials=True,
30+
allow_methods=["*"],
31+
allow_headers=["*"],
32+
)
33+
_app.include_router(router=api_router)
34+
35+
FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
36+
return _app

0 commit comments

Comments
 (0)