fastapi-struct 0.1.1__tar.gz → 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_struct-0.2.1/PKG-INFO +231 -0
- fastapi_struct-0.2.1/README.md +218 -0
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/pyproject.toml +3 -2
- fastapi_struct-0.2.1/src/create_fastapi_app/__init__.py +1 -0
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/src/create_fastapi_app/cli.py +15 -6
- fastapi_struct-0.2.1/src/create_fastapi_app/generator.py +49 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/.env +4 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/.env.example +4 -0
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/src/create_fastapi_app/templates/basic/.gitignore +3 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/README.md +35 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/api/v1/api.py +6 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/api/v1/routes/items.py +23 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/api/v1/routes/users.py +26 -0
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/src/create_fastapi_app/templates/basic/app/core/config.py +3 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/core/database.py +8 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/core/security.py +13 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/main.py +24 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/models/item.py +9 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/models/user.py +10 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/repositories/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/repositories/item_repository.py +35 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/repositories/user_repository.py +36 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/schemas/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/schemas/item.py +17 -0
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/src/create_fastapi_app/templates/basic/app/schemas/user.py +7 -4
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/services/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/services/item_service.py +21 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/services/user_service.py +26 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/utils/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/utils/email.py +8 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/pyproject.toml +22 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/requirements.txt +7 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/tests/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/tests/conftest.py +9 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/tests/test_items.py +22 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/tests/test_users.py +28 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/.env +4 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/.env.example +4 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/.gitignore +9 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/README.md +31 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/api/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/api/v1/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/api/v1/api.py +6 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/api/v1/routes/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/api/v1/routes/health.py +15 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/api/v1/routes/predict.py +16 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/core/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/core/config.py +20 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/main.py +37 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/ml/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/ml/model.py +34 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/ml/pipeline.py +17 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/schemas/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/schemas/predict.py +20 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/services/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/app/services/inference.py +30 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/pyproject.toml +21 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/requirements.txt +6 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/tests/__init__.py +0 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/tests/conftest.py +9 -0
- fastapi_struct-0.2.1/src/create_fastapi_app/templates/ml/tests/test_predict.py +17 -0
- fastapi_struct-0.2.1/src/fastapi_struct.egg-info/PKG-INFO +231 -0
- fastapi_struct-0.2.1/src/fastapi_struct.egg-info/SOURCES.txt +74 -0
- fastapi_struct-0.2.1/tests/test_generator.py +37 -0
- fastapi_struct-0.1.1/PKG-INFO +0 -267
- fastapi_struct-0.1.1/README.md +0 -254
- fastapi_struct-0.1.1/src/create_fastapi_app/__init__.py +0 -1
- fastapi_struct-0.1.1/src/create_fastapi_app/generator.py +0 -33
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/.env.example +0 -2
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/README.md +0 -23
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/db/database.py +0 -1
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/dependencies.py +0 -1
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/main.py +0 -19
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/models/__init__.py +0 -1
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/routers/health.py +0 -8
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/routers/users.py +0 -33
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/services/__init__.py +0 -1
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/pyproject.toml +0 -21
- fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/tests/test_main.py +0 -15
- fastapi_struct-0.1.1/src/fastapi_struct.egg-info/PKG-INFO +0 -267
- fastapi_struct-0.1.1/src/fastapi_struct.egg-info/SOURCES.txt +0 -32
- fastapi_struct-0.1.1/tests/test_generator.py +0 -29
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/MANIFEST.in +0 -0
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/setup.cfg +0 -0
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/src/create_fastapi_app/templates/basic/app/__init__.py +0 -0
- {fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/core → fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/api}/__init__.py +0 -0
- {fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/db → fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/api/v1}/__init__.py +0 -0
- {fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/routers → fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/api/v1/routes}/__init__.py +0 -0
- {fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/app/schemas → fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/core}/__init__.py +0 -0
- {fastapi_struct-0.1.1/src/create_fastapi_app/templates/basic/tests → fastapi_struct-0.2.1/src/create_fastapi_app/templates/basic/app/models}/__init__.py +0 -0
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/src/fastapi_struct.egg-info/dependency_links.txt +0 -0
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/src/fastapi_struct.egg-info/entry_points.txt +0 -0
- {fastapi_struct-0.1.1 → fastapi_struct-0.2.1}/src/fastapi_struct.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-struct
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: CLI tool to generate standard, layered enterprise folder structures for FastAPI applications.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Keywords: fastapi,project-structure,boilerplate,scaffold,cli,template,folder-structure,architecture,repository-pattern,rest-api
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Framework :: FastAPI
|
|
9
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="https://pypi.org/project/fastapi-struct/"><img src="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" width="320" alt="fastapi-struct"></a>
|
|
16
|
+
</p>
|
|
17
|
+
<p align="center">
|
|
18
|
+
<em>CLI tool to generate standard, production-ready FastAPI folder structures in seconds — with dedicated templates for Enterprise Web APIs and AI/ML Inference Services.</em>
|
|
19
|
+
</p>
|
|
20
|
+
<p align="center">
|
|
21
|
+
<a href="https://pypi.org/project/fastapi-struct" target="_blank">
|
|
22
|
+
<img src="https://img.shields.io/pypi/v/fastapi-struct.svg?color=%2334D058" alt="Package version">
|
|
23
|
+
</a>
|
|
24
|
+
<a href="https://pypi.org/project/fastapi-struct" target="_blank">
|
|
25
|
+
<img src="https://img.shields.io/pypi/pyversions/fastapi-struct.svg?color=%2334D058" alt="Supported Python versions">
|
|
26
|
+
</a>
|
|
27
|
+
<a href="https://github.com/fastapi/fastapi" target="_blank">
|
|
28
|
+
<img src="https://img.shields.io/badge/FastAPI-0.115+-009688.svg?logo=fastapi&logoColor=white" alt="FastAPI">
|
|
29
|
+
</a>
|
|
30
|
+
<a href="https://github.com/pydantic/pydantic" target="_blank">
|
|
31
|
+
<img src="https://img.shields.io/badge/Pydantic-v2-E92063.svg?logo=pydantic&logoColor=white" alt="Pydantic v2">
|
|
32
|
+
</a>
|
|
33
|
+
<a href="https://opensource.org/licenses/MIT" target="_blank">
|
|
34
|
+
<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License: MIT">
|
|
35
|
+
</a>
|
|
36
|
+
</p>
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
**FastAPI Documentation:** [https://fastapi.tiangolo.com](https://fastapi.tiangolo.com)
|
|
41
|
+
**PyPI Package:** [https://pypi.org/project/fastapi-struct/](https://pypi.org/project/fastapi-struct/)
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## ⚡ What is `fastapi-struct`?
|
|
46
|
+
|
|
47
|
+
**`fastapi-struct`** generates clean, modular, and production-tested architectures for FastAPI applications. Rather than spending hours writing repetitive boilerplate, you get an instantly runnable codebase tailored to your exact project type:
|
|
48
|
+
|
|
49
|
+
1. **`basic` (Default)**: Layered enterprise web API architecture (Routes → Services → Repositories → Database & Models).
|
|
50
|
+
2. **`ml`**: Lean, specialized AI/ML inference microservice (Lifespan Model Loader → Inference Pipeline → Predictions & Latency Tracking).
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 📦 Quick Start & CLI Usage
|
|
55
|
+
|
|
56
|
+
### 1. Enterprise Web API (Default)
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# With uvx (instant, no install needed)
|
|
60
|
+
uvx fastapi-struct my_api
|
|
61
|
+
|
|
62
|
+
# Or with pip:
|
|
63
|
+
pip install fastapi-struct
|
|
64
|
+
fastapi-struct my_api
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 2. AI / ML Inference Service (`--template ml`)
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
# Generate a dedicated AI/ML service:
|
|
71
|
+
fastapi-struct my_ml_service --template ml
|
|
72
|
+
|
|
73
|
+
# Or shorthand:
|
|
74
|
+
fastapi-struct my_ml_service -t ml
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 3. Scaffold in the Current Directory (`.`)
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
fastapi-struct . --template ml
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
## 📂 Template 1: AI / ML Inference Architecture (`-t ml`)
|
|
86
|
+
|
|
87
|
+
Specially designed for AI/ML and GenAI engineers. Removes database CRUD overhead and optimizes for model loading, tensor preprocessing, inference, and latency benchmarking.
|
|
88
|
+
|
|
89
|
+
```text
|
|
90
|
+
my_ml_service/
|
|
91
|
+
│
|
|
92
|
+
├── app/
|
|
93
|
+
│ ├── __init__.py
|
|
94
|
+
│ ├── main.py # FastAPI app + lifespan model loader (loads weights once at startup)
|
|
95
|
+
│ │
|
|
96
|
+
│ ├── api/
|
|
97
|
+
│ │ ├── __init__.py
|
|
98
|
+
│ │ └── v1/ # API Versioning
|
|
99
|
+
│ │ ├── __init__.py
|
|
100
|
+
│ │ ├── api.py # Central router aggregating endpoints
|
|
101
|
+
│ │ └── routes/
|
|
102
|
+
│ │ ├── __init__.py
|
|
103
|
+
│ │ ├── health.py # /health probe (readiness check + GPU/device info)
|
|
104
|
+
│ │ └── predict.py # /predict endpoint (input validation, inference, latency)
|
|
105
|
+
│ │
|
|
106
|
+
│ ├── core/
|
|
107
|
+
│ │ ├── __init__.py
|
|
108
|
+
│ │ └── config.py # MODEL_NAME, MODEL_PATH, DEVICE ("cpu"/"cuda"), API_KEYS
|
|
109
|
+
│ │
|
|
110
|
+
│ ├── ml/ # Machine learning logic
|
|
111
|
+
│ │ ├── __init__.py
|
|
112
|
+
│ │ ├── model.py # Model wrapper (PyTorch / ONNX / HuggingFace)
|
|
113
|
+
│ │ └── pipeline.py # Tokenizer, preprocessing & postprocessing
|
|
114
|
+
│ │
|
|
115
|
+
│ ├── schemas/ # Request & Response Pydantic models
|
|
116
|
+
│ │ ├── __init__.py
|
|
117
|
+
│ │ └── predict.py # PredictionRequest, PredictionResponse, HealthResponse
|
|
118
|
+
│ │
|
|
119
|
+
│ └── services/ # Orchestration layer
|
|
120
|
+
│ ├── __init__.py
|
|
121
|
+
│ └── inference.py # Validates payload, executes pipeline, measures latency
|
|
122
|
+
│
|
|
123
|
+
├── tests/ # Automated test suite
|
|
124
|
+
│ ├── __init__.py
|
|
125
|
+
│ ├── conftest.py # TestClient fixtures + mock model
|
|
126
|
+
│ └── test_predict.py # Pytest test cases for /health & /predict
|
|
127
|
+
│
|
|
128
|
+
├── .gitignore
|
|
129
|
+
├── .env # Local model configuration
|
|
130
|
+
├── .env.example
|
|
131
|
+
├── requirements.txt # Ready for pip install -r requirements.txt
|
|
132
|
+
├── pyproject.toml # Build & pytest configuration
|
|
133
|
+
└── README.md
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## 📂 Template 2: Layered Enterprise Web Architecture (`-t basic`)
|
|
139
|
+
|
|
140
|
+
Designed for full-stack and backend services that require domain-driven separation of concerns.
|
|
141
|
+
|
|
142
|
+
```text
|
|
143
|
+
my_fastapi_project/
|
|
144
|
+
│
|
|
145
|
+
├── app/
|
|
146
|
+
│ ├── __init__.py
|
|
147
|
+
│ ├── main.py # Application factory & router registration
|
|
148
|
+
│ │
|
|
149
|
+
│ ├── api/v1/routes/ # HTTP route endpoints (items.py, users.py)
|
|
150
|
+
│ ├── core/ # config.py, database.py, security.py
|
|
151
|
+
│ ├── models/ # Database ORM models (item.py, user.py)
|
|
152
|
+
│ ├── schemas/ # Pydantic serialization schemas (item.py, user.py)
|
|
153
|
+
│ ├── services/ # Pure business logic layer
|
|
154
|
+
│ ├── repositories/ # Database CRUD queries layer
|
|
155
|
+
│ └── utils/ # Utility helpers (email.py)
|
|
156
|
+
│
|
|
157
|
+
├── tests/ # conftest.py, test_items.py, test_users.py
|
|
158
|
+
├── requirements.txt
|
|
159
|
+
├── pyproject.toml
|
|
160
|
+
└── README.md
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## 🚀 Running Your Generated Project
|
|
166
|
+
|
|
167
|
+
### 1. Step into your project directory
|
|
168
|
+
```bash
|
|
169
|
+
cd my_ml_service
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### 2. Create and activate a virtual environment
|
|
173
|
+
|
|
174
|
+
**Windows PowerShell:**
|
|
175
|
+
```powershell
|
|
176
|
+
python -m venv .venv
|
|
177
|
+
.\.venv\Scripts\Activate.ps1
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
**Windows CMD:**
|
|
181
|
+
```cmd
|
|
182
|
+
python -m venv .venv
|
|
183
|
+
call .venv\Scripts\activate.bat
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
**macOS / Linux:**
|
|
187
|
+
```bash
|
|
188
|
+
python3 -m venv .venv
|
|
189
|
+
source .venv/bin/activate
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### 3. Install dependencies
|
|
193
|
+
```bash
|
|
194
|
+
pip install -r requirements.txt
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### 4. Start the server
|
|
198
|
+
```bash
|
|
199
|
+
python -m uvicorn app.main:app --reload
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## 🔍 Interactive API Documentation
|
|
205
|
+
|
|
206
|
+
Once running, visit:
|
|
207
|
+
|
|
208
|
+
- **Swagger UI**: [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
|
|
209
|
+
- **ReDoc**: [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc)
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## 🧪 Running Automated Tests
|
|
214
|
+
|
|
215
|
+
Run the included Pytest suite:
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
python -m pytest
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Output:
|
|
222
|
+
```text
|
|
223
|
+
tests\test_predict.py .. [100%]
|
|
224
|
+
============================== 2 passed in 0.15s ==============================
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## 📜 License
|
|
230
|
+
|
|
231
|
+
This project is licensed under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://pypi.org/project/fastapi-struct/"><img src="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" width="320" alt="fastapi-struct"></a>
|
|
3
|
+
</p>
|
|
4
|
+
<p align="center">
|
|
5
|
+
<em>CLI tool to generate standard, production-ready FastAPI folder structures in seconds — with dedicated templates for Enterprise Web APIs and AI/ML Inference Services.</em>
|
|
6
|
+
</p>
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://pypi.org/project/fastapi-struct" target="_blank">
|
|
9
|
+
<img src="https://img.shields.io/pypi/v/fastapi-struct.svg?color=%2334D058" alt="Package version">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://pypi.org/project/fastapi-struct" target="_blank">
|
|
12
|
+
<img src="https://img.shields.io/pypi/pyversions/fastapi-struct.svg?color=%2334D058" alt="Supported Python versions">
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://github.com/fastapi/fastapi" target="_blank">
|
|
15
|
+
<img src="https://img.shields.io/badge/FastAPI-0.115+-009688.svg?logo=fastapi&logoColor=white" alt="FastAPI">
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://github.com/pydantic/pydantic" target="_blank">
|
|
18
|
+
<img src="https://img.shields.io/badge/Pydantic-v2-E92063.svg?logo=pydantic&logoColor=white" alt="Pydantic v2">
|
|
19
|
+
</a>
|
|
20
|
+
<a href="https://opensource.org/licenses/MIT" target="_blank">
|
|
21
|
+
<img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License: MIT">
|
|
22
|
+
</a>
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
**FastAPI Documentation:** [https://fastapi.tiangolo.com](https://fastapi.tiangolo.com)
|
|
28
|
+
**PyPI Package:** [https://pypi.org/project/fastapi-struct/](https://pypi.org/project/fastapi-struct/)
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## ⚡ What is `fastapi-struct`?
|
|
33
|
+
|
|
34
|
+
**`fastapi-struct`** generates clean, modular, and production-tested architectures for FastAPI applications. Rather than spending hours writing repetitive boilerplate, you get an instantly runnable codebase tailored to your exact project type:
|
|
35
|
+
|
|
36
|
+
1. **`basic` (Default)**: Layered enterprise web API architecture (Routes → Services → Repositories → Database & Models).
|
|
37
|
+
2. **`ml`**: Lean, specialized AI/ML inference microservice (Lifespan Model Loader → Inference Pipeline → Predictions & Latency Tracking).
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## 📦 Quick Start & CLI Usage
|
|
42
|
+
|
|
43
|
+
### 1. Enterprise Web API (Default)
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
# With uvx (instant, no install needed)
|
|
47
|
+
uvx fastapi-struct my_api
|
|
48
|
+
|
|
49
|
+
# Or with pip:
|
|
50
|
+
pip install fastapi-struct
|
|
51
|
+
fastapi-struct my_api
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 2. AI / ML Inference Service (`--template ml`)
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
# Generate a dedicated AI/ML service:
|
|
58
|
+
fastapi-struct my_ml_service --template ml
|
|
59
|
+
|
|
60
|
+
# Or shorthand:
|
|
61
|
+
fastapi-struct my_ml_service -t ml
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 3. Scaffold in the Current Directory (`.`)
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
fastapi-struct . --template ml
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## 📂 Template 1: AI / ML Inference Architecture (`-t ml`)
|
|
73
|
+
|
|
74
|
+
Specially designed for AI/ML and GenAI engineers. Removes database CRUD overhead and optimizes for model loading, tensor preprocessing, inference, and latency benchmarking.
|
|
75
|
+
|
|
76
|
+
```text
|
|
77
|
+
my_ml_service/
|
|
78
|
+
│
|
|
79
|
+
├── app/
|
|
80
|
+
│ ├── __init__.py
|
|
81
|
+
│ ├── main.py # FastAPI app + lifespan model loader (loads weights once at startup)
|
|
82
|
+
│ │
|
|
83
|
+
│ ├── api/
|
|
84
|
+
│ │ ├── __init__.py
|
|
85
|
+
│ │ └── v1/ # API Versioning
|
|
86
|
+
│ │ ├── __init__.py
|
|
87
|
+
│ │ ├── api.py # Central router aggregating endpoints
|
|
88
|
+
│ │ └── routes/
|
|
89
|
+
│ │ ├── __init__.py
|
|
90
|
+
│ │ ├── health.py # /health probe (readiness check + GPU/device info)
|
|
91
|
+
│ │ └── predict.py # /predict endpoint (input validation, inference, latency)
|
|
92
|
+
│ │
|
|
93
|
+
│ ├── core/
|
|
94
|
+
│ │ ├── __init__.py
|
|
95
|
+
│ │ └── config.py # MODEL_NAME, MODEL_PATH, DEVICE ("cpu"/"cuda"), API_KEYS
|
|
96
|
+
│ │
|
|
97
|
+
│ ├── ml/ # Machine learning logic
|
|
98
|
+
│ │ ├── __init__.py
|
|
99
|
+
│ │ ├── model.py # Model wrapper (PyTorch / ONNX / HuggingFace)
|
|
100
|
+
│ │ └── pipeline.py # Tokenizer, preprocessing & postprocessing
|
|
101
|
+
│ │
|
|
102
|
+
│ ├── schemas/ # Request & Response Pydantic models
|
|
103
|
+
│ │ ├── __init__.py
|
|
104
|
+
│ │ └── predict.py # PredictionRequest, PredictionResponse, HealthResponse
|
|
105
|
+
│ │
|
|
106
|
+
│ └── services/ # Orchestration layer
|
|
107
|
+
│ ├── __init__.py
|
|
108
|
+
│ └── inference.py # Validates payload, executes pipeline, measures latency
|
|
109
|
+
│
|
|
110
|
+
├── tests/ # Automated test suite
|
|
111
|
+
│ ├── __init__.py
|
|
112
|
+
│ ├── conftest.py # TestClient fixtures + mock model
|
|
113
|
+
│ └── test_predict.py # Pytest test cases for /health & /predict
|
|
114
|
+
│
|
|
115
|
+
├── .gitignore
|
|
116
|
+
├── .env # Local model configuration
|
|
117
|
+
├── .env.example
|
|
118
|
+
├── requirements.txt # Ready for pip install -r requirements.txt
|
|
119
|
+
├── pyproject.toml # Build & pytest configuration
|
|
120
|
+
└── README.md
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## 📂 Template 2: Layered Enterprise Web Architecture (`-t basic`)
|
|
126
|
+
|
|
127
|
+
Designed for full-stack and backend services that require domain-driven separation of concerns.
|
|
128
|
+
|
|
129
|
+
```text
|
|
130
|
+
my_fastapi_project/
|
|
131
|
+
│
|
|
132
|
+
├── app/
|
|
133
|
+
│ ├── __init__.py
|
|
134
|
+
│ ├── main.py # Application factory & router registration
|
|
135
|
+
│ │
|
|
136
|
+
│ ├── api/v1/routes/ # HTTP route endpoints (items.py, users.py)
|
|
137
|
+
│ ├── core/ # config.py, database.py, security.py
|
|
138
|
+
│ ├── models/ # Database ORM models (item.py, user.py)
|
|
139
|
+
│ ├── schemas/ # Pydantic serialization schemas (item.py, user.py)
|
|
140
|
+
│ ├── services/ # Pure business logic layer
|
|
141
|
+
│ ├── repositories/ # Database CRUD queries layer
|
|
142
|
+
│ └── utils/ # Utility helpers (email.py)
|
|
143
|
+
│
|
|
144
|
+
├── tests/ # conftest.py, test_items.py, test_users.py
|
|
145
|
+
├── requirements.txt
|
|
146
|
+
├── pyproject.toml
|
|
147
|
+
└── README.md
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## 🚀 Running Your Generated Project
|
|
153
|
+
|
|
154
|
+
### 1. Step into your project directory
|
|
155
|
+
```bash
|
|
156
|
+
cd my_ml_service
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### 2. Create and activate a virtual environment
|
|
160
|
+
|
|
161
|
+
**Windows PowerShell:**
|
|
162
|
+
```powershell
|
|
163
|
+
python -m venv .venv
|
|
164
|
+
.\.venv\Scripts\Activate.ps1
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**Windows CMD:**
|
|
168
|
+
```cmd
|
|
169
|
+
python -m venv .venv
|
|
170
|
+
call .venv\Scripts\activate.bat
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
**macOS / Linux:**
|
|
174
|
+
```bash
|
|
175
|
+
python3 -m venv .venv
|
|
176
|
+
source .venv/bin/activate
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 3. Install dependencies
|
|
180
|
+
```bash
|
|
181
|
+
pip install -r requirements.txt
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### 4. Start the server
|
|
185
|
+
```bash
|
|
186
|
+
python -m uvicorn app.main:app --reload
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## 🔍 Interactive API Documentation
|
|
192
|
+
|
|
193
|
+
Once running, visit:
|
|
194
|
+
|
|
195
|
+
- **Swagger UI**: [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
|
|
196
|
+
- **ReDoc**: [http://127.0.0.1:8000/redoc](http://127.0.0.1:8000/redoc)
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
## 🧪 Running Automated Tests
|
|
201
|
+
|
|
202
|
+
Run the included Pytest suite:
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
python -m pytest
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Output:
|
|
209
|
+
```text
|
|
210
|
+
tests\test_predict.py .. [100%]
|
|
211
|
+
============================== 2 passed in 0.15s ==============================
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## 📜 License
|
|
217
|
+
|
|
218
|
+
This project is licensed under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -4,8 +4,8 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "fastapi-struct"
|
|
7
|
-
version = "0.
|
|
8
|
-
description = "CLI tool to generate standard,
|
|
7
|
+
version = "0.2.1"
|
|
8
|
+
description = "CLI tool to generate standard, layered enterprise folder structures for FastAPI applications."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
11
11
|
license = "MIT"
|
|
@@ -18,6 +18,7 @@ keywords = [
|
|
|
18
18
|
"template",
|
|
19
19
|
"folder-structure",
|
|
20
20
|
"architecture",
|
|
21
|
+
"repository-pattern",
|
|
21
22
|
"rest-api",
|
|
22
23
|
]
|
|
23
24
|
classifiers = [
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.1"
|
|
@@ -7,14 +7,21 @@ from create_fastapi_app.generator import create_project
|
|
|
7
7
|
|
|
8
8
|
def main():
|
|
9
9
|
parser = argparse.ArgumentParser(
|
|
10
|
-
prog="
|
|
11
|
-
description="Create a
|
|
10
|
+
prog="fastapi-struct",
|
|
11
|
+
description="Create a production-ready FastAPI project.",
|
|
12
12
|
)
|
|
13
13
|
parser.add_argument(
|
|
14
14
|
"name",
|
|
15
15
|
nargs="?",
|
|
16
16
|
help="Name of the project to generate (or . for current directory)",
|
|
17
17
|
)
|
|
18
|
+
parser.add_argument(
|
|
19
|
+
"-t",
|
|
20
|
+
"--template",
|
|
21
|
+
choices=["basic", "ml"],
|
|
22
|
+
default="basic",
|
|
23
|
+
help="Architecture template to use: 'basic' (layered enterprise web API) or 'ml' (AI/ML inference service). Default: basic",
|
|
24
|
+
)
|
|
18
25
|
parser.add_argument(
|
|
19
26
|
"--version",
|
|
20
27
|
action="version",
|
|
@@ -28,17 +35,19 @@ def main():
|
|
|
28
35
|
sys.exit(2)
|
|
29
36
|
|
|
30
37
|
try:
|
|
31
|
-
path = create_project(args.name)
|
|
38
|
+
path = create_project(args.name, template=args.template)
|
|
32
39
|
except (ValueError, FileExistsError, FileNotFoundError) as exc:
|
|
33
40
|
parser.error(str(exc))
|
|
34
41
|
|
|
35
|
-
|
|
42
|
+
template_desc = "AI/ML Inference Service" if args.template == "ml" else "Enterprise Layered Web API"
|
|
43
|
+
print(f"\nSuccess! Created {template_desc} at: {path}")
|
|
36
44
|
print("\nNext steps:")
|
|
37
45
|
if args.name != ".":
|
|
38
46
|
print(f" cd {args.name}")
|
|
39
47
|
print(" python -m venv .venv")
|
|
40
|
-
print(r" .\.venv\Scripts\Activate.ps1")
|
|
41
|
-
print(
|
|
48
|
+
print(r" .\.venv\Scripts\Activate.ps1 # Windows PowerShell")
|
|
49
|
+
print(" # or: source .venv/bin/activate # macOS / Linux")
|
|
50
|
+
print(" pip install -r requirements.txt")
|
|
42
51
|
print(" python -m uvicorn app.main:app --reload")
|
|
43
52
|
|
|
44
53
|
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
import re
|
|
3
|
+
import shutil
|
|
4
|
+
|
|
5
|
+
PACKAGE_DIR = Path(__file__).parent
|
|
6
|
+
TEMPLATES_DIR = PACKAGE_DIR / "templates"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def create_project(name: str, template: str = "basic") -> Path:
|
|
10
|
+
if name == ".":
|
|
11
|
+
project_path = Path.cwd()
|
|
12
|
+
project_name = project_path.name
|
|
13
|
+
else:
|
|
14
|
+
if not re.fullmatch(r"[A-Za-z][A-Za-z0-9_-]*", name):
|
|
15
|
+
raise ValueError(
|
|
16
|
+
"Project name must start with a letter and contain "
|
|
17
|
+
"only letters, numbers, underscores, or hyphens."
|
|
18
|
+
)
|
|
19
|
+
project_path = Path.cwd() / name
|
|
20
|
+
project_name = name
|
|
21
|
+
|
|
22
|
+
if project_path.exists():
|
|
23
|
+
raise FileExistsError(
|
|
24
|
+
f"Destination directory already exists: {project_path}"
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
selected_template_dir = TEMPLATES_DIR / template
|
|
28
|
+
if not selected_template_dir.is_dir():
|
|
29
|
+
available = [p.name for p in TEMPLATES_DIR.iterdir() if p.is_dir()]
|
|
30
|
+
raise FileNotFoundError(
|
|
31
|
+
f"Template '{template}' not found. Available templates: {', '.join(available)}"
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
# Copy files allowing existing directory (useful when name is '.')
|
|
35
|
+
shutil.copytree(selected_template_dir, project_path, dirs_exist_ok=True)
|
|
36
|
+
|
|
37
|
+
config_path = project_path / "pyproject.toml"
|
|
38
|
+
if config_path.exists():
|
|
39
|
+
config = config_path.read_text(encoding="utf-8")
|
|
40
|
+
config = config.replace(
|
|
41
|
+
'name = "my-api"',
|
|
42
|
+
f'name = "{project_name.lower().replace("_", "-")}"',
|
|
43
|
+
).replace(
|
|
44
|
+
'name = "my-ml-api"',
|
|
45
|
+
f'name = "{project_name.lower().replace("_", "-")}"',
|
|
46
|
+
)
|
|
47
|
+
config_path.write_text(config, encoding="utf-8")
|
|
48
|
+
|
|
49
|
+
return project_path
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# FastAPI Application
|
|
2
|
+
|
|
3
|
+
Enterprise-grade modular FastAPI application generated with `fastapi-struct`.
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
### 1. Create Virtual Environment
|
|
8
|
+
```bash
|
|
9
|
+
python -m venv .venv
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Activate:
|
|
13
|
+
- Windows PowerShell: `.\.venv\Scripts\Activate.ps1`
|
|
14
|
+
- Windows CMD: `call .venv\Scripts\activate.bat`
|
|
15
|
+
- Linux/macOS: `source .venv/bin/activate`
|
|
16
|
+
|
|
17
|
+
### 2. Install Dependencies
|
|
18
|
+
```bash
|
|
19
|
+
pip install -r requirements.txt
|
|
20
|
+
```
|
|
21
|
+
*(or `pip install -e ".[dev]"`)*
|
|
22
|
+
|
|
23
|
+
### 3. Run Development Server
|
|
24
|
+
```bash
|
|
25
|
+
python -m uvicorn app.main:app --reload
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Interactive API docs available at:
|
|
29
|
+
- Swagger UI: http://127.0.0.1:8000/docs
|
|
30
|
+
- ReDoc: http://127.0.0.1:8000/redoc
|
|
31
|
+
|
|
32
|
+
### 4. Run Tests
|
|
33
|
+
```bash
|
|
34
|
+
python -m pytest
|
|
35
|
+
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from fastapi import APIRouter, HTTPException, status
|
|
2
|
+
from app.schemas.item import ItemCreate, ItemResponse
|
|
3
|
+
from app.services.item_service import item_service
|
|
4
|
+
|
|
5
|
+
router = APIRouter(prefix="/items", tags=["Items"])
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@router.get("/", response_model=list[ItemResponse])
|
|
9
|
+
async def read_items():
|
|
10
|
+
return item_service.list_items()
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@router.post("/", response_model=ItemResponse, status_code=status.HTTP_201_CREATED)
|
|
14
|
+
async def create_item(item_in: ItemCreate):
|
|
15
|
+
return item_service.create_item(item_in)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@router.get("/{item_id}", response_model=ItemResponse)
|
|
19
|
+
async def read_item(item_id: int):
|
|
20
|
+
item = item_service.get_item(item_id)
|
|
21
|
+
if not item:
|
|
22
|
+
raise HTTPException(status_code=404, detail="Item not found")
|
|
23
|
+
return item
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from fastapi import APIRouter, HTTPException, status
|
|
2
|
+
from app.schemas.user import UserCreate, UserResponse
|
|
3
|
+
from app.services.user_service import user_service
|
|
4
|
+
|
|
5
|
+
router = APIRouter(prefix="/users", tags=["Users"])
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@router.get("/", response_model=list[UserResponse])
|
|
9
|
+
async def read_users():
|
|
10
|
+
return user_service.list_users()
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@router.post("/", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
|
|
14
|
+
async def create_user(user_in: UserCreate):
|
|
15
|
+
try:
|
|
16
|
+
return user_service.create_user(user_in)
|
|
17
|
+
except ValueError as e:
|
|
18
|
+
raise HTTPException(status_code=400, detail=str(e))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@router.get("/{user_id}", response_model=UserResponse)
|
|
22
|
+
async def read_user(user_id: int):
|
|
23
|
+
user = user_service.get_user(user_id)
|
|
24
|
+
if not user:
|
|
25
|
+
raise HTTPException(status_code=404, detail="User not found")
|
|
26
|
+
return user
|