blob: 0790dcb54db1d491c6eb7f56409d70a617b11944 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# fastapi-poetry-starter
<!-- TOC -->
* [fastapi-poetry-starter](#fastapi-poetry-starter)
* [Description](#description)
* [Prerequisites](#prerequisites)
* [1. Install Python 3 and Poetry](#1-install-python-3-and-poetry)
* [2. Create a virtual environment with all necessary dependencies](#2-create-a-virtual-environment-with-all-necessary-dependencies)
* [3. Activate your virtual environment](#3-activate-your-virtual-environment)
* [Run application](#run-application)
* [Testing](#testing)
* [With coverage](#with-coverage)
* [With coverage and HTML output](#with-coverage-and-html-output)
* [Linting](#linting)
* [Formatting](#formatting)
* [Containerisation](#containerisation)
* [1. Build image and tag it as `fastapi-poetry-starter`](#1-build-image-and-tag-it-as-fastapi-poetry-starter)
* [2. Run a container of the previously tagged image (`fastapi-poetry-starter`)](#2-run-a-container-of-the-previously-tagged-image-fastapi-poetry-starter)
* [3. Check running containers](#3-check-running-containers)
* [4. Hit sample endpoint](#4-hit-sample-endpoint)
<!-- TOC -->
## Description
A project starter for personal usage containing the following:
- [Python 3.12.\*](https://www.python.org/)
- [FastAPI](https://fastapi.tiangolo.com/) web framework
- Structured logging using [`structlog`](https://www.structlog.org/)
- Dependency management using [`poetry`](https://python-poetry.org/)
- Containerisation using a Dockerfile
- Testing with [`pytest`](https://docs.pytest.org/) and optionally with coverage
with [`pytest-cov`](https://pytest-cov.readthedocs.io/)
- Linting/formatting using [`ruff`](https://beta.ruff.rs/docs/)
- [`.gitignore`](https://github.com/github/gitignore/blob/main/Python.gitignore)
## Prerequisites
- [Python 3.12.\*](https://www.python.org/downloads/)
- [Poetry](https://python-poetry.org/)
### 1. Install Python 3 and Poetry
**MacOS (using `brew`)**
```bash
brew install python3 poetry
```
**Ubuntu/Debian**
```bash
sudo apt install python3 python3-venv pipx
pipx ensurepath
pipx install poetry
```
### 2. Create a virtual environment with all necessary dependencies
From the root of the project execute:
```bash
poetry install
```
### 3. Activate your virtual environment
From the root of the project execute:
```bash
poetry shell
```
## Run application
Runs the FastAPI web application on port `8000` using [uvicorn](https://www.uvicorn.org/):
```bash
uvicorn fastapi_poetry_starter.main:app --reload
```
## Testing
```bash
pytest
```
### With coverage
```bash
pytest --cov=app
```
### With coverage and HTML output
```bash
pytest --cov-report html --cov=app
```
## Linting
```bash
ruff check fastapi_poetry_starter/* tests/*
```
## Formatting
```bash
ruff format fastapi_poetry_starter/* tests/*
```
## Containerisation
The following `podman` commands are direct replacements of the Docker CLI. You can see that their syntax is identical:
### 1. Build image and tag it as `fastapi-poetry-starter`
```bash
podman image build -t fastapi-poetry-starter .
```
### 2. Run a container of the previously tagged image (`fastapi-poetry-starter`)
Run our FastAPI application and map our local port `8000` to `80` on the running container:
```bash
podman container run -d --name fastapi-poetry-starter -p 8000:80 --network bridge fastapi-poetry-starter
```
### 3. Check running containers
```bash
podman ps
```
```bash
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78586e5b4683 localhost/fastapi-poetry-starter:latest uvicorn main:app ... 13 minutes ago Up 5 minutes ago 0.0.0.0:8000->80/tcp nifty_roentgen
```
### 4. Hit sample endpoint
Our FastAPI server now runs on port `8000` on our local machine. We can test it with:
```bash
curl -i http://localhost:8000/healthcheck
```
Output:
```bash
HTTP/1.1 200 OK
server: uvicorn
content-length: 0
```
|