Compare commits
17
Commits
nanfangyu
...
02b5e31307
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02b5e31307 | ||
|
|
8c048d65e4 | ||
|
|
ebdfb9016b | ||
|
|
1d085867d6 | ||
|
|
59fe699940 | ||
|
|
03f0761974 | ||
|
|
de755c3c26 | ||
|
|
8a131c1b7b | ||
|
|
71aafe32e9 | ||
|
|
50b30d6eb0 | ||
|
|
343a4e1532 | ||
|
|
a96ee13276 | ||
|
|
f31743d670 | ||
|
|
0f7a0dba2f | ||
|
|
6d76d6af48 | ||
|
|
ddbdd3f041 | ||
|
|
e234cd66da |
@@ -0,0 +1,8 @@
|
||||
#这是一个示例文件,确保api端口能被正常调用
|
||||
from fastapi import APIRouter
|
||||
|
||||
example_router = APIRouter()
|
||||
|
||||
@example_router.get("/example")
|
||||
def example():
|
||||
return {'hello DoubaoTeam'}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
"mysql+pymysql://root:123456@localhost/fastapi_db?charset=utf8mb4"
|
||||
|
||||
database_connect_user = 'root'
|
||||
database_connect_password = '123456'
|
||||
database_connect_ip = 'localhost'
|
||||
database_connect_database_name = 'student_manager'
|
||||
@@ -0,0 +1,33 @@
|
||||
'''
|
||||
1.此文件已经配置 engine 和 SessionLocal .
|
||||
由于还没合库,需要自己去 /core/database 配置库名。默认为 student_manager
|
||||
具体配置如下
|
||||
用户:root
|
||||
密码:123456
|
||||
IP:localhost
|
||||
库名:student_manager
|
||||
|
||||
2.如果还没开始建库,建库时统一将名称命名为 student_manager。
|
||||
如果已经建库或在 models 目录写好Base函数,那先别提交分支,不然合并的时候会出问题,后天合库的时候再说。
|
||||
'''
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from .config import (
|
||||
database_connect_user,
|
||||
database_connect_password,
|
||||
database_connect_ip,
|
||||
database_connect_database_name,
|
||||
)
|
||||
engine = create_engine( #engine负责连接mysql
|
||||
f"mysql+pymysql://{database_connect_user}:{database_connect_password}@{database_connect_ip}/{database_connect_database_name}?charset=utf8mb4",
|
||||
pool_size=50
|
||||
)
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False,autocommit=False)
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,26 @@
|
||||
from fastapi import FastAPI
|
||||
from api.example import example_router
|
||||
from middleware import log_middleware
|
||||
import uvicorn
|
||||
app = FastAPI()
|
||||
app.middleware("http")(log_middleware)
|
||||
|
||||
#-------------朱婷婷--------------
|
||||
|
||||
#-------------李玉杰--------------
|
||||
|
||||
#-------------张义---------------
|
||||
|
||||
#-------------薄鑫---------------
|
||||
|
||||
#-------------张昕浩---------------
|
||||
app.include_router(example_router)
|
||||
|
||||
#-------------曾凯---------------
|
||||
|
||||
#-------------南方宇---------------
|
||||
|
||||
#-----------主程序----------------
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(app, host="0.0.0.0", port=23333)
|
||||
@@ -0,0 +1,9 @@
|
||||
import time
|
||||
|
||||
async def log_middleware(request, call_next):
|
||||
start_time = time.time()
|
||||
response = await call_next(request)
|
||||
end_time = time.time()
|
||||
cost = end_time - start_time
|
||||
print(f'{request.method} {request.url.path} cost={cost}')
|
||||
return response
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
from sqlalchemy import *
|
||||
from datetime import datetime,time,date
|
||||
|
||||
Base = declarative_base() # 执行函数,返回一个基类
|
||||
|
||||
class Classes( Base ): # 在python里的名字
|
||||
__tablename__ = 'classes' # 在数据库中表的名字
|
||||
#————创建班级"主键"的字段名————
|
||||
id = Column( Integer # 声明字段的数据类型是"整数"
|
||||
, primary_key = True # 声明是"主键"
|
||||
, autoincrement = True # 声明是"自增主键"
|
||||
, nullable = False # 声明是"非空"
|
||||
)
|
||||
# ————创建班级"编号"的字段名————
|
||||
class_no = Column( String(50) # 声明字段的数据类型是"字符串,且最长为50个字符"
|
||||
, unique = True # 声明是"唯一约束"
|
||||
, nullable = False # 声明是"非空"
|
||||
)
|
||||
# ————创建"班级名称"的字段名————
|
||||
class_name = Column( String(100) # 声明字段的数据类型是"字符串,且最长为100个字符"
|
||||
, nullable = False # 声明是"非空"
|
||||
)
|
||||
# ————创建班级"开课时间"的字段名————
|
||||
start_date = Column( DATE # 声明是字段数据类型是日期
|
||||
, nullable = True # 声明是'可为空'
|
||||
)
|
||||
# ————创建班级"结课时间"的字段名————
|
||||
end_date = Column( DATE # 声明是字段数据类型是'日期'
|
||||
, nullable = True # 声明是"可为空"
|
||||
)
|
||||
# ————创建班级"创建时间"的字段名————
|
||||
created_at = Column( DATETIME # 声明是字段数据类型是"日期时间"
|
||||
, default=datetime.now # 声明是创建的默认值就是"当前时间"
|
||||
, nullable = False # 声明是"非空"
|
||||
)
|
||||
# ————创建班级"更新时间"的字段名————
|
||||
update_at = Column( DATETIME # 声明是字段数据类型是"日期时间"
|
||||
, default=datetime.now #声明是第一次更新的时间就是第一次创建的时间一致
|
||||
, onupdate=datetime.now # 声明"最后修改的时间"
|
||||
, nullable = False # 声明是"非空"
|
||||
)
|
||||
# ————创建班级"备注信息"的字段名————
|
||||
remark = Column( String(100) # 声明是字段数据类型是"字符串"
|
||||
, nullable = True # 声明是"可以为空"
|
||||
)
|
||||
# ————创建班级"逻辑删除"的字段名————
|
||||
is_deleted = Column( INTEGER # 声明是字段数据类型是"数字"
|
||||
, default = 0 # 声明默认值是"0,未删除"
|
||||
, nullable = False # 声明是"非空"
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
fastapi==0.128.0
|
||||
uvicorn==0.40.0
|
||||
SQLAlchemy==2.0.49
|
||||
PyMySQL==1.1.2
|
||||
pydantic==2.13.3
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
'''
|
||||
两种情况:
|
||||
1.响应成功,响应体设置为SuccessResponse。
|
||||
2.响应失败,需要主动调用HTTPExpection报错,不然会产生响应码200,响应失败的问题。
|
||||
'''
|
||||
from pydantic import BaseModel,Field
|
||||
from typing import Any
|
||||
class SuccessResponse(BaseModel):
|
||||
status_code:int=Field(default=200, ge= 200, lt = 300)
|
||||
msg: Any=Field(default='OK')
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
Reference in New Issue
Block a user