# my-fastapi-project **Repository Path**: nittun/my-fastapi-project ## Basic Information - **Project Name**: my-fastapi-project - **Description**: python + fastapi 框架搭建,AI应用的开发 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-04-17 - **Last Updated**: 2025-04-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: Python, FastAPI ## README # my-fastapi-project ## 环境信息 - python3.11.6 - fastapi - SQLAlchemy 2.* - pg-15 ## 目录结构 ```text fastapi_project/ ├── app/ # 核心应用代码 │ └── ai/ # AI 模块目录 │ ├── api/ # 路由层(Router) │ │ ├── __init__.py │ │ ├── v1/ # 版本控制(如v1) │ │ │ ├── __init__.py │ │ │ └── endpoints.py # 具体接口定义 │ ├── core/ # 配置、日志、启动逻辑 │ │ ├── __init__.py │ │ ├── config.py # 环境配置 │ │ ├── logging_config.py # 日志配置 │ │ └── startup.py # 启动逻辑 │ ├── models/ # SQLAlchemy ORM 模型 │ │ ├── __init__.py │ │ └── user.py │ ├── schemas/ # Pydantic 数据校验模型 │ │ ├── __init__.py │ │ └── user_schema.py │ ├── services/ # 业务逻辑处理 │ │ ├── __init__.py │ │ └── user_service.py │ ├── utils/ # 工具类 & 辅助函数 │ │ ├── __init__.py │ │ └── common.py │ └── main.py # FastAPI 应用入口 │ ├── scripts/ # 启动脚本、初始化脚本等 │ └── start.sh │ ├── tests/ # 单元测试 │ ├── __init__.py │ └── test_user.py │ ├── .env # 环境变量配置文件 ├── .gitignore # 忽略文件 ├── pyproject.toml / requirements.txt ├── README.md └── run.py # 项目启动脚本(可选) ``` ## sqlacodegen 使用步骤(自动生成orm文件) ```text 用 sqlacodegen 就能把 指定表 的结构一次性生成成 SQLAlchemy ORM 代码 step1: pip install sqlacodegen pip install "psycopg2-binary>=2.9" # 推荐 binary 版,省去编译 step2:自动生成orm文件 sqlacodegen \ postgresql://user:pwd@host:5432/dbname \ --tables users,roles,login_log \ --outfile app/models/_auto.py step3:一次性生成所有 Schema(无法使用!!!) pip install 'datamodel-code-generator[http]' datamodel-codegen \ --input app/models/_auto.py \ --input-file-type python \ --output app/schemas/_generated \ --output-model-type pydantic_v2.BaseModel \ --use-standard-collections --use-union-operator ``` ## 笔记 1、创建中间件,挂载dbsession 2、创建抽象的service类,支持单表crud操作 3、日志配置:日志输出,日志文件等设置