Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# mysql
BASE_HOST=127.0.0.1
BASE_USER=root
BASE_PASSWORD=123456
BASE_PORT=3306
BASE_DB=base
Binary file removed IMG_1516.JPG
Binary file not shown.
25 changes: 24 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from fastapi.openapi.docs import (get_redoc_html, get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html)
from fastapi.openapi.utils import get_openapi

# 创建FastAPI应用实例,设置debug模式,并设置doc和redoc页面为None
application = FastAPI(
debug=settings.APP_DEBUG,
docs_url=None,
Expand Down Expand Up @@ -78,26 +79,37 @@ async def redoc_html():
application.add_event_handler("shutdown", Events.stopping(application))

# 异常错误处理

# 捕获HTTP异常
application.add_exception_handler(HTTPException, Exception.http_error_handler)
# 捕获请求验证错误
application.add_exception_handler(RequestValidationError, Exception.http422_error_handler)
# 捕获Unicorn异常
application.add_exception_handler(Exception.UnicornException, Exception.unicorn_exception_handler)
# 捕获MySQL异常:记录不存在
application.add_exception_handler(DoesNotExist, Exception.mysql_does_not_exist)
# 捕获MySQL异常:完整性错误
application.add_exception_handler(IntegrityError, Exception.mysql_integrity_error)
# 捕获MySQL异常:验证错误
application.add_exception_handler(ValidationError, Exception.mysql_validation_error)
# 捕获MySQL异常:操作错误
application.add_exception_handler(OperationalError, Exception.mysql_operational_error)



# 中间件
# 添加中间件BaseMiddleware,用于处理全局请求和响应。
application.add_middleware(Middleware.BaseMiddleware)

# 添加中间件CORSMiddleware,用于处理跨域资源共享(CORS)相关的请求和响应。
application.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=settings.CORS_ALLOW_CREDENTIALS,
allow_methods=settings.CORS_ALLOW_METHODS,
allow_headers=settings.CORS_ALLOW_HEADERS,
)

# 添加中间件SessionMiddleware,用于处理HTTP会话。
application.add_middleware(
SessionMiddleware,
secret_key=settings.SECRET_KEY,
Expand All @@ -112,4 +124,15 @@ async def redoc_html():
application.mount('/', StaticFiles(directory=settings.STATIC_DIR), name="static")
application.state.views = Jinja2Templates(directory=settings.TEMPLATE_DIR)

'''
fastapi的 state,可以向请求头里放一些东西
Request对象的app.state属性是一个存储应用程序级别(application-level)状态的对象。
这个属性通常在FastAPI应用程序的启动过程中设置,并且可以包含任意自定义的应用程序级别的数据。
app.state属性是一个字典,你可以将任何需要在整个应用程序中共享的数据存储在其中。
app.state属性没有固定的属性列表,因为它可以包含任何你认为需要在应用程序的各个部分之间共享的数据。
你可以根据你的需求自由地向其中添加属性。
通常,app.state属性用于存储应用程序的配置、共享的数据库连接池、缓存连接池、全局设置、第三方服务的客户端实例等。
这可以帮助你在不同的路由处理函数之间共享数据,而无需在每个函数中显式传递这些数据。
'''

app = application
5 changes: 5 additions & 0 deletions database/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ async def register_mysql(app: FastAPI):
generate_schemas=False,
add_exception_handlers=False,
)
'''
generate_schemas=True 检测数据库中相应表是否存在,是否已经有这个表,如果没有就自动创建这个表,如果有了也不会更改现有的表。
(实测,要先新建 base 数据库才好用)
add_exception_handlers=True 开启 mysql 异常信息反馈。
'''
3 changes: 3 additions & 0 deletions database/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ async def code_cache() -> Redis:
:return: cache 连接池
"""
# 从URL方式创建redis连接池
# 从环境变量中获取缓存池的URL,包括缓存服务器的主机名和端口,以及缓存数据库编号
# 创建一个aioredis连接池,从URL中

sys_cache_pool = aioredis.ConnectionPool.from_url(
f"redis://{os.getenv('CACHE_HOST', '127.0.0.1')}:{os.getenv('CACHE_PORT', 6379)}",
db=os.getenv('CACHE_DB', 1),
Expand Down
6 changes: 3 additions & 3 deletions requirement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ PyJWT==2.3.0
python-dotenv==0.19.1
tortoise-orm[aiomysql]==0.19.0
tencentcloud-sdk-python==3.0.618
qcloud-python-sts==3.1.1
wechatpy==2.0.0a11
qcloud-python-sts==3.1.5
qrcode[pil]==7.3.1
python-alipay-sdk==3.0.4
passlib==1.7.4
sqlmodel==0.0.6
sqlmodel==0.0.6
wechatpy 2.0.0a26
Binary file added static/Route_planning.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions static/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
</head>
<body>
<h3>门户首页</h3>
<img src="Route_planning.jpg" width="1500" />
</body>
</html>
8 changes: 8 additions & 0 deletions views/viewpoints/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ async def home(request: Request):
:param request:
:return:
"""
# app.py 中 ,application.state.views = Jinja2Templates(directory=settings.TEMPLATE_DIR)
#通常用法:
# templates = Jinja2Templates(directory=settings.TEMPLATE_DIR)
# return templates.TemplateResponse("index.html", {"request": request})
# return方法2:
# return templates.get_template("index.html").render({'request':request, 'id': id})

return request.app.state.views.TemplateResponse("index.html", {"request": request})