增加docker文件

This commit is contained in:
zhl
2026-09-21 20:43:32 +08:00
parent ef88fbdbe9
commit a061f1ab8b
5 changed files with 100 additions and 6 deletions
+14 -4
View File
@@ -1,5 +1,6 @@
# main0814.py
# 项目入口文件:创建 FastAPI 应用、注册路由、创建数据库表
from contextlib import asynccontextmanager
from fastapi import FastAPI
from pathlib import Path
from fastapi.responses import FileResponse, JSONResponse
@@ -11,11 +12,20 @@ from starlette.middleware.cors import CORSMiddleware
from api import student_api, advisor_api, employment_api, score_api, class_api, teacher_api
from init_db import init_all, drop_tables
# 1. 应用生命周期:启动时自动建表(幂等,已有表不会重建)
@asynccontextmanager
async def lifespan(app: FastAPI):
init_all(False, False) # 不重置、不灌种子,仅保证表存在
yield
# 2. 创建 FastAPI 实例
app = FastAPI(
title="FastAPI + SQLAlchemy 分层架构(MySQL)",
description="用户管理示例,演示分层架构和 MySQL 集成",
version="1.0.0"
version="1.0.0",
lifespan=lifespan,
)
# 3. 添加跨域中间件(允许前端跨域请求)
@@ -94,7 +104,7 @@ if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:app", # 指定应用位置(模块名:应用变量名)
host="0.0.0.0", # 监听所有网络接口
port=8004, # 端口
reload=True # 开发模式,代码变动自动重启
host="127.0.0.1", # 只监听本机,终端里的链接可直接点击打开
port=8005, # 端口(与 Dockerfile / docker-compose 一致)
reload=True # 开发模式,代码变动自动重启
)