Skip to content

joeljhou/fast-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

搭建FastAPI服务

虚拟环境

  1. 通过python内置的venv模块创建虚拟环境目录.venv
    python3 -m venv .venv
  2. 激活虚拟环境
    source .venv/bin/activate
    激活
  3. 退出虚拟环境
    deactivate

安装依赖

  1. 安装fastapi/uvicorn依赖
    python -m pip install fastapi uvicorn
  2. 使用requirements.txt管理项目
    touch requirements.txt
    echo -e "fastapi\nuvicorn" > requirements.txt
  3. 获取依赖当前具体版本
    pip freeze

创建 FastAPI 应用

参考:main.py

运行 FastAPI 服务

uvicorn app.main:app --reload --port 2345

使用 Docker 本地运行

  1. 构建镜像并启动服务(默认端口 8000
    docker compose build
    docker compose up
  2. 访问接口与文档
    • 根路径: http://localhost:8000/
    • 文档: http://localhost:8000/docs
  3. 停止服务
    docker compose down

说明:docker-compose.yml 会将本地 ./app 挂载为只读到容器中,便于开发时同步代码;如需热重载,可在 compose 中改用 command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload 并安装对应依赖。

About

本地Docker fast-api接口,提供小工具

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published