This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
85 lines (65 loc) · 1.92 KB
/
config.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
from datetime import tzinfo
from typing import Optional
import pytz
from fastapi.openapi.utils import get_openapi
from pydantic import BaseSettings, Field
from pytz import UnknownTimeZoneError
from tortoise.contrib.fastapi import register_tortoise
from utils.patch import MyFastAPI
def default_debug() -> bool:
return os.getenv('MODE', 'dev') != 'production'
def parse_tz() -> tzinfo:
try:
return pytz.timezone(os.getenv('TZ', 'Asia/Shanghai'))
except UnknownTimeZoneError:
return pytz.UTC
class Settings(BaseSettings):
mode: str = 'dev'
debug: bool = Field(default_factory=default_debug)
tz: tzinfo = pytz.UTC
db_url: str = 'sqlite://db.sqlite3'
test_db: str = 'sqlite://:memory:'
default_size: Optional[int] = 10
max_size: Optional[int] = 30
config = Settings(tz=parse_tz())
app = MyFastAPI.get_app()
MODELS = ['user.models', 'bbs.models', 'admin.models']
TORTOISE_ORM = {
'apps': {
'models': {
'models': MODELS + ['aerich.models']
}
},
'connections': { # aerich 暂不支持 sqlite
'default': config.db_url
},
'use_tz': True,
'timezone': 'utc'
}
if config.mode != 'test':
register_tortoise(
app,
config=TORTOISE_ORM,
generate_schemas=True,
add_exception_handlers=True,
)
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title='OpenTreeHole Docs',
version="2.0.0",
description="OpenAPI doc for OpenTreeHole",
routes=app.routes
)
# look for the error 422 and removes it
for path in openapi_schema['paths'].values():
for method in path:
try:
del path[method]['responses']['422']
except KeyError:
pass
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi