An open API service indexing awesome lists of open source software.

https://github.com/smarlhens/python-fastapi-hello-word

FastAPI hello-world example API using uvicorn.
https://github.com/smarlhens/python-fastapi-hello-word

fastapi hello-world python uvicorn

Last synced: 8 months ago
JSON representation

FastAPI hello-world example API using uvicorn.

Awesome Lists containing this project

README

          


FastAPI logo
uvicorn logo


Poetry logo
pre-commit logo
isort logo
bandit logo
pytest logo


Docker logo
GitHub Actions logo

# Python FastAPI Hello World

[![CodeQL](https://github.com/smarlhens/python-fastapi-hello-word/workflows/codeql/badge.svg)](https://github.com/smarlhens/python-fastapi-hello-word/actions/workflows/codeql.yml)
[![GitHub CI](https://github.com/smarlhens/python-fastapi-hello-word/workflows/ci/badge.svg)](https://github.com/smarlhens/python-fastapi-hello-word/actions/workflows/ci.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/smarlhens/python-fastapi-hello-word/main.svg)](https://results.pre-commit.ci/latest/github/smarlhens/python-fastapi-hello-word/main)
[![GitHub license](https://img.shields.io/github/license/smarlhens/python-fastapi-hello-word)](https://github.com/smarlhens/python-fastapi-hello-word)

---

## Table of Contents

- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [What's in the box ?](#whats-in-the-box-)
- [Testing](#testing)
- [Docker](#docker)

---

## Prerequisites

- [Python](https://www.python.org/downloads/) **>=3.10 <3.11** (_tested with 3.10.5_)
- [pre-commit](https://pre-commit.com/#install)
- [docker](https://docs.docker.com/get-docker/) (_optional_)

---

## Installation

1. Clone the git repository

```bash
git clone https://github.com/smarlhens/python-fastapi-hello-word.git
```

2. Go into the project directory

```bash
cd python-fastapi-hello-word/
```

3. Checkout working branch

```bash
git checkout
```

4. Enable pre-commit hooks

```bash
pre-commit install
```

5. Start FastAPI app

```bash
uvicorn myservice.main:app --host 0.0.0.0 --proxy-headers --forwarded-allow-ips='*' --port 8000
```

6. Navigate to docs: [http://0.0.0.0:8000/docs](http://0.0.0.0:8000/docs)

---

## What's in the box ?

### Poetry

[Poetry](https://python-poetry.org/) is a tool for dependency management and packaging in Python. It allows you to
declare the libraries your project depends on and it will manage (install/update) them for you.

**pyproject.toml file** ([`pyproject.toml`](pyproject.toml)): orchestrate your project and its dependencies
**poetry.lock file** ([`poetry.lock`](poetry.lock)): ensure that the package versions are consistent for everyone
working on your project

For more configuration options and details, see the [configuration docs](https://python-poetry.org/docs/).

### pre-commit

[pre-commit](https://pre-commit.com/) is a framework for managing and maintaining multi-language pre-commit hooks.

**.pre-commit-config.yaml file** ([`.pre-commit-config.yaml`](.pre-commit-config.yaml)): describes what repositories and
hooks are installed

For more configuration options and details, see the [configuration docs](https://pre-commit.com/).

### flake8

[flake8](https://flake8.pycqa.org/) is a tool for style guide enforcement.

Rules are defined in the [`setup.cfg`](setup.cfg).

For more configuration options and details, see the [configuration docs](https://flake8.pycqa.org/).

### mypy

[mypy](http://mypy-lang.org/) is an optional static type checker for Python that aims to combine the benefits of
dynamic (or "duck") typing and static typing.

Rules are defined in the [`pyproject.toml`](pyproject.toml).

For more configuration options and details, see the [configuration docs](https://mypy.readthedocs.io/).

### isort

[isort](https://pycqa.github.io/isort/) is tool that sort your imports, so you don't have to.

Rules are defined in the [`pyproject.toml`](pyproject.toml).

For more configuration options and details, see the [configuration docs](https://pycqa.github.io/isort/).

### black

[black](https://black.readthedocs.io/) is an uncompromising code formatter.

Rules are defined in the [`pyproject.toml`](pyproject.toml).

For more configuration options and details, see the [configuration docs](https://black.readthedocs.io/).

### bandit

[bandit](https://bandit.readthedocs.io/) is a tool designed to find common security issues in Python code.

Rules are defined in the [`pyproject.toml`](pyproject.toml).

For more configuration options and details, see the [configuration docs](https://bandit.readthedocs.io/).

### docformatter

[docformatter](https://github.com/PyCQA/docformatter) is a tool designed to format docstrings to
follow [PEP 257](https://peps.python.org/pep-0257/).

Options are defined in the [`.pre-commit-config.yaml`](.pre-commit-config.yaml).

---

## Testing

We are using [pytest](https://docs.pytest.org/) & [pytest-cov](https://github.com/pytest-dev/pytest-cov) to write tests.

To run tests:

```bash
poetry run pytest tests
```

Output

```text
collected 3 items

tests/test_myservice.py::test_read_root PASSED [ 33%]
tests/test_myservice.py::test_read_item_with_id_and_query PASSED [ 66%]
tests/test_myservice.py::test_read_item_with_id_without_query PASSED [100%]
```

To run tests with coverage:

```bash
poetry run pytest tests --cov=src
```

Output

```text
collected 3 items

tests/test_myservice.py::test_read_root PASSED [ 33%]
tests/test_myservice.py::test_read_item_with_id_and_query PASSED [ 66%]
tests/test_myservice.py::test_read_item_with_id_without_query PASSED [100%]

---------- coverage: platform linux, python 3.10.4-final-0 -----------
Name Stmts Miss Cover
-----------------------------------------------
src/myservice/__init__.py 1 0 100%
src/myservice/main.py 15 0 100%
src/myservice/settings.py 8 0 100%
-----------------------------------------------
TOTAL 24 0 100%
```

---

## Docker

### Build

To build the docker `production` image using [`Dockerfile`](Dockerfile):

```bash
docker build . -t my-fastapi-application:latest
```

To build the docker `development` image using [`Dockerfile`](Dockerfile):

```bash
docker build . --target development -t my-fastapi-application:dev
```

### Run

To run the FastAPI production app example using Docker:

```bash
docker run -p 8000:8000 -it --rm my-fastapi-application:latest # or :dev for development
```

### Execute command inside container

```bash
docker run -p 8000:8000 -it --rm --entrypoint /bin/bash my-fastapi-application:lastest # or :dev for development
```

---