FastAPI(六十五)实战开发《在线课程学习系统》基础架构的搭建
在之前三篇,我们分享的就是需求的分析,基本接口的整理,数据库链接的配置。这次我们分享项目的基本框架,目录结构如下:
common目录
通用的目录,一些通用的处理放在这里
models目录
数据库相关的放在这里
routers目录
所有的接口存放的地方
test
测试用例存放的地方
config.py 配置文件
Dockerfile docker打包的,后续部署看到
main.py 主运行文件
整体的架构梳理完毕后,我们就可以进行我们相关的开发了,这里我们在之前设计的数据库的相关的,可以放在对应的目录的文件中。
我们之前的数据库的设计的都可以放到对应的文件中,数据库的表格设计的都放在了models的models.py中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | from sqlalchemy import Column, Integer, String, ForeignKey, Boolean, Text, DateTime from datetime import datetime from config import EVENT if EVENT = = "test" : from models.testDatabase import Base, engine else : from models.database import Base, engine class User(Base): '''用户基础表''' __tablename__ = "users" id = Column(Integer, primary_key = True , index = True ) username = Column(String(length = 32 ), unique = True , index = True ) # 用户名 password = Column(String(length = 252 )) # 密码 status = Column(Integer, default = 0 ) # 1.删除,0正常 jobnum = Column(Integer, nullable = True ) # 工号 studentnum = Column(Integer, nullable = True ) # 学号 age = Column(Integer) # 年龄 sex = Column(String(length = 8 ), default = "男" ) # 性别 role = Column(Integer) # 角色 addtime = Column(DateTime,default = datetime.now()) class Role(Base): '''角色表''' __tablename__ = "roles" id = Column(Integer, primary_key = True , index = True ) name = Column(String(length = 8 ), unique = True , index = True ) # 角色名称 class Course(Base): '''课程表''' __tablename__ = "courses" id = Column(Integer, primary_key = True , index = True ) name = Column(String(length = 252 ), unique = True , index = True ) # 课程名称 icon = Column(String(length = 252 ), nullable = True ) # icon desc = Column(String(length = 252 ), nullable = True ) # 描述 status = Column(Boolean, default = False ) # 状态 onsale = Column(Boolean, default = False ) # 是否上架 catalog = Column(Text, nullable = True ) # 目录 owner = Column(Integer, ForeignKey( 'users.id' )) # 拥有者 likenum = Column(Integer, default = 0 ) # 点赞数 class Studentcourse(Base): '''学生课程表''' __tablename__ = "studentcourses" id = Column(Integer, primary_key = True , index = True ) students = Column(Integer, ForeignKey( 'users.id' )) # 学生 course = Column(Integer, ForeignKey( 'courses.id' )) # 课程 addtime = Column(DateTime, default = datetime.now()) # 加入时间 updatetime = Column(DateTime, default = addtime) # 更新时间 status = Column(Integer, default = 0 ) # 1.删除,0正常 class Commentcourse(Base): '''课程评论''' __tablename__ = "commentcourses" id = Column(Integer, primary_key = True , index = True ) course = Column(Integer, ForeignKey( 'courses.id' )) # 课程id users = Column(Integer, ForeignKey( 'users.id' )) # 评论人 pid = Column(Integer) # 回复。 addtime = Column(DateTime, default = datetime.now()) # 添加时间 top = Column(Boolean, default = False ) # 是否置顶 context = Column(Text) status = Column(Integer, default = 0 ) # 1.删除,0正常 class Message(Base): '''消息表''' __tablename__ = "messages" id = Column(Integer, primary_key = True , index = True ) senduser = Column(Integer, ForeignKey( 'users.id' )) # 发送者 acceptusers = Column(Integer, ForeignKey( 'users.id' )) # 接受者 read = Column(Boolean, default = False ) # 是否已读,接受者是否已读 sendtime = Column(String(length = 252 )) # 发送时间 pid = Column(Integer) addtime = Column(DateTime, default = datetime.now()) # 添加时间 context = Column(Text) status = Column(Integer, default = 0 ) # 1.删除,0正常 Base.metadata.create_all(bind = engine) |
那么之前的数据库配置的就可以放在对应的database中。
对应的链接的链接数据库的,存放在get_db.py中
from models.database import * from models.testDatabase import TestingSessionLocal from config import EVENT def get_test_db(): db = TestingSessionLocal() try: yield db finally: db.close() def get_db_pro(): """ 每一个请求处理完毕后会关闭当前连接,不同的请求使用不同的连接 :return: """ db = TestingSessionLocal() try: yield db finally: db.close() if EVENT == "test": get_db = get_test_db else: get_db = get_db_pro
那么我们剩下的开发中,只需要关注models的crud.py和对应的schemas.py。在common的是我们json统一处理,和log处理。对应的可以参考
FastAPI 学习之路(五十九)封装统一的json返回处理工具

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?