# fastapi_template **Repository Path**: sdfsdfs445/fastapi_template ## Basic Information - **Project Name**: fastapi_template - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-12-01 - **Last Updated**: 2024-12-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # fastapi_template模板使用说明 ## 安装依赖 ```shell # 安装项目生成器cookiecutter pip install cookiecutter # 指定git仓库地址 cookiecutter https://gitee.com/sdfsdfs445/fastapi_template #根据自己需要输入信息,自动生成项目 cd project #初始化git git init git add . git commit -m "init" #安装poetry包管理器 pip install poetry # 安装依赖 poetry install poetry install -G dev ``` ## 依赖版本 ```toml [tool.poetry] name = "{{cookiecutter.project_slug}}" version = "{{cookiecutter.version}}" description = "{{cookiecutter.project_short_description}}" authors = ["{{cookiecutter.github_username}} <{{cookiecutter.email}}>"] readme = "README.md" # 包定义,如果包结构非标准,可以自定义包路径 packages = [{include = "src"}] [tool.poetry.dependencies] python = "==3.12" click = "==8.1.7" dynaconf = "==3.2.6" sqlalchemy = "==2.0.36" mysqlclient = "==2.2.5" pydantic = "==2.9.2" fastapi = "==0.115.4" uvicorn = "==0.32.0" alembic = "==1.14.0" [tool.poetry.scripts] {{cookiecutter.project_cmd_tool}} = "src.{{cookiecutter.project_slug}}.cmdline:main" [tool.poetry.group.dev.dependencies] pytest = "==8.3.3" requests = "==2.32.3" httpx = "==0.27.2" pytest-mock = "==3.14.0" isort = "==5.13.2" flake8 = "==7.1.1" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" # 配置项目下载源 [[tool.poetry.source]] name = "tsinghua" priority = "primary" url = "https://pypi.tuna.tsinghua.edu.cn/simple" [[tool.poetry.source]] name = "aliyun" url = "https://mirrors.aliyun.com/pypi/simple/" priority = "supplemental" ``` ## 模板使用 ### 配置数据库 第一次需要创建数据库 config/settings.yml ```yaml LOG_LEVEL: INFO # ###################################################################################################### # # https://docs.sqlalchemy.org/en/13/core/engines.html DATABASE: DRIVER: mysql NAME: example_blog HOST: 127.0.0.1 PORT: 3306 USERNAME: root PASSWORD: root QUERY: charset: utf8mb4 HOST: 127.0.0.1 PORT: 45678 ``` ### 运行本地环境 ```shell #根据cmd_tool_name来执行项目 cmd_tool_name server -h localhost -p 8888 ``` 访问docs接口文档 [http://localhost:8888/docs](http://localhost:8888/docs) ## 更多使用 自定义命令行 src/{{project_name}}/cmdline.py ```python @click.group(invoke_without_command=True) @click.pass_context @click.option('-V', '--version', is_flag=True, help='Show version and exit.') def main(ctx, version): if version: click.echo(click.__version__) elif ctx.invoked_subcommand is None: click.echo(ctx.get_help()) @main.command() @click.option('-h', '--host', show_default=True, help=f'Host IP. Default: {settings.HOST}') @click.option('-p', '--port', show_default=True, type=int, help=f'Port. Default: {settings.PORT}') @click.option('--level', help='Log level') def server(host, port, level): """Start server.""" kwargs = { 'LOGLEVEL': level, 'HOST': host, 'PORT': port, } for name, value in kwargs.items(): if value: settings.set(name, value) Server().run() @main.command() @click.pass_context @click.option('-h', '--help', is_flag=True) @click.argument('args', nargs=-1) def migrate(ctx: Context, help, args): """usage migrate -- arguments """ with utils.chdir(Path(__file__).parent / 'migration'): argv = list(args) if help: argv.append('--help') config.main(prog=ctx.command_path, argv=argv) ``` pytest测试 ```shell pytest tests/test_views.py #运行指定测试 pytest #运行所有测试 ``` isort优化导入 ```shell isort . #格式化代码 ``` flake8优化代码风格 ```shell flake8 #优化代码风格 ``` 打包发布 ```shell poetry build poetry publish #发布,需要提供pypi的token ```