diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..00d2a0c --- /dev/null +++ b/.env.example @@ -0,0 +1,6 @@ +# mysql +BASE_HOST=127.0.0.1 +BASE_USER=root +BASE_PASSWORD=123456 +BASE_PORT=3306 +BASE_DB=base \ No newline at end of file diff --git a/IMG_1516.JPG b/IMG_1516.JPG deleted file mode 100644 index db0a7a7..0000000 Binary files a/IMG_1516.JPG and /dev/null differ diff --git a/app.py b/app.py index 4cf505a..62926ec 100644 --- a/app.py +++ b/app.py @@ -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, @@ -78,18 +79,29 @@ 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, @@ -97,7 +109,7 @@ async def redoc_html(): allow_methods=settings.CORS_ALLOW_METHODS, allow_headers=settings.CORS_ALLOW_HEADERS, ) - +# 添加中间件SessionMiddleware,用于处理HTTP会话。 application.add_middleware( SessionMiddleware, secret_key=settings.SECRET_KEY, @@ -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 diff --git a/database/mysql.py b/database/mysql.py index 5e67810..492c418 100644 --- a/database/mysql.py +++ b/database/mysql.py @@ -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 异常信息反馈。 + ''' diff --git a/database/redis.py b/database/redis.py index a6e1425..e7a1ac4 100644 --- a/database/redis.py +++ b/database/redis.py @@ -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), diff --git a/requirement.txt b/requirement.txt index d573f03..2157b70 100644 --- a/requirement.txt +++ b/requirement.txt @@ -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 \ No newline at end of file +sqlmodel==0.0.6 +wechatpy 2.0.0a26 \ No newline at end of file diff --git a/static/Route_planning.jpg b/static/Route_planning.jpg new file mode 100644 index 0000000..6cebff6 Binary files /dev/null and b/static/Route_planning.jpg differ diff --git a/static/templates/index.html b/static/templates/index.html index de06020..4171c78 100644 --- a/static/templates/index.html +++ b/static/templates/index.html @@ -6,5 +6,6 @@

门户首页

+ \ No newline at end of file diff --git a/views/viewpoints/home.py b/views/viewpoints/home.py index 40f87e9..18990d3 100644 --- a/views/viewpoints/home.py +++ b/views/viewpoints/home.py @@ -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}) +