Skip to content
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
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 🐛 Bug report
name: Bug report
description: Report something that's broken
labels: ["bug", "needs-triage"]
body:
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
blank_issues_enabled: false
contact_links:
- name: 💬 Ask a question
- name: Ask a question
url: https://github.com/mohit-tanwar-dev/python-backend/discussions
about: Ask questions and discuss ideas in Discussions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Feature request
name: Feature request
description: Suggest a new feature or improvement
labels: ["enhancement", "needs-triage"]
body:
Expand Down
16 changes: 8 additions & 8 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

## Type of change

- [ ] 🐛 Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] 💥 Breaking change (would cause existing functionality to not work as expected)
- [ ] 📝 Documentation update
- [ ] ♻️ Refactor (no functional changes)
- [ ] 🧪 Test additions / improvements
- [ ] 🔧 CI / build / tooling
- [ ] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (would cause existing functionality to not work as expected)
- [ ] Documentation update
- [ ] Refactor (no functional changes)
- [ ] Test additions / improvements
- [ ] CI / build / tooling

## Checklist

- [ ] My code follows the [style guide](../ruff.toml) (`ruff check .` passes)
- [ ] My code follows the style guide (`ruff check .` passes)
- [ ] I ran `ruff format .` and the code is formatted
- [ ] I added / updated tests for my changes
- [ ] `pytest -v` passes locally
Expand Down
16 changes: 8 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- FastAPI application scaffold with `lifespan` event hooks.
- `/health` and `/api/v1/health` endpoints returning service status, version, and uptime.
- Root `/` endpoint returning basic service info.
- Pydantic-settings based configuration with sensible defaults.
- Structured logging via stdlib `logging`.
- `pytest` test suite covering all endpoints.
- `ruff` lint + format configuration targeting Python 3.12.
- `mypy` for static type checking.
- `.env.example` documenting all supported environment variables.
- `/health` and `/api/v1/health` endpoints.
- Root `/` endpoint.
- Pydantic-settings configuration with defaults.
- Structured logging.
- pytest test suite covering all endpoints.
- Ruff lint + format configuration targeting Python 3.12.
- mypy for static type checking.
- `.env.example`.
- MIT License.

[Unreleased]: https://github.com/mohit-tanwar-dev/python-backend/compare/v0.1.0...HEAD
Expand Down
40 changes: 12 additions & 28 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
# Contributing to python-backend

Thanks for taking the time to contribute! 🎉

This document describes how to set up your environment and submit changes.

## 🛠️ Development setup
## Development setup

```bash
# 1. Fork & clone
git clone https://github.com/<your-username>/python-backend.git
cd python-backend

# 2. Create a virtual environment
python -m venv .venv
source .venv/bin/activate # Linux / macOS
# .venv\Scripts\activate # Windows

# 3. Install dev dependencies
pip install -r requirements-dev.txt

# 4. Copy env file (optional — defaults work out of the box)
cp .env.example .env

# 5. Run the server
uvicorn app.main:app --reload

# 6. Run tests / lint
pytest -v
ruff check .
ruff format --check .
mypy app tests
```

## 🌿 Branch & commit conventions
## Branch and commit conventions

- Branch from `main`: `feat/my-feature`, `fix/my-bugfix`, `docs/my-change`
- Branch from `main`: `feat/...`, `fix/...`, `docs/...`
- Use [Conventional Commits](https://www.conventionalcommits.org/):
- `feat: add /ready endpoint`
- `fix: handle missing env var gracefully`
Expand All @@ -43,20 +28,19 @@ mypy app tests
- `test: add tests for /api/v1/health`
- `chore: bump ruff to 0.8.5`

## 🚦 Pull request flow
## Pull request flow

1. Open a PR against `main`.
2. CI must pass (ruff + pytest on Python 3.11 & 3.12).
3. Request a review.
4. Squash-and-merge once approved.
2. CI must pass (ruff + pytest on Python 3.11 and 3.12).
3. Squash-and-merge once approved.

## Code style
## Code style

- Python 3.12+ syntax is fine (PEP 695 type hints, etc.)
- Line length: 100 chars (configured in `ruff.toml`)
- All functions / modules have docstrings
- Tests live in `tests/` and are named `test_*.py`
- Python 3.12+ syntax (PEP 695 type hints)
- Line length: 100 chars (`ruff.toml`)
- All functions and modules have docstrings
- Tests live in `tests/` named `test_*.py`

## 📜 Code of conduct
## Code of conduct

Be kind. Be patient. Be helpful.
196 changes: 47 additions & 149 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,31 @@
[![Code style: ruff](https://img.shields.io/badge/code%20style-ruff-261230?style=flat-square)](https://docs.astral.sh/ruff/)
[![GitHub release](https://img.shields.io/github/v/release/mohit-tanwar-dev/python-backend?style=flat-square)](https://github.com/mohit-tanwar-dev/python-backend/releases)

> A production-ready **FastAPI** scaffold with health checks, structured logging, configurable settings, and CI/CD out of the box.
> Use this as the starting point for any new Python backend service.
FastAPI scaffold with health endpoint, structured logging, env-driven config, pytest suite, ruff/mypy, and GitHub Actions CI.

---
## Stack

## ✨ Features
- Python 3.12 (3.11 supported)
- FastAPI 0.115, Uvicorn
- Pydantic v2 + pydantic-settings
- pytest + httpx
- Ruff (lint + format), mypy
- GitHub Actions CI, Dependabot
- Multi-stage Dockerfile (non-root user, healthcheck)

- 🚀 **FastAPI** async web framework with auto-generated OpenAPI docs (`/docs`, `/redoc`)
- ⚙️ **Pydantic v2 + pydantic-settings** for type-safe, environment-driven configuration
- 📋 **Health-check endpoint** at `/health` and `/api/v1/health` — perfect for Kubernetes probes and load balancers
- 🪵 **Structured logging** with consistent timestamps and log levels
- 🧪 **pytest** test suite with `httpx` + `TestClient` covering every endpoint
- 🧹 **Ruff** for linting and formatting (replaces black + isort + flake8)
- 🔍 **mypy** for static type checking
- 🤖 **GitHub Actions CI** running on every push & PR (Python 3.11 + 3.12 matrix)
- 📦 **Dependabot** for automated dependency updates
- 🐳 **Docker-ready** with multi-stage Dockerfile (optional)
- 📚 **Issue & PR templates**, **CONTRIBUTING.md**, **CODE_OF_CONDUCT.md**, **SECURITY.md**

---

## 🛠️ Tech stack

| Layer | Tool |
| ------------ | ----------------------------------- |
| Framework | [FastAPI](https://fastapi.tiangolo.com/) 0.115 |
| Server | [Uvicorn](https://www.uvicorn.org/) 0.34 |
| Validation | [Pydantic](https://pydantic.dev/) 2.10 + `pydantic-settings` |
| Testing | [pytest](https://pytest.org/) 8.3 + `pytest-asyncio` |
| HTTP client | [httpx](https://www.python-httpx.org/) 0.28 |
| Lint/format | [Ruff](https://docs.astral.sh/ruff/) 0.8 |
| Type check | [mypy](https://mypy-lang.org/) 1.14 |
| Python | 3.12+ (3.11 supported) |

---

## 📁 Project structure
## Project structure

```
python-backend/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI app entrypoint
│ ├── main.py
│ ├── core/
│ │ ├── __init__.py
│ │ ├── config.py # Pydantic settings
│ │ └── logging.py # Structured logging
│ └── api/
│ ├── __init__.py
│ └── v1/
│ ├── __init__.py
│ ├── router.py # Aggregates route modules
│ └── endpoints/
│ ├── __init__.py
│ └── health.py # Health-check endpoint
├── tests/
│ ├── __init__.py
│ └── test_health.py
│ │ ├── config.py
│ │ └── logging.py
│ └── api/v1/
│ ├── router.py
│ └── endpoints/health.py
├── tests/test_health.py
├── .github/
│ ├── workflows/ci.yml
│ ├── ISSUE_TEMPLATE/
Expand All @@ -76,143 +43,74 @@ python-backend/
├── ruff.toml
├── .env.example
├── Dockerfile
├── LICENSE
├── CHANGELOG.md
├── CONTRIBUTING.md
├── CODE_OF_CONDUCT.md
├── SECURITY.md
└── README.md
└── SECURITY.md
```

---

## 🚀 Quick start

### Option A — Local development
## Quick start

```bash
# 1. Clone
git clone https://github.com/mohit-tanwar-dev/python-backend.git
cd python-backend

# 2. Create a virtual environment
python -m venv .venv
source .venv/bin/activate # Linux / macOS
# .venv\Scripts\activate # Windows

# 3. Install dependencies
pip install -r requirements-dev.txt # includes dev / test extras

# 4. (optional) configure env
cp .env.example .env

# 5. Run the server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
python -m venv .venv && source .venv/bin/activate
pip install -r requirements-dev.txt
cp .env.example .env # optional — defaults work
uvicorn app.main:app --reload
```

Open in your browser:
- 🩺 Health — http://localhost:8000/health
- 📚 Swagger UI — http://localhost:8000/docs
- 📘 ReDoc — http://localhost:8000/redoc
- 📋 OpenAPI JSON — http://localhost:8000/openapi.json
Endpoints:

- `GET /` — service info
- `GET /health` — health check
- `GET /api/v1/health` — versioned health check
- `GET /docs` — Swagger UI
- `GET /redoc` — ReDoc

### Option B — Docker
Docker:

```bash
docker build -t python-backend:latest .
docker run --rm -p 8000:8000 --env-file .env python-backend:latest
```

---
## Configuration

## 🔌 API endpoints

| Method | Path | Description |
| ------ | ----------------- | ---------------------------------------------- |
| `GET` | `/` | Root — basic service info & docs link |
| `GET` | `/health` | Public health check (no API prefix) |
| `GET` | `/api/v1/health` | Versioned health check under `/api/v1` prefix |

### Example response (`GET /health`)

```json
{
"status": "ok",
"app": "python-backend",
"version": "0.1.0",
"env": "local",
"uptime_seconds": 12.345
}
```

---

## ⚙️ Configuration

All settings are loaded from environment variables (or a local `.env` file).
Every setting has a sensible default, so the service runs out-of-the-box.
Settings loaded from environment variables or `.env`. All have defaults.

| Variable | Default | Description |
| --------------- | ---------------- | ---------------------------------------- |
| `APP_NAME` | `python-backend` | Human-readable app name |
| `APP_ENV` | `local` | Environment: `local` / `dev` / `prod` |
| `APP_DEBUG` | `true` | Enable verbose logging & error details |
| `APP_NAME` | `python-backend` | Service name |
| `APP_ENV` | `local` | `local` / `dev` / `prod` |
| `APP_DEBUG` | `true` | Verbose logs and error details |
| `APP_HOST` | `0.0.0.0` | Bind host |
| `APP_PORT` | `8000` | Bind port |
| `APP_LOG_LEVEL` | `INFO` | Log level: DEBUG/INFO/WARNING/ERROR |

Copy `.env.example` to `.env` and edit as needed.
| `APP_LOG_LEVEL` | `INFO` | DEBUG / INFO / WARNING / ERROR |

---

## 🧪 Testing & quality checks
## Tests

```bash
# Run tests
pytest -v

# Lint
ruff check .

# Format check (without writing)
ruff format --check .

# Format (write)
ruff format .

# Type check
mypy app tests
```

CI runs all of the above on every push and pull request, on Python 3.11 **and** 3.12.

---

## 🤝 Contributing

Contributions are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for setup instructions, branch conventions, and PR guidelines.

1. Fork the repo
2. Create a feature branch: `git checkout -b feat/my-feature`
3. Commit using [Conventional Commits](https://www.conventionalcommits.org/)
4. Open a PR against `main`

Please follow the [Code of Conduct](CODE_OF_CONDUCT.md) in all interactions.

---
CI runs all of the above on Python 3.11 and 3.12.

## 📜 Changelog
## Contributing

See [CHANGELOG.md](CHANGELOG.md) for release history.
See [CONTRIBUTING.md](CONTRIBUTING.md). Fork, branch from `main`, open a PR.

---
## Changelog

## 🔐 Security
See [CHANGELOG.md](CHANGELOG.md).

To report a vulnerability, see [SECURITY.md](SECURITY.md).
## Security

---
See [SECURITY.md](SECURITY.md).

## 📄 License
## License

[MIT](LICENSE) © Mohit Tanwar
MIT — see [LICENSE](LICENSE).
Loading
Loading