FastAPI构建的API服务

Overview

使用FastAPI 构建的商城项目API

学习FastAPI 构建项目目录

构建项目接口: 对应博客:https://www.charmcode.cn/article/2020-06-08_vue_mall_api

声明

此项目已经不再维护, 可以参考我另外一个项目https://github.com/CoderCharm/fastapi-mysql-generator

本来使用FastAPI写接口是为了配合我学习Vue顺手看着文档学的,没想到这么多老哥关注,上面那个项目我会尽可能更新完善,一起加油学习吧!! 🤪

环境

Python版本 FastAPI版本

项目文件结构

文件结构是仿照Flask项目目录构建的,官方推荐的模版对我而言太大。

官方有推荐项目文件模版 https://fastapi.tiangolo.com/tutorial/bigger-applications/

项目文件结构
.
|_app                           // 主项目文件
| 
|___api
| |_____init__.py               // (重要)工厂模式生成app对象
| |____v1
| |____database.py              // 数据库对象
| |____schemas.py               // 验证参数       (可放到对应模块内)
| |____models.py                // models模型类型 (可放到对应模块内)
| |____home                     // 项目模块文件
| | |____home.py
| | |______init__.py
| | |____home_backup.py
| |____category
| | |______init__.py
| |______init__.py
| |____profile
| | |____profile.py
| | |______init__.py
| |____goods
|   |____goods.py
|   |____goods_backup.py
|____test                     // 测试用例
| |______init__.py
| |____test_sqlite.py
|____utils                    // 工具类
| |______init__.py
| |___response_code.py        // 自定义返回的状态码
|____setting                  // 配置文件夹
| |______init__.py            // 根据虚拟环境 导出不同配置
| |____development_config.py  // 开发环境配置
| |____production_config.py   // 生产环境配置
|____extensions               // 扩展文件
| |______init__.py            // 导出扩展文件
| |____logger.py              // 
|____alembic                  //   alembic  初始化自动生成的 
| |____script.py.mako
| |____env.py
| |____versions
| |____README
|____alembic.ini              // alembic  初始化自动生成的 
|____.gitignore
|____requirements.text        // 依赖文件
|____main.py                  // 项目启动文件
|____mall_data.sql            // mysql insert 数据
|____mall_table.sql           // msyql表格 
|____README.md
|____Pipfile
|____Pipfile.lock

配置环境

setting目录下 __init__.py文件,会根据ENV的环境变量 导入不同的环境

development_config.py // 开发环境配置 production_config.py // 生产环境配置

如果不配置, 就只能访问 backup 备份的接口

导入数据

  • mall_data.sql // mysql insert 数据
  • mall_table.sql // msyql表格

上面两个文件是mysql数据, 需自行导入

常规启动

安装依赖

# 推荐先安装pipenv
pip install pipenv -i https://mirrors.aliyun.com/pypi/simple/

# 先进入到项目文件下
cd /项目目录/MallAPI

# 安装pipenv python版本3.7+
pipenv install --python 3.7 # 注意 --python空格3.7

# 安装完后激活环境
pipenv shell

# 安装依赖
pip install -r requirements.text -i https://mirrors.aliyun.com/pypi/simple/

启动

cd /项目目录/MallAPI

# 在main.py文件同级目录下 执行
uvicorn main:app --host=127.0.0.1 --port=8010 --reload

当然也可以直接执行

python main.py

Docker 启动

构建镜像

可以参考FastAPI作者tiangolo构建的镜像

https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker

镜像名字随便取的, 继承FastAPI作者构建的镜像

docker build -t mallapp .

临时测试运行

docker run -d --name mycontainer -p 8030:8030 mallapp

后台运行启动

docker run -d --name mycontainer -p 80:80 mallapp

启动后API文档地址

http://127.0.0.1:8010/api/v1/docs

线上文档地址

https://www.charmcode.cn/api/v1/docs

sqlalchemy + alembic数据迁移

尝试使用sqlalchemy + alembic 但是之前使用的Flask-Sqlalchemy扩展,迁移数据库很方便, 研究这个感觉很麻烦,算了直接使用sql语句,数据表手动创建

# 安装
pip install alembic

# 初始化 生成alembic配置文件
alembic init alembic

具体使用 sqlalchemy 可以参考我这个后台管理的Demo

https://github.com/CoderCharm/FastAdmin/tree/master/backend

参考

Owner
王小右
尽人事,听天命。Just For Fun. Go/Python/JavaScript&Vue
王小右
FastAPI on Google Cloud Run

cloudrun-fastapi Boilerplate for running FastAPI on Google Cloud Run with Google Cloud Build for deployment. For all documentation visit the docs fold

Anthony Corletti 139 Dec 27, 2022
FastAPI Boilerplate

FastAPI Boilerplate Features SQlAlchemy session Custom user class Top-level dependency Dependencies for specific permissions Celery SQLAlchemy for asy

Hide 417 Jan 07, 2023
REST API with FastAPI and SQLite3.

REST API with FastAPI and SQLite3

Luis Quiñones Requelme 2 Mar 14, 2022
A simple docker-compose app for orchestrating a fastapi application, a celery queue with rabbitmq(broker) and redis(backend)

fastapi - celery - rabbitmq - redis - Docker A simple docker-compose app for orchestrating a fastapi application, a celery queue with rabbitmq(broker

Kartheekasasanka Kaipa 83 Dec 19, 2022
Admin Panel for GinoORM - ready to up & run (just add your models)

Gino-Admin Docs (state: in process): Gino-Admin docs Play with Demo (current master 0.2.3) Gino-Admin demo (login: admin, pass: 1234) Admin

Iuliia Volkova 46 Nov 02, 2022
asgi-server-timing-middleware

ASGI Server-Timing middleware An ASGI middleware that wraps the excellent yappi profiler to let you measure the execution time of any function or coro

33 Dec 15, 2022
I'm curious if pydantic + fast api can be sensibly used with DDD + hex arch methodology

pydantic-ddd-exploration I'm curious if pydantic + fast api can be sensibly used with DDD + hex arch methodology Prerequisites nix direnv (nix-env -i

Olgierd Kasprowicz 2 Nov 17, 2021
Auth for use with FastAPI

FastAPI Auth Pluggable auth for use with FastAPI Supports OAuth2 Password Flow Uses JWT access and refresh tokens 100% mypy and test coverage Supports

David Montague 95 Jan 02, 2023
volunteer-database

This is the official CSM (Crowd source medical) database The What Now? We created this in light of the COVID-19 pandemic to allow volunteers to work t

32 Jun 21, 2022
Piccolo Admin provides a simple yet powerful admin interface on top of Piccolo tables

Piccolo Admin Piccolo Admin provides a simple yet powerful admin interface on top of Piccolo tables - allowing you to easily add / edit / filter your

188 Jan 09, 2023
FastAPI Learning Example,对应中文视频学习教程:https://space.bilibili.com/396891097

视频教学地址 中文学习教程 1、本教程每一个案例都可以独立跑,前提是安装好依赖包。 2、本教程并未按照官方教程顺序,而是按照实际使用顺序编排。 Video Teaching Address FastAPI Learning Example 1.Each case in this tutorial c

381 Dec 11, 2022
Fastapi practice project

todo-list-fastapi practice project How to run Install dependencies npm, yarn: standard-version, husky make: script for lint, test pipenv: virtualenv +

Deo Kim 10 Nov 30, 2022
基于Pytorch的脚手架项目,Celery+FastAPI+Gunicorn+Nginx+Supervisor实现服务部署,支持Docker发布

cookiecutter-pytorch-fastapi 基于Pytorch的 脚手架项目 按规范添加推理函数即可实现Celery+FastAPI+Gunicorn+Nginx+Supervisor+Docker的快速部署 Requirements Python = 3.6 with pip in

17 Dec 23, 2022
EML analyzer is an application to analyze the EML file

EML analyzer EML analyzer is an application to analyze the EML file which can: Analyze headers. Analyze bodies. Extract IOCs (URLs, domains, IP addres

Manabu Niseki 162 Dec 28, 2022
FastAPI Admin Dashboard based on FastAPI and Tortoise ORM.

FastAPI ADMIN 中文文档 Introduction FastAPI-Admin is a admin dashboard based on fastapi and tortoise-orm. FastAPI-Admin provide crud feature out-of-the-bo

long2ice 1.6k Dec 31, 2022
Web Inventory tool, takes screenshots of webpages using Pyppeteer (headless Chrome/Chromium) and provides some extra bells & whistles to make life easier.

WitnessMe WitnessMe is primarily a Web Inventory tool inspired by Eyewitness, its also written to be extensible allowing you to create custom function

byt3bl33d3r 648 Jan 05, 2023
ReST based network device broker

The Open API Platform for Network Devices netpalm makes it easy to push and pull state from your apps to your network by providing multiple southbound

368 Dec 31, 2022
FastAPI + Django experiment

django-fastapi-example This is an experiment to demonstrate one potential way of running FastAPI with Django. It won't be actively maintained. If you'

Jordan Eremieff 78 Jan 03, 2023
Example app using FastAPI and JWT

FastAPI-Auth Example app using FastAPI and JWT virtualenv -p python3 venv source venv/bin/activate pip3 install -r requirements.txt mv config.yaml.exa

Sander 28 Oct 25, 2022
An image validator using FastAPI.

fast_api_image_validator An image validator using FastAPI.

Kevin Zehnder 7 Jan 06, 2022