|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Annotated |
| 5 | + |
| 6 | +import uvicorn |
| 7 | + |
| 8 | +from cappa import Arg, Subcommands, invoke |
| 9 | +from rich.panel import Panel |
| 10 | +from rich.progress import ( |
| 11 | + Progress, |
| 12 | + SpinnerColumn, |
| 13 | + TextColumn, |
| 14 | + TimeElapsedColumn, |
| 15 | +) |
| 16 | +from rich.text import Text |
| 17 | + |
| 18 | +from backend import console, get_version |
| 19 | +from backend.core.conf import settings |
| 20 | +from backend.plugin.tools import get_plugins, install_requirements |
| 21 | + |
| 22 | + |
| 23 | +def run(host: str, port: int, reload: bool, workers: int | None) -> None: |
| 24 | + console.print(Text('检测插件依赖...', style='bold cyan')) |
| 25 | + |
| 26 | + plugins = get_plugins() |
| 27 | + |
| 28 | + with Progress( |
| 29 | + SpinnerColumn(finished_text='[bold green]插件依赖安装完成[/]'), |
| 30 | + TextColumn('[green]{task.completed}/{task.total}[/]'), |
| 31 | + TimeElapsedColumn(), |
| 32 | + console=console, |
| 33 | + ) as progress: |
| 34 | + task = progress.add_task('安装插件依赖...', total=len(plugins)) |
| 35 | + for i, plugin in enumerate(plugins): |
| 36 | + install_requirements(plugin) |
| 37 | + progress.advance(task) |
| 38 | + |
| 39 | + url = f'http://{host}:{port}' |
| 40 | + docs_url = url + settings.FASTAPI_DOCS_URL |
| 41 | + redoc_url = url + settings.FASTAPI_REDOC_URL |
| 42 | + openapi_url = url + settings.FASTAPI_OPENAPI_URL |
| 43 | + |
| 44 | + console.print(Text('启动 fba 服务...', style='bold magenta')) |
| 45 | + |
| 46 | + panel_content = Text() |
| 47 | + panel_content.append(f'📝 Swagger 文档: {docs_url}\n', style='blue') |
| 48 | + panel_content.append(f'📚 Redoc 文档: {redoc_url}\n', style='yellow') |
| 49 | + panel_content.append(f'📡 OpenAPI JSON: {openapi_url}\n', style='green') |
| 50 | + panel_content.append( |
| 51 | + '🌍 fba 官方文档: https://fastapi-practices.github.io/fastapi_best_architecture_docs/', |
| 52 | + style='cyan', |
| 53 | + ) |
| 54 | + |
| 55 | + console.print(Panel(panel_content, title='fba 服务信息', border_style='purple', padding=(1, 2))) |
| 56 | + uvicorn.run(app='backend.main:app', host=host, port=port, reload=reload, workers=workers) |
| 57 | + |
| 58 | + |
| 59 | +@dataclass |
| 60 | +class Run: |
| 61 | + host: Annotated[ |
| 62 | + str, |
| 63 | + Arg( |
| 64 | + long=True, |
| 65 | + default='127.0.0.1', |
| 66 | + help='提供服务的主机 IP 地址,对于本地开发,请使用 `127.0.0.1`。' |
| 67 | + '要启用公共访问,例如在局域网中,请使用 `0.0.0.0`', |
| 68 | + ), |
| 69 | + ] |
| 70 | + port: Annotated[ |
| 71 | + int, |
| 72 | + Arg(long=True, default=8000, help='提供服务的主机端口号'), |
| 73 | + ] |
| 74 | + reload: Annotated[ |
| 75 | + bool, |
| 76 | + Arg(long=True, default=True, help='启用在(代码)文件更改时自动重新加载服务器'), |
| 77 | + ] |
| 78 | + workers: Annotated[ |
| 79 | + int | None, |
| 80 | + Arg(long=True, default=None, help='使用多个工作进程。与 `--reload` 标志互斥'), |
| 81 | + ] |
| 82 | + |
| 83 | + def __call__(self): |
| 84 | + run(host=self.host, port=self.port, reload=self.reload, workers=self.workers) |
| 85 | + |
| 86 | + |
| 87 | +@dataclass |
| 88 | +class FbaCli: |
| 89 | + version: Annotated[ |
| 90 | + bool, |
| 91 | + Arg(short='-V', long=True, default=False, help='打印 fba 当前版本号'), |
| 92 | + ] |
| 93 | + subcmd: Subcommands[Run | None] = None |
| 94 | + |
| 95 | + def __call__(self): |
| 96 | + if self.version: |
| 97 | + get_version() |
| 98 | + |
| 99 | + |
| 100 | +def main() -> None: |
| 101 | + invoke(FbaCli) |
0 commit comments