-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (54 loc) · 1.65 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import logging
from fastapi import FastAPI, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.encoders import jsonable_encoder
from fastapi.requests import Request
from fastapi.responses import ORJSONResponse
from fastapi.exceptions import RequestValidationError
from tortoise.contrib.fastapi import register_tortoise
from config import *
from auth import auth_route
__version__ = APP_VERSION
# debug tortoise
fmt = logging.Formatter(
fmt="%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
sh = logging.FileHandler('slow_query.log')
sh.setLevel(logging.DEBUG)
sh.setFormatter(fmt)
# will print debug sql
logger_db_client = logging.getLogger("db_client")
logger_db_client.setLevel(logging.DEBUG)
logger_db_client.addHandler(sh)
app = FastAPI(
debug=DEBUG,
title='',
description='',
version=__version__,
redoc_url=None
)
app.add_middleware(
CORSMiddleware,
allow_origins=ORIGINS,
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*']
)
app.include_router(
prefix='/auth',
router=auth_route,
tags=['auth']
)
register_tortoise(app,
db_url=DB_URL,
modules={'models': ['auth']},
add_exception_handlers=True)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return ORJSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=jsonable_encoder({"detail": exc.errors(), "body": exc.body, "req": {
'url': request.url, 'method': request.method
}}),
)