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

feat(chore): setup all project #1

Merged
merged 3 commits into from
Sep 8, 2024
Merged
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
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
92 changes: 92 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
venv/
ENV/
env.bak/
venv.bak/
.venv/
pip-wheel-metadata/
*.egg-info/
.installed.cfg
*.egg
*.whl
*.manifest
*.spec

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.cache/
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
tests/__pycache__/

# Jupyter Notebook
.ipynb_checkpoints/
*.ipynb

# PyCharm
.idea/
*.iml

# Environments
.env
.env.local
.env.test
.env.production
*.env

# macOS
.DS_Store

# Visual Studio Code
.vscode/
*.code-workspace

# MyPy
.mypy_cache/

# Pyre type checker
.pyre/

# Profiling data
.prof

# VSCode
.vscode/
*.code-workspace

# JetBrains IDEs
.idea/

# Sublime Text
.sublime/

# Anaconda
.conda/
/.idea/*
133 changes: 132 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,132 @@
# fastapi-starter
[![CI](https://github.com/simonmacor/fastapi-starter/actions/workflows/ci.yml/badge.svg)](https://github.com/simonmacor/fastapi-starter/actions/workflows/ci.yml)

# FastAPI Web Starter Kit

**FastAPI Web Starter Kit** – a base project designed to help you develop web applications using Python and the **FastAPI** framework. This starter kit is built to simplify backend development with a pre-configured architecture for testing, database management, and mocking third-party services.

## Features

- **FastAPI Framework**: A modern and fast framework for building web APIs with Python.
- **BDD Testing with Behave**: Use behavior-driven development (BDD) for scenario-based testing.
- **Unit Testing with Pytest**: Pre-configured for unit and functional tests.
- **SQLAlchemy**: ORM (Object-Relational Mapper) for database interaction.
- **Alembic**: A database schema migration tool for managing schema changes.
- **Mountebank**: Mock third-party services with imposters for integration testing.
- **PostgreSQL**: Latest version of PostgreSQL as the default database.
- **Adminer**: A web-based tool to easily manage the PostgreSQL database.

## Prerequisites

- **Docker** and **Docker Compose** must be installed on your machine.

## Installation

1. Clone this repository:

```bash
git clone https://github.com/your-project/fastapi-web-starter-kit.git
cd fastapi-web-starter-kit
```

2. Install Python dependencies (if working locally without Docker):

```bash
python3 -m venv env
source env/bin/activate
pip install -r requirements.txt
```

3. Start the application using Docker Compose:

```bash
docker-compose up --build
```

4. Access the FastAPI application at [http://localhost:8000](http://localhost:8000).
5. Access **Adminer** to manage the database at [http://localhost:8080](http://localhost:8080).

## Important Files

- **`requirements.txt`**: Contains the necessary Python libraries.
- [FastAPI Documentation](https://fastapi.tiangolo.com/)
- [Behave Documentation](https://behave.readthedocs.io/en/latest/)
- [Pytest Documentation](https://docs.pytest.org/en/6.2.x/)
- [SQLAlchemy Documentation](https://docs.sqlalchemy.org/en/14/)
- [Alembic Documentation](https://alembic.sqlalchemy.org/en/latest/)

- **`docker-compose.yml`**: Docker configuration for the application, including PostgreSQL, Adminer, and Mountebank.

- **`alembic/`**: Directory for database migrations using Alembic.

- **`tests/`**: Directory for unit, functional, and BDD tests.

- **`imposters.ejs`**: Configuration file for Mountebank, allowing you to mock third-party services.

## Included Services

### FastAPI

FastAPI is used to create fast and performant RESTful APIs. The application is available at [http://localhost:8000](http://localhost:8000).

- [FastAPI Documentation](https://fastapi.tiangolo.com/)

### PostgreSQL and Adminer

PostgreSQL is the database configured with the latest version, and you can manage it via **Adminer** at [http://localhost:8080](http://localhost:8080).

- [PostgreSQL Documentation](https://www.postgresql.org/docs/)
- [Adminer Documentation](https://www.adminer.org/)

### Mountebank

Mountebank is configured to simulate third-party services and test API integrations. Imposter configuration is stored in the `imposters.ejs` file.

- [Mountebank Documentation](http://www.mbtest.org/docs/gettingStarted)

### Testing

- **BDD with Behave**: BDD scenarios are defined in the `features/` directory.
- [Behave Documentation](https://behave.readthedocs.io/en/latest/)

- **Unit and Functional Testing with Pytest**: Traditional tests are located in the `tests/` directory.
- [Pytest Documentation](https://docs.pytest.org/en/6.2.x/)

### Database Migrations

Database schema migrations are managed via Alembic. Use the following commands to create and apply migrations:

```bash
# Create a new migration
alembic revision --autogenerate -m "Migration name"

# Apply migrations
alembic upgrade head
```

- [Alembic Documentation](https://alembic.sqlalchemy.org/en/latest/)

## Useful Commands

- **Start the application:**

```bash
docker-compose up --build
```

- **Apply database migrations:**

```bash
docker-compose exec app alembic upgrade head
```

- **Run unit and functional tests:**

```bash
docker-compose exec app pytest
```

- **Run BDD tests:**

```bash
docker-compose exec app behave
```
40 changes: 40 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: '3.8'

services:
mountebank:
image: bbyars/mountebank:latest
container_name: mountebank
ports:
- "2525:2525" # Port Mountebank admin interface
- "4545:4545" # Default imposters port
- "5555:5555" # Another imposter port
environment:
MB_LOGLEVEL: info
volumes:
- ./imposters:/mnt/imposters # imposters directory
db:
image: postgres:latest
environment:
POSTGRES_USER: book_user
POSTGRES_PASSWORD: book_password
POSTGRES_DB: book_recommendation
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- db_network

adminer:
image: adminer
restart: always
ports:
- 8080:8080
networks:
- db_network

volumes:
pgdata:

networks:
db_network:
10 changes: 10 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
return {"Hello": "World"}
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
alembic==1.13.2
behave==1.2.6
fastapi==0.114.0
pytest==8.3.2
SQLAlchemy==2.0.34
starlette==0.38.5
Loading