This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
155 lines (130 loc) · 3.87 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import base64
import os
import re
from datetime import tzinfo
from typing import List, Optional
import pytz
from aiocache import caches
from fastapi.openapi.utils import get_openapi
from pydantic import BaseSettings, Field
from pytz import UnknownTimeZoneError
from tortoise import Tortoise
from tortoise.contrib.fastapi import register_tortoise
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
def get_secret(name: str) -> Optional[str]:
"""
The function to get variables from docker secrets
:param name: the name of the docker secret
:return: the secret value after type cast
"""
try:
with open(os.path.join('/var/run/secrets', name), 'r', encoding='utf-8') as secret_file:
value = secret_file.read().rstrip('\n')
except Exception as e:
print(e)
return
return value
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: int = 10
site_name: str = 'Open Tree Hole'
domain: str = 'fduhole.com'
email_whitelist: List[str] = []
verification_code_expires: int = 10
email_user: str = ''
email_password: str = ''
email_host: str = ''
email_port: int = 465
email_use_tls: bool = True
email_public_key_path: str = 'data/treehole_demo_public.pem'
email_private_key_path: str = 'data/treehole_demo_private.pem'
register_apikey_seed: str = ''
kong_url: str = 'http://kong:8001'
kong_token: str = ''
authorize_in_debug: bool = True
redis_url: str = 'redis://redis:6379'
identifier_salt: str = str(base64.b64encode(b'123456'), 'utf-8')
provision_key: str = ''
config = Settings(tz=parse_tz())
SECRET_FIELDS = [
'db_url',
'email_password',
'register_apikey_seed',
'identifier_salt',
'provision_key',
'kong_token'
]
for name in SECRET_FIELDS:
value = get_secret(name)
if value:
setattr(config, name, value)
if not config.debug:
match = re.match(r'redis://(.+):(\d+)', config.redis_url)
assert match
caches.set_config({
'default': {
'cache': 'aiocache.RedisCache',
'endpoint': match.group(1),
'port': match.group(2),
'timeout': 5
}})
else:
caches.set_config({
'default': {
'cache': 'aiocache.SimpleMemoryCache'
}})
if config.mode != 'production':
print(f'server is running in {config.mode} mode, do not use in production')
if not config.email_whitelist:
print(f'email whitelist not set')
MODELS = ['models', 'shamir']
TORTOISE_ORM = {
'apps': {
'models': {
'models': ['aerich.models'] + MODELS
}
},
'connections': { # aerich 暂不支持 sqlite
'default': config.db_url
},
'use_tz': True,
'timezone': 'utc'
}
from main import app
if config.mode != 'test':
register_tortoise(
app,
config=TORTOISE_ORM,
generate_schemas=True,
add_exception_handlers=True,
)
Tortoise.init_models(MODELS, 'models')
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