Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python11 + Docker #4

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
[flake8]
max-line-length = 88
ignore =
E203, # See https://github.com/PyCQA/pycodestyle/issues/373
W503
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "pip" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
16 changes: 0 additions & 16 deletions .github/workflows/ci.yml

This file was deleted.

40 changes: 0 additions & 40 deletions .github/workflows/realworld-tests.yml

This file was deleted.

31 changes: 31 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Realworld API Tests

on: [push]

jobs:
pre-commit:
timeout-minutes: 2
runs-on: ubuntu-latest
steps:
- name: Checkout Project
uses: actions/checkout@v3
- name: Execute Static Tests
uses: pre-commit/[email protected]
with:
extra_args: --all-files
unit-tests:
timeout-minutes: 2
runs-on: ubuntu-latest
steps:
- name: Checkout Project
uses: actions/checkout@v3
- name: Execute Unit Tests
run: docker-compose up --build unit-tests
integration-tests:
timeout-minutes: 2
runs-on: ubuntu-latest
steps:
- name: Checkout Project
uses: actions/checkout@v3
- name: Execute Integration Tests
run: docker-compose up --build integration-tests
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

12 changes: 8 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ repos:
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/floatingpurr/sync_with_poetry
rev: "1.1.0"
hooks:
- id: sync_with_poetry
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v2.7.1"
hooks:
- id: prettier
- repo: https://github.com/psf/black
rev: 22.6.0
rev: 22.12.0
hooks:
- id: black
- repo: https://github.com/timothycrosley/isort.git
rev: "5.10.1"
rev: "5.13.2"
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.971
hooks:
- id: mypy
- repo: https://gitlab.com/pycqa/flake8.git
rev: 5.0.4
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
14 changes: 2 additions & 12 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
{
"python.formatting.provider": "black",
"editor.rulers": [88],
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.linting.mypyEnabled": true,
"python.envFile": "${workspaceFolder}/.env",
"python.pythonPath": "${workspaceFolder}/.venv/bin/python3.8",
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
},
"python.testing.pytestEnabled": true,
"editor.formatOnSave": true,
Expand All @@ -16,10 +11,5 @@
".pytest_cache/": true,
".mypy_cache/": true
},
"python.linting.mypyArgs": [
"--config-file=${workspaceFolder}/mypy.ini",
"--no-pretty"
],
"python.languageServer": "Pylance",
"python.linting.pydocstyleArgs": ["--config=./.pydocstyle"]
"python.languageServer": "Pylance"
}
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,35 @@
[![Maintainability](https://api.codeclimate.com/v1/badges/......)](https://codeclimate.com/repos/....)
-->

![Python: 3.10](https://img.shields.io/badge/python-3.10-informational.svg)
![Python: 3.11](https://img.shields.io/badge/python-3.11-informational.svg)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/python/black)
[![mypy: checked](https://img.shields.io/badge/mypy-checked-informational.svg)](http://mypy-lang.org/)
[![Manager: poetry](https://img.shields.io/badge/manager-poetry-blueviolet.svg)](https://poetry.eustace.io/)
[![Tests](https://github.com/art049/fastapi-odmantic-realworld-example/actions/workflows/tests.yml/badge.svg)](https://github.com/art049/fastapi-odmantic-realworld-example/actions/workflows/tests.yml)

</div>

> ### [FastAPI](https://github.com/tiangolo/fastapi) + [ODMantic](https://github.com/art049/odmantic) codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the [RealWorld](https://github.com/gothinkster/realworld) spec and API.

[![CI](https://github.com/art049/fastapi-odmantic-realworld-example/actions/workflows/ci.yml/badge.svg)](https://github.com/art049/fastapi-odmantic-realworld-example/actions/workflows/ci.yml)
[![Realworld Tests](https://github.com/art049/fastapi-odmantic-realworld-example/actions/workflows/realworld-tests.yml/badge.svg)](https://github.com/art049/fastapi-odmantic-realworld-example/actions/workflows/realworld-tests.yml)
> ### [FastAPI](https://github.com/tiangolo/fastapi) + [ODMantic](https://github.com/art049/odmantic) codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the [RealWorld](https://github.com/gothinkster/realworld) spec and API

## Getting Started

### :hammer: Installation

- [Install Docker](https://docs.docker.com/engine/install/) (necessary to run a local MongoDB instance)
- Make sure Python 3.10 is available on your system
- Install [poetry](https://poetry.eustace.io/)
- Setup the environment `./scripts/setup.sh`
- [Install Docker](https://docs.docker.com/engine/install/)
- [Install Compose](https://docs.docker.com/compose/install/)
- Build and run: `docker-compose up api`

### :bulb: Useful scripts

- Start the MongoDB instance `./scripts/start-mongo.sh`
- Stop the MongoDB instance `./scripts/stop-mongo.sh`
- Start the FastAPI server `./scripts/start.sh`
- Format the code `./scripts/format.sh`
- Manually run the linter `./scripts/lint.sh`
- Manually run the tests `./scripts/test.sh`
- Unit tests: `docker-compose up unit-tests`
- Integration tests: `docker-compose up integration-tests`
- Pre-commit setup: `docker-compose up pre-commit`

## Coming Soon

- [ ] Articles with details on every single step required to build this app
- [ ] Testing
- [ ] Live reloading
- [ ] Unit Testing with coverage
- [ ] Debug vscode
- [ ] Deployment on AWS with MongoDB Atlas
60 changes: 60 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
services:
api:
build: .
expose:
- 8000
environment:
- MONGO_URI=mongodb://mongodb:27017/realworld
ports:
- "8000:8000"
networks:
- realworld
depends_on:
- mongodb

mongodb:
image: mongo:6-jammy
ports:
- "27017:27017"
volumes:
- mongodb:/data/db
networks:
- realworld

unit-tests:
build:
dockerfile: dockerfile.dev
context: .
volumes:
- ./:/app/
entrypoint: ["poetry", "run", "pytest", "tests"]
profiles: [tests]

integration-tests:
build:
context: ./tests/integration
environment:
- APIURL=http://api:8000
depends_on:
- api
networks:
- realworld
profiles: [tests]

pre-commit:
build:
dockerfile: dockerfile.dev
context: .
volumes:
- ./:/app/
- pre-commit:/root/.cache/pre-commit/
entrypoint: ["poetry", "run", "pre-commit", "run", "--all-files"]
profiles: [pre-commit]

volumes:
pre-commit:
mongodb:

networks:
realworld:
driver: bridge
18 changes: 18 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.11-slim-buster

RUN pip install poetry==1.7.1

ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=0 \
POETRY_VIRTUALENVS_CREATE=0 \
POETRY_CACHE_DIR=/tmp/poetry_cache

WORKDIR /app

COPY pyproject.toml poetry.lock ./

COPY src ./src

RUN poetry install --without dev --no-root

ENTRYPOINT ["poetry", "run", "uvicorn", "--host", "0.0.0.0", "--app-dir", "./src/", "api:app"]
21 changes: 21 additions & 0 deletions dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.11-slim-buster

RUN apt-get update \
&& apt-get install -y --no-install-recommends git \
&& apt-get purge -y --auto-remove \
&& rm -rf /var/lib/apt/lists/

RUN pip install poetry==1.7.1

ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=0 \
POETRY_VIRTUALENVS_CREATE=0 \
POETRY_CACHE_DIR=/tmp/poetry_cache

WORKDIR /app

RUN git config --global --add safe.directory /app

COPY pyproject.toml poetry.lock ./

RUN poetry install --no-root
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
python_version=3.10
python_version=3.11
pretty=True
ignore_missing_imports = True
namespace_packages=True
Expand Down
Loading