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
王小右
Example projects built using Piccolo.

Piccolo examples Here are some example Piccolo projects. Tutorials headless blog fastapi Build a documented API with an admin in minutes! Live project

15 Nov 23, 2022
An image validator using FastAPI.

fast_api_image_validator An image validator using FastAPI.

Kevin Zehnder 7 Jan 06, 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
Online Repo Browser

MSYS2 Web Interface A simple web interface for browsing the MSYS2 repos. Rebuild CSS/JS (optional): cd frontend npm install npm run build Run for Dev

MSYS2 64 Dec 30, 2022
Lung Segmentation with fastapi

Lung Segmentation with fastapi This app uses FastAPI as backend. Usage for app.py First install required libraries by running: pip install -r requirem

Pejman Samadi 0 Sep 20, 2022
Generate FastAPI projects for high performance applications

Generate FastAPI projects for high performance applications. Based on MVC architectural pattern, WSGI + ASGI. Includes tests, pipeline, base utilities, Helm chart, and script for bootstrapping local

Radosław Szamszur 274 Jan 08, 2023
Adds integration of the Jinja template language to FastAPI.

fastapi-jinja Adds integration of the Jinja template language to FastAPI. This is inspired and based off fastapi-chamelon by Mike Kennedy. Check that

Marc Brooks 58 Nov 29, 2022
Code for my JWT auth for FastAPI tutorial

FastAPI tutorial Code for my video tutorial FastAPI tutorial What is FastAPI? FastAPI is a high-performant REST API framework for Python. It's built o

José Haro Peralta 8 Dec 16, 2022
Ready-to-use and customizable users management for FastAPI

FastAPI Users Ready-to-use and customizable users management for FastAPI Documentation: https://frankie567.github.io/fastapi-users/ Source Code: https

François Voron 2.4k Jan 01, 2023
Htmdf - html to pdf with support for variables using fastApi.

htmdf Converts html to pdf with support for variables using fastApi. Installation Clone this repository. git clone https://github.com/ShreehariVaasish

Shreehari 1 Jan 30, 2022
A simple web to serve data table. It is built with Vuetify, Vue, FastApi.

simple-report-data-table-vuetify A simple web to serve data table. It is built with Vuetify, Vue, FastApi. The main features: RBAC with casbin simple

11 Dec 22, 2022
📦 Autowiring dependency injection container for python 3

Lagom - Dependency injection container What Lagom is a dependency injection container designed to give you "just enough" help with building your depen

Steve B 146 Dec 29, 2022
Basic FastAPI starter with GraphQL, Docker, and MongoDB configurations.

FastAPI + GraphQL Starter A python starter project using FastAPI and GraphQL. This project leverages docker for containerization and provides the scri

Cloud Bytes Collection 1 Nov 24, 2022
Async and Sync wrapper client around httpx, fastapi, date stuff

lazyapi Async and Sync wrapper client around httpx, fastapi, and datetime stuff. Motivation This library is forked from an internal project that works

2 Apr 19, 2022
python template private service

Template for private python service This is a cookiecutter template for an internal REST API service, written in Python, inspired by layout-golang. Th

UrvanovCompany 15 Oct 02, 2022
A simple api written in python/fastapi that serves movies from a cassandra table.

A simple api written in python/fastapi that serves movies from a cassandra table. 1)clone the repo 2)rename sample_global_config_.py to global_config.

Sreeraj 1 Aug 26, 2021
All of the ad-hoc things you're doing to manage incidents today, done for you, and much more!

About What's Dispatch? Put simply, Dispatch is: All of the ad-hoc things you’re doing to manage incidents today, done for you, and a bunch of other th

Netflix, Inc. 3.7k Jan 05, 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
MLServer

MLServer An open source inference server to serve your machine learning models. ⚠️ This is a Work in Progress. Overview MLServer aims to provide an ea

Seldon 341 Jan 03, 2023
row level security for FastAPI framework

Row Level Permissions for FastAPI While trying out the excellent FastApi framework there was one peace missing for me: an easy, declarative way to def

Holger Frey 315 Dec 25, 2022