-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathcli.py
More file actions
536 lines (455 loc) · 16.4 KB
/
cli.py
File metadata and controls
536 lines (455 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import os
from logging import getLogger
from pathlib import Path
from typing import Any, Dict, Union
import typer
from rich import print
from rich.padding import Padding
from rich.panel import Panel
from typing_extensions import Annotated
from fastapi_cli.discover import get_import_string
from fastapi_cli.exceptions import FastAPICLIException
from . import __version__
from .logging import setup_logging
app = typer.Typer(rich_markup_mode="rich")
setup_logging()
logger = getLogger(__name__)
try:
import uvicorn
except ImportError: # pragma: no cover
uvicorn = None # type: ignore[assignment]
def version_callback(value: bool) -> None:
if value:
print(f"FastAPI CLI version: [green]{__version__}[/green]")
raise typer.Exit()
def create_structure(structure: Dict[str, Any], base_path: str = "") -> None:
"""
Recursively creates a directory structure and files based on the provided dictionary.
This function traverses a nested dictionary structure and creates corresponding
directories and files. It can handle nested directories, empty files, and files with content.
Args:
structure (Dict[str, Any]): A dictionary representing the desired file/directory structure.
- Keys are names of directories or files.
- Values can be:
- dict: represents a subdirectory
- str: represents file content
- list: represents a directory with multiple files or subdirectories
base_path (str, optional): The base path where the structure should be created.
Defaults to the current directory.
Behavior:
- If a value is a dict, it creates a directory and recursively calls itself.
- If a value is a string, it creates a file with that content.
- If a value is a list, it creates a directory and processes each item in the list:
- String items become empty files.
- Dict items are treated as {filename: content} pairs.
Raises:
OSError: If there's an issue creating directories or files.
TypeError: If the structure contains unsupported types.
Example:
structure = {
"dir1": {
"file1.txt": "content",
"subdir": {
"file2.txt": "more content"
}
},
"dir2": [
"empty_file.txt",
{"config.json": '{"key": "value"}'}
]
}
create_structure(structure, "/path/to/base")
Note:
This function will overwrite existing files if they have the same name as items in the structure.
"""
for key, value in structure.items():
path = os.path.join(base_path, key)
if isinstance(value, dict):
os.makedirs(path, exist_ok=True)
create_structure(value, path)
elif isinstance(value, str):
with open(path, "w") as f:
f.write(value)
elif isinstance(value, list):
os.makedirs(path, exist_ok=True)
for item in value:
if isinstance(item, str):
with open(os.path.join(path, item), "w") as f:
f.write("")
elif isinstance(item, dict):
for file_name, content in item.items():
with open(os.path.join(path, file_name), "w") as f:
f.write(content)
@app.callback()
def callback(
version: Annotated[
Union[bool, None],
typer.Option(
"--version", help="Show the version and exit.", callback=version_callback
),
] = None,
) -> None:
"""
FastAPI CLI - The [bold]fastapi[/bold] command line app. 😎
Manage your [bold]FastAPI[/bold] projects, run your FastAPI apps, and more.
Read more in the docs: [link]https://fastapi.tiangolo.com/fastapi-cli/[/link].
"""
def _run(
path: Union[Path, None] = None,
*,
host: str = "127.0.0.1",
port: int = 8000,
reload: bool = True,
workers: Union[int, None] = None,
root_path: str = "",
command: str,
app: Union[str, None] = None,
proxy_headers: bool = False,
) -> None:
try:
use_uvicorn_app = get_import_string(path=path, app_name=app)
except FastAPICLIException as e:
logger.error(str(e))
raise typer.Exit(code=1) from None
serving_str = f"[dim]Serving at:[/dim] [link]http://{host}:{port}[/link]\n\n[dim]API docs:[/dim] [link]http://{host}:{port}/docs[/link]"
if command == "dev":
panel = Panel(
f"{serving_str}\n\n[dim]Running in development mode, for production use:[/dim] \n\n[b]fastapi run[/b]",
title="FastAPI CLI - Development mode",
expand=False,
padding=(1, 2),
style="black on yellow",
)
else:
panel = Panel(
f"{serving_str}\n\n[dim]Running in production mode, for development use:[/dim] \n\n[b]fastapi dev[/b]",
title="FastAPI CLI - Production mode",
expand=False,
padding=(1, 2),
style="green",
)
print(Padding(panel, 1))
if not uvicorn:
raise FastAPICLIException(
"Could not import Uvicorn, try running 'pip install uvicorn'"
) from None
uvicorn.run(
app=use_uvicorn_app,
host=host,
port=port,
reload=reload,
workers=workers,
root_path=root_path,
proxy_headers=proxy_headers,
)
@app.command()
def dev(
path: Annotated[
Union[Path, None],
typer.Argument(
help="A path to a Python file or package directory (with [blue]__init__.py[/blue] files) containing a [bold]FastAPI[/bold] app. If not provided, a default set of paths will be tried."
),
] = None,
*,
host: Annotated[
str,
typer.Option(
help="The host to serve on. For local development in localhost use [blue]127.0.0.1[/blue]. To enable public access, e.g. in a container, use all the IP addresses available with [blue]0.0.0.0[/blue]."
),
] = "127.0.0.1",
port: Annotated[
int,
typer.Option(
help="The port to serve on. You would normally have a termination proxy on top (another program) handling HTTPS on port [blue]443[/blue] and HTTP on port [blue]80[/blue], transferring the communication to your app."
),
] = 8000,
reload: Annotated[
bool,
typer.Option(
help="Enable auto-reload of the server when (code) files change. This is [bold]resource intensive[/bold], use it only during development."
),
] = True,
root_path: Annotated[
str,
typer.Option(
help="The root path is used to tell your app that it is being served to the outside world with some [bold]path prefix[/bold] set up in some termination proxy or similar."
),
] = "",
app: Annotated[
Union[str, None],
typer.Option(
help="The name of the variable that contains the [bold]FastAPI[/bold] app in the imported module or package. If not provided, it is detected automatically."
),
] = None,
proxy_headers: Annotated[
bool,
typer.Option(
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
),
] = True,
) -> Any:
"""
Run a [bold]FastAPI[/bold] app in [yellow]development[/yellow] mode. 🧪
This is equivalent to [bold]fastapi run[/bold] but with [bold]reload[/bold] enabled and listening on the [blue]127.0.0.1[/blue] address.
It automatically detects the Python module or package that needs to be imported based on the file or directory path passed.
If no path is passed, it tries with:
- [blue]main.py[/blue]
- [blue]app.py[/blue]
- [blue]api.py[/blue]
- [blue]app/main.py[/blue]
- [blue]app/app.py[/blue]
- [blue]app/api.py[/blue]
It also detects the directory that needs to be added to the [bold]PYTHONPATH[/bold] to make the app importable and adds it.
It detects the [bold]FastAPI[/bold] app object to use. By default it looks in the module or package for an object named:
- [blue]app[/blue]
- [blue]api[/blue]
Otherwise, it uses the first [bold]FastAPI[/bold] app found in the imported module or package.
"""
_run(
path=path,
host=host,
port=port,
reload=reload,
root_path=root_path,
app=app,
command="dev",
proxy_headers=proxy_headers,
)
@app.command()
def run(
path: Annotated[
Union[Path, None],
typer.Argument(
help="A path to a Python file or package directory (with [blue]__init__.py[/blue] files) containing a [bold]FastAPI[/bold] app. If not provided, a default set of paths will be tried."
),
] = None,
*,
host: Annotated[
str,
typer.Option(
help="The host to serve on. For local development in localhost use [blue]127.0.0.1[/blue]. To enable public access, e.g. in a container, use all the IP addresses available with [blue]0.0.0.0[/blue]."
),
] = "0.0.0.0",
port: Annotated[
int,
typer.Option(
help="The port to serve on. You would normally have a termination proxy on top (another program) handling HTTPS on port [blue]443[/blue] and HTTP on port [blue]80[/blue], transferring the communication to your app."
),
] = 8000,
reload: Annotated[
bool,
typer.Option(
help="Enable auto-reload of the server when (code) files change. This is [bold]resource intensive[/bold], use it only during development."
),
] = False,
workers: Annotated[
Union[int, None],
typer.Option(
help="Use multiple worker processes. Mutually exclusive with the --reload flag."
),
] = None,
root_path: Annotated[
str,
typer.Option(
help="The root path is used to tell your app that it is being served to the outside world with some [bold]path prefix[/bold] set up in some termination proxy or similar."
),
] = "",
app: Annotated[
Union[str, None],
typer.Option(
help="The name of the variable that contains the [bold]FastAPI[/bold] app in the imported module or package. If not provided, it is detected automatically."
),
] = None,
proxy_headers: Annotated[
bool,
typer.Option(
help="Enable/Disable X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Port to populate remote address info."
),
] = True,
) -> Any:
"""
Run a [bold]FastAPI[/bold] app in [green]production[/green] mode. 🚀
This is equivalent to [bold]fastapi dev[/bold] but with [bold]reload[/bold] disabled and listening on the [blue]0.0.0.0[/blue] address.
It automatically detects the Python module or package that needs to be imported based on the file or directory path passed.
If no path is passed, it tries with:
- [blue]main.py[/blue]
- [blue]app.py[/blue]
- [blue]api.py[/blue]
- [blue]app/main.py[/blue]
- [blue]app/app.py[/blue]
- [blue]app/api.py[/blue]
It also detects the directory that needs to be added to the [bold]PYTHONPATH[/bold] to make the app importable and adds it.
It detects the [bold]FastAPI[/bold] app object to use. By default it looks in the module or package for an object named:
- [blue]app[/blue]
- [blue]api[/blue]
Otherwise, it uses the first [bold]FastAPI[/bold] app found in the imported module or package.
"""
_run(
path=path,
host=host,
port=port,
reload=reload,
workers=workers,
root_path=root_path,
app=app,
command="run",
proxy_headers=proxy_headers,
)
@app.command()
def init(
name: str = typer.Option("fastapi_project", help="Name of the project"),
) -> Any:
"""Initialize a new FastAPI project with example code"""
project_structure = {
name: {
"app": {
"api": {
"v1": {
"endpoints": [
{
"items.py": """from fastapi import APIRouter, HTTPException
from typing import List, Dict
router = APIRouter()
items = {}
@router.get("/items/", response_model=List[Dict[str, Any]])
async def read_items():
return [{"id": k, **v} for k, v in items.items()]
@router.get("/items/{item_id}")
async def read_item(item_id: int):
if item_id not in items:
raise HTTPException(status_code=404, detail="Item not found")
return items[item_id]
@router.post("/items/")
async def create_item(item: Dict[str, Any]):
item_id = max(items.keys() or [0]) + 1
items[item_id] = item
return {"id": item_id, **item}
"""
},
"__init__.py",
],
},
"__init__.py": "",
},
"core": {
"config.py": """from pydantic import BaseSettings
class Settings(BaseSettings):
app_name: str = "FastAPI Project"
debug: bool = False
class Config:
env_file = ".env"
settings = Settings()
""",
"__init__.py": "",
},
"db": {
"base.py": """from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
""",
"session.py": """from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
""",
"__init__.py": "",
},
"models": [
{
"item.py": """from sqlalchemy import Column, Integer, String
from app.db.base import Base
class Item(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True)
description = Column(String, index=True)
"""
},
"__init__.py",
],
"schemas": [
{
"item.py": """from pydantic import BaseModel
class ItemBase(BaseModel):
title: str
description: str = None
class ItemCreate(ItemBase):
pass
class Item(ItemBase):
id: int
class Config:
orm_mode = True
"""
},
"__init__.py",
],
"main.py": """from fastapi import FastAPI
from app.api.v1.endpoints import items
from app.core.config import settings
app = FastAPI(title=settings.app_name, debug=settings.debug)
app.include_router(items.router, prefix="/api/v1")
@app.get("/")
async def root():
return {"message": "Welcome to FastAPI!"}
""",
"__init__.py": "",
},
"tests": [
{
"test_main.py": """from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Welcome to FastAPI!"}
"""
},
"__init__.py",
],
"requirements.txt": """fastapi==0.68.0
uvicorn==0.15.0
sqlalchemy==1.4.23
pydantic==1.8.2
""",
"README.md": f"""# {name}
This is a FastAPI project generated using the FastAPI CLI tool.
## Getting Started
1. Install dependencies:
```
pip install -r requirements.txt
```
2. Run the server:
```
uvicorn app.main:app --reload
```
3. Open your browser and go to http://localhost:8000/docs to see the API documentation.
## Project Structure
- `app/`: Main application package
- `api/`: API endpoints
- `core/`: Core functionality (config, etc.)
- `db/`: Database-related code
- `models/`: SQLAlchemy models
- `schemas/`: Pydantic schemas
- `main.py`: Main FastAPI application
- `tests/`: Test files
## Running Tests
To run tests, use the following command:
```
pytest
```
""",
}
}
create_structure(project_structure)
typer.echo(f"FastAPI project '{name}' created successfully with example code!")
def main() -> None:
app()