# fastapi-app **Repository Path**: piao/fastapi-app ## Basic Information - **Project Name**: fastapi-app - **Description**: fastapi 框架学习项目 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-07-15 - **Last Updated**: 2025-11-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: Python, FastAPI ## README ## app 项目目录结构说明 - `Dockerfile` 镜像打包文件 - `main.py` 入口 - `dependencies.py` 依赖引入 - `requirements.txt` 安装依赖及版本配置 - `api/` 包含API路由和请求、响应模型 + `routers/` API路由定义,入口文件 + `schemas/` API验证请求和响应数据模型定义 - `core/` 核心模块 + `config.py` 配置相关 + `security.py` 安全性相关功能 - `services/` 应用程序或服务的业务逻辑 - `db/` 数据库模型定义和数据库交互 + `mysql.py` mysql交互 - `tests/` 单元测试和集成测试 + `api/` API路由测试 + `core/` 核心模块测试 多个API路径时,可以创建多个同级目录,比如api2的子项目 ## 生成本地证书 ```sh openssl req -newkey rsa:2048 -nodes -keyout server.key -x509 -days 365 -out server.crt ``` ## 启动 docker中启动mysql ```sh docker run --name app-mysql -d -p 3306:3306 \ -e MYSQL_ROOT_PASSWORD=mysecretpassword \ -e MYSQL_DATABASE=mydbname \ -e MYSQL_USER=myusername \ -e MYSQL_PASSWORD=mysecretpassword \ mysql:8.0.35 ``` docker中启动redis ```sh docker run -d -p 6379:6379 -p 8001:8001 redis/redis-stack:7.4.0-v0 ``` [手动运行服务器 - Uvicorn][1] ```sh source .venv/bin/activate pip3 install -r requirements.txt uvicorn main:app --host 0.0.0.0 --port 80 deactivate ``` 启用https ```sh uvicorn main:app --host 0.0.0.0 --port 443 --log-level debug --access-log --workers 2 --ssl-keyfile server.key --ssl-certfile server.crt --no-server-header --date-header --env-file ./env --lifespan on ``` ## 打包镜像 ```sh docker build -t fastapi_app:v0.1.0 -f app.Dockerfile . ``` ```sh docker run -d --name fastapi_app -p 8080:8080 fastapi_app:v0.1.0 ``` ## 打包部署 [Gunicorn][11]主要是一个使用WSGI标准的应用服务器。 这意味着 Gunicorn 可以为 Flask 和 Django 等应用程序提供服务。 Gunicorn 本身与 FastAPI 不兼容,因为 FastAPI 使用最新的 ASGI 标准。 ```sh pip install uvicorn pip install gunicorn ``` gunicorn在unix上被广泛应用高性能的Python WSGI UNIX HTTP Server. 命令行参数启动 ```sh gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:80 ``` 文件方式启动应用 ```py import os # 守护进程 daemon=True # 监听的 IP 和端口 bind='0.0.0.0:8000' # 进程文件 pidfile='./gunicorn.pid' # 工作目录 chdir='./' # 工作模式 worker_class='uvicorn.workers.UvicornWorker' # 要使用的worker进程数量 workers=4 # 每个工作进程的线程数量 threads=2 # 最大并发量 worker_connections=2000 # 日志级别 loglevel='debug' access_log_format='%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s " "%(a)s"' # 日志路径 log_dir="./log" if not os.path.exists(log_dir): os.makedirs(log_dir) accesslog='./log/gunicorn_access.log' errorlog='./log/gunicorn_error.log' ``` 使用上面的配置文件gunicorn.py启动 ```sh gunicorn main:app -c gunicorn.py ``` ## tests ```sh source .venv/bin/activate pip install pytest (.venv) ➜ app git:(master) ✗ pytest --version pytest 8.2.2 # 重新激活venv deactivate && source .venv/bin/activate pytest -s tests/app.py::test_root pytest -s tests/services/snowflake.py::test_process_id pytest -s tests/api/id.py::test_csrf_cookie pytest -s tests/api/id.py::TestID::test_get_id ``` ## 参考 - [Middleware][2] - [Python FastAPI系列:自定义FastAPI middleware中间件][3] - [Python框架篇(5):FastApi-中间件使用][4] - [Building Custom Middleware in FastAPI][5] - [pytest测试框架-- 基本功能使用详解][6] - [fastapi-users full example][7] - [Free Responsive HTML Email Template][8] - [Redis OM][9] - [Redis OM Python][10] - [Telegram Bot AI Integration Guide][12] - [python-telegram-bot][13] [1]: https://fastapi.tiangolo.com/zh/deployment/manually/ [2]: https://www.starlette.io/middleware/ [3]: https://blog.csdn.net/jcgeneral/article/details/134006573 [4]: https://blog.csdn.net/weixin_39001207/article/details/135004135 [5]: https://semaphoreci.com/blog/custom-middleware-fastapi [6]: https://download.csdn.net/blog/column/12489628/135565920 [7]: https://fastapi-users.github.io/fastapi-users/latest/configuration/full-example/#full-example [8]: https://github.com/leemunroe/responsive-html-email-template [9]: https://pypi.org/project/redis-om/ [10]: https://www.tkcnn.com/redis/Redis-Stack/Get-started/tutorials/Python.html#概述 [11]: https://fastapi.tiangolo.com/zh/deployment/server-workers/ [12]: https://aotegaliyev.github.io/telegrambot-ai-integration-guide/ [13]: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions---Your-first-Bot