Skip to content

Commit 682e7a3

Browse files
committed
[TEMPLATE] add fastapi-single-module template, edit fastsapi-psql-orm template's docker steps
1 parent f3d4e9f commit 682e7a3

File tree

13 files changed

+260
-19
lines changed

13 files changed

+260
-19
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ repos:
3232
types: [python]
3333
pass_filenames: false
3434

35+
- id: inspect-templates
36+
name: inspect changed fastapi templates
37+
entry: python scripts/inspect-changed-templates.py
38+
language: system
39+
pass_filenames: false
40+
stages: [commit]
41+
3542
ci:
3643
autofix_commit_msg: 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
3744
autoupdate_commit_msg: ⬆ [pre-commit.ci] pre-commit autoupdate
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env python3
2+
# --------------------------------------------------------------------------
3+
# Inspect only changed FastAPI templates (for pre-commit / local use)
4+
# --------------------------------------------------------------------------
5+
import os
6+
import subprocess
7+
import sys
8+
from pathlib import Path
9+
from typing import List, Set
10+
11+
# Ensure src is importable
12+
PROJECT_ROOT = Path(__file__).parent.parent
13+
sys.path.insert(0, str(PROJECT_ROOT / "src"))
14+
15+
from fastapi_fastkit.backend.inspector import inspect_fastapi_template
16+
17+
TEMPLATE_DIR = PROJECT_ROOT / "src" / "fastapi_fastkit" / "fastapi_project_template"
18+
19+
20+
def get_changed_files() -> List[str]:
21+
"""Return a list of changed files according to git (staged + unstaged)."""
22+
# Include staged and unstaged changes compared to HEAD
23+
cmd = [
24+
"git",
25+
"diff",
26+
"--name-only",
27+
"--cached",
28+
]
29+
staged = subprocess.run(cmd, capture_output=True, text=True, check=False)
30+
cmd_unstaged = [
31+
"git",
32+
"diff",
33+
"--name-only",
34+
]
35+
unstaged = subprocess.run(cmd_unstaged, capture_output=True, text=True, check=False)
36+
37+
files: Set[str] = set()
38+
if staged.stdout:
39+
files.update(
40+
[line.strip() for line in staged.stdout.splitlines() if line.strip()]
41+
)
42+
if unstaged.stdout:
43+
files.update(
44+
[line.strip() for line in unstaged.stdout.splitlines() if line.strip()]
45+
)
46+
return sorted(files)
47+
48+
49+
def extract_changed_templates(changed_files: List[str]) -> List[str]:
50+
"""Map changed files to template directory names under TEMPLATE_DIR."""
51+
templates: Set[str] = set()
52+
template_root_str = str(TEMPLATE_DIR).rstrip("/")
53+
54+
for f in changed_files:
55+
fp = (PROJECT_ROOT / f).resolve()
56+
# Only consider files under template root
57+
try:
58+
if str(fp).startswith(template_root_str):
59+
# template name is immediate child dir of TEMPLATE_DIR
60+
# e.g., .../fastapi_project_template/<template>/...
61+
rel = str(fp)[len(template_root_str) + 1 :]
62+
parts = rel.split(os.sep)
63+
if parts:
64+
templates.add(parts[0])
65+
except Exception:
66+
continue
67+
68+
# Filter only directories that truly exist
69+
return sorted([t for t in templates if (TEMPLATE_DIR / t).is_dir()])
70+
71+
72+
def main() -> int:
73+
changed_files = get_changed_files()
74+
changed_templates = extract_changed_templates(changed_files)
75+
76+
if not changed_templates:
77+
print("No template changes detected; skipping inspection.")
78+
return 0
79+
80+
print(f"Detected changed templates: {', '.join(changed_templates)}")
81+
any_failed = False
82+
83+
for template in changed_templates:
84+
t_path = TEMPLATE_DIR / template
85+
print(f"Inspecting changed template: {template}\n Path: {t_path}")
86+
try:
87+
result = inspect_fastapi_template(str(t_path))
88+
if not result.get("is_valid", False):
89+
any_failed = True
90+
except Exception as e:
91+
any_failed = True
92+
print(f"Exception while inspecting {template}: {e}")
93+
94+
if any_failed:
95+
print("Template inspection failed for at least one changed template.")
96+
return 1
97+
98+
print("All changed templates passed inspection.")
99+
return 0
100+
101+
102+
if __name__ == "__main__":
103+
sys.exit(main())

src/fastapi_fastkit/fastapi_project_template/fastapi-mcp/README.md-tpl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,12 @@ Once the server is running, visit:
190190
The application exposes the following tools and endpoints via MCP:
191191

192192
### MCP-Specific Endpoints
193+
193194
- **mcp_hello**: Simple hello world endpoint (`/mcp-routes/hello`)
194195
- **mcp_status**: MCP configuration status (`/mcp-routes/status`)
195196

196197
### API Endpoints via MCP
198+
197199
- **get_items**: List items with pagination
198200
- **get_item**: Get specific item by ID
199201
- **create_item**: Create new item (requires auth)
@@ -229,21 +231,19 @@ COPY scripts/ scripts/
229231
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]
230232
```
231233

232-
## Contributing
233-
234-
1. Fork the repository
235-
2. Create a feature branch
236-
3. Make your changes
237-
4. Run tests and linting
238-
5. Submit a pull request
239-
240-
## License
241-
242-
This project is licensed under the MIT License.
243-
244234
## Support
245235

246236
For support and questions:
247237
- Create an issue in the repository
248238
- Check the FastAPI documentation: https://fastapi.tiangolo.com/
249239
- Check the FastAPI-MCP documentation: https://github.com/tadata-org/fastapi_mcp
240+
241+
# Project Origin
242+
243+
This project was created based on the template from the [FastAPI-fastkit](https://github.com/bnbong/FastAPI-fastkit) project.
244+
245+
The `FastAPI-fastkit` is an open-source project that helps Python and FastAPI beginners quickly set up a FastAPI-based application development environment in a framework-like structure.
246+
247+
### Template Information
248+
- Template creator: [bnbong](mailto:[email protected])
249+
- FastAPI-fastkit project maintainer: [bnbong](mailto:[email protected])

src/fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/Dockerfile-tpl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ FROM python:3.12-slim
22

33
WORKDIR /app
44

5-
RUN apt-get update && \
6-
apt-get install -y --no-install-recommends \
7-
build-essential \
8-
&& rm -rf /var/lib/apt/lists/*
9-
105
COPY requirements.txt .
116
COPY setup.py .
127
COPY src/ src/

src/fastapi_fastkit/fastapi_project_template/fastapi-psql-orm/docker-compose.yml-tpl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3'
2-
31
services:
42
db:
53
image: postgres:16-alpine
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# FastAPI Single Module Template
2+
3+
This template provides a minimal FastAPI application that runs with a single `main.py` module.
4+
5+
## Structure
6+
```
7+
.
8+
├── README.md
9+
├── requirements.txt
10+
├── pyproject.toml
11+
├── setup.py
12+
├── setup.cfg
13+
├── src/
14+
│ └── main.py
15+
└── tests/
16+
├── conftest.py
17+
└── test_main.py
18+
```
19+
20+
## Run
21+
```bash
22+
uvicorn src.main:app --reload
23+
```
24+
25+
## Requirements
26+
- Python >= 3.9
27+
- FastAPI, Uvicorn
28+
29+
# Project Origin
30+
31+
This project was created based on the template from the [FastAPI-fastkit](https://github.com/bnbong/FastAPI-fastkit) project.
32+
33+
The `FastAPI-fastkit` is an open-source project that helps Python and FastAPI beginners quickly set up a FastAPI-based application development environment in a framework-like structure.
34+
35+
### Template Information
36+
- Template creator: [bnbong](mailto:[email protected])
37+
- FastAPI-fastkit project maintainer: [bnbong](mailto:[email protected])
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[project]
2+
name = "<project_name>"
3+
version = "0.1.0"
4+
description = "<description>"
5+
authors = [
6+
{name = "<author>", email = "<author_email>"},
7+
]
8+
readme = "README.md"
9+
license = "MIT"
10+
requires-python = ">=3.9"
11+
dependencies = [
12+
"fastapi>=0.115.8",
13+
"uvicorn[standard]>=0.34.0",
14+
]
15+
16+
[dependency-groups]
17+
dev = [
18+
"pytest>=8.3.4",
19+
"httpx>=0.28.1",
20+
]
21+
22+
[build-system]
23+
requires = ["hatchling"]
24+
build-backend = "hatchling.build"
25+
26+
[tool.hatch.build.targets.wheel]
27+
packages = ["src"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fastapi==0.115.8
2+
uvicorn[standard]==0.34.0
3+
pydantic==2.10.6
4+
pydantic-settings==2.7.1
5+
pytest==8.3.4
6+
httpx==0.28.1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[tool:pytest]
2+
pythonpath = src
3+
testpaths = tests
4+
python_files = test_*.py
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from setuptools import setup
2+
3+
install_requires: list[str] = [
4+
"fastapi",
5+
"uvicorn",
6+
]
7+
8+
description = "[FastAPI-fastkit templated] <description>"
9+
10+
setup(
11+
name="<project_name>",
12+
description=description,
13+
author="<author>",
14+
author_email=f"<author_email>",
15+
packages=["src"],
16+
install_requires=install_requires,
17+
)

0 commit comments

Comments
 (0)