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
王小右
Social Distancing Detector using deep learning and capable to run on edge AI devices such as NVIDIA Jetson, Google Coral, and more.

Smart Social Distancing Smart Social Distancing Introduction Getting Started Prerequisites Usage Processor Optional Parameters Configuring AWS credent

Neuralet 129 Dec 12, 2022
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
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
Publish Xarray Datasets via a REST API.

Xpublish Publish Xarray Datasets via a REST API. Serverside: Publish a Xarray Dataset through a rest API ds.rest.serve(host="0.0.0.0", port=9000) Clie

xarray-contrib 106 Jan 06, 2023
A complete end-to-end machine learning portal that covers processes starting from model training to the model predicting results using FastAPI.

Machine Learning Portal Goal Application Workflow Process Design Live Project Goal A complete end-to-end machine learning portal that covers processes

Shreyas K 39 Nov 24, 2022
Analytics service that is part of iter8. Robust analytics and control to unleash cloud-native continuous experimentation.

iter8-analytics iter8 enables statistically robust continuous experimentation of microservices in your CI/CD pipelines. For in-depth information about

16 Oct 14, 2021
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
Recommend recipes based on what ingredients you have at home

🌱 MyChef 📦 Overview MyChef is an application that helps you decide what meal to make based on what you have at home. Simply enter in ingredients you

Logan Connolly 44 Nov 08, 2022
Python supercharged for the fastai library

Welcome to fastcore Python goodies to make your coding faster, easier, and more maintainable Python is a powerful, dynamic language. Rather than bake

fast.ai 810 Jan 06, 2023
signal-cli-rest-api is a wrapper around signal-cli and allows you to interact with it through http requests

signal-cli-rest-api signal-cli-rest-api is a wrapper around signal-cli and allows you to interact with it through http requests. Features register/ver

Sebastian Noel Lübke 31 Dec 09, 2022
Twitter API with fastAPI

Twitter API with fastAPI Content Forms Cookies and headers management Files edition Status codes HTTPExceptions Docstrings or documentation Deprecate

Juan Agustin Di Pasquo 1 Dec 21, 2021
FastAPI Skeleton App to serve machine learning models production-ready.

FastAPI Model Server Skeleton Serving machine learning models production-ready, fast, easy and secure powered by the great FastAPI by Sebastián Ramíre

268 Jan 01, 2023
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
ASGI middleware for authentication, rate limiting, and building CRUD endpoints.

Piccolo API Utilities for easily exposing Piccolo models as REST endpoints in ASGI apps, such as Starlette and FastAPI. Includes a bunch of useful ASG

81 Dec 09, 2022
api versioning for fastapi web applications

fastapi-versioning api versioning for fastapi web applications Installation pip install fastapi-versioning Examples from fastapi import FastAPI from f

Dean Way 472 Jan 02, 2023
sample web application built with FastAPI + uvicorn

SPARKY Sample web application built with FastAPI & Python 3.8 shows simple Flask-like structure with a Bootstrap template index.html also has a backgr

mrx 21 Jan 03, 2022
a lightweight web framework based on fastapi

start-fastapi Version 2021, based on FastAPI, an easy-to-use web app developed upon Starlette Framework Version 2020 中文文档 Requirements python 3.6+ (fo

HiKari 71 Dec 30, 2022
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
python fastapi example connection to mysql

Quickstart Then run the following commands to bootstrap your environment with poetry: git clone https://github.com/xiaozl/fastapi-realworld-example-ap

55 Dec 15, 2022
API & Webapp to answer questions about COVID-19. Using NLP (Question Answering) and trusted data sources.

This open source project serves two purposes. Collection and evaluation of a Question Answering dataset to improve existing QA/search methods - COVID-

deepset 329 Nov 10, 2022