1 Star 0 Fork 1

toliong/Fasdapi

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
app.py 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
binkuolo 提交于 2022-04-30 16:20 . fasdapi-rabc权限-JWT
# -*- coding:utf-8 -*-
"""
@Created on : 2022/4/22 22:02
@Author: binkuolo
@Des: app运行时文件
"""
from fastapi import FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
from config import settings
from fastapi.staticfiles import StaticFiles
from core.Router import AllRouter
from core.Events import startup, stopping
from core.Exception import http_error_handler, http422_error_handler, unicorn_exception_handler, UnicornException, \
mysql_operational_error, mysql_does_not_exist
from core.Middleware import Middleware
from fastapi.templating import Jinja2Templates
from tortoise.exceptions import OperationalError, DoesNotExist
application = FastAPI(
debug=settings.APP_DEBUG,
description=settings.DESCRIPTION,
version=settings.VERSION,
title=settings.PROJECT_NAME
)
# 事件监听
application.add_event_handler("startup", startup(application))
application.add_event_handler("shutdown", stopping(application))
# 异常错误处理
application.add_exception_handler(HTTPException, http_error_handler)
application.add_exception_handler(RequestValidationError, http422_error_handler)
application.add_exception_handler(UnicornException, unicorn_exception_handler)
application.add_exception_handler(DoesNotExist, mysql_does_not_exist)
application.add_exception_handler(OperationalError, mysql_operational_error)
# 路由
application.include_router(AllRouter)
# 中间件
application.add_middleware(Middleware)
application.add_middleware(
SessionMiddleware,
secret_key=settings.SECRET_KEY,
session_cookie=settings.SESSION_COOKIE,
max_age=settings.SESSION_MAX_AGE
)
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,
)
# 静态资源目录
application.mount('/static', StaticFiles(directory=settings.STATIC_DIR), name="static")
application.state.views = Jinja2Templates(directory=settings.TEMPLATE_DIR)
app = application
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/toliong/fasdapi.git
git@gitee.com:toliong/fasdapi.git
toliong
fasdapi
Fasdapi
master

搜索帮助