-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
238 lines (190 loc) · 7.04 KB
/
config.py
File metadata and controls
238 lines (190 loc) · 7.04 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import os
from pathlib import Path
from urllib.parse import urlparse
from dotenv import load_dotenv
env_path = os.getenv("DOTENV_PATH", os.path.join(os.path.dirname(__file__), ".env"))
if os.getenv("LOAD_DOTENV", "true").lower() not in ("0", "false", "no"):
load_dotenv(dotenv_path=env_path)
BASE_PATH: Path = Path(__file__).resolve().parent
DB_PATH: Path = BASE_PATH / "database"
UPLOAD_FOLDER: Path = DB_PATH / "uploads"
CHATS_FOLDER: Path = DB_PATH / "chats"
TELEGRAM_CHATS_FOLDER: Path = DB_PATH / "telegram_chats"
CREATE_IMAGE_FOLDER: Path = DB_PATH / "generated_images"
LOGS_FOLDER: Path = BASE_PATH / "logs"
for folder in [UPLOAD_FOLDER, CHATS_FOLDER, TELEGRAM_CHATS_FOLDER, CREATE_IMAGE_FOLDER, LOGS_FOLDER]:
folder.mkdir(parents=True, exist_ok=True)
try:
MAX_CONTENT_LENGTH: int = int(os.getenv("MAX_CONTENT_LENGTH", 10 * 1024 * 1024))
except ValueError:
MAX_CONTENT_LENGTH: int = 10 * 1024 * 1024
ALLOWED_IMAGE_EXTENSIONS = {"png", "jpg", "jpeg", "webp", "gif"}
DEFAULT_LANGUAGE: str = "ru"
def _sqlite_directory_usable(folder: Path) -> bool:
try:
folder.mkdir(parents=True, exist_ok=True)
probe = folder / f".sqlite_probe_{os.getpid()}.tmp"
probe.write_text("ok", encoding="utf-8")
probe.unlink()
return True
except Exception:
return False
def _normalize_host_entry(value: str) -> str:
raw = (value or "").strip()
if not raw:
return ""
if raw.startswith("."):
return raw.lower()
candidate = raw
if "://" in raw:
parsed = urlparse(raw)
candidate = parsed.hostname or ""
else:
parsed = urlparse(f"//{raw}")
candidate = parsed.hostname or raw
candidate = candidate.split("/")[0].split("?")[0].split("#")[0].strip()
if not candidate:
return ""
return candidate.lower()
ALLOWED_HOSTS = []
for host in ("127.0.0.1", "localhost"):
normalized = _normalize_host_entry(host)
if normalized:
ALLOWED_HOSTS.append(normalized)
if os.getenv("ALLOWED_HOSTS"):
for raw_host in os.getenv("ALLOWED_HOSTS").split(","):
normalized = _normalize_host_entry(raw_host)
if normalized and normalized not in ALLOWED_HOSTS:
ALLOWED_HOSTS.append(normalized)
CORS_ORIGINS = [
"http://localhost:3000",
"http://localhost:8000",
"http://127.0.0.1:3000",
"http://127.0.0.1:8000",
"http://localhost:5000",
"http://localhost:5173",
"http://127.0.0.1:5173",
]
if os.getenv("CORS_ORIGINS"):
CORS_ORIGINS.extend([o.strip() for o in os.getenv("CORS_ORIGINS").split(",") if o.strip()])
CORS_ORIGINS = [o for o in CORS_ORIGINS if o != "*"]
CORS_ALLOW_HEADERS = [
"Content-Type",
"Authorization",
"X-Requested-With",
"X-CSRF-Token",
"X-Request-Id",
"Accept",
"Accept-Language",
]
CORS_EXPOSE_HEADERS = [
"Content-Type",
"X-CSRF-Token",
"X-Request-Id",
"X-Total-Count",
"X-Page-Number",
]
CORS_METHODS = ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"]
CORS_MAX_AGE = 3600
CORS_ALLOW_CREDENTIALS = True
CORS_SEND_WILDCARD = False
CORS_ALWAYS_SEND = True
SERVER_THREADS: int = 8
SERVER_CONNECTION_LIMIT: int = 200
SERVER_CHANNEL_TIMEOUT: int = 300
SECRET_KEY = os.getenv("SECRET_KEY")
_database_url = os.getenv("DATABASE_URL")
if _database_url:
_db_url = _database_url.strip()
if _db_url.startswith("sqlite:///"):
_sqlite_target = _db_url[len("sqlite:///") :]
if _sqlite_target and _sqlite_target != ":memory:":
if not Path(_sqlite_target).is_absolute():
_sqlite_path = (BASE_PATH / _sqlite_target).resolve()
target_parent = _sqlite_path.parent
else:
_sqlite_path = Path(_sqlite_target)
target_parent = _sqlite_path.parent
if not _sqlite_directory_usable(target_parent):
fallback_sqlite_path = (BASE_PATH / "users.db").resolve()
_sqlite_path = fallback_sqlite_path
_sqlite_path.parent.mkdir(parents=True, exist_ok=True)
SQLALCHEMY_DATABASE_URI = f"sqlite:///{_sqlite_path.as_posix()}"
else:
SQLALCHEMY_DATABASE_URI = _db_url
else:
SQLALCHEMY_DATABASE_URI = _db_url
else:
SQLALCHEMY_DATABASE_URI = f"sqlite:///{(DB_PATH / 'users.db').as_posix()}"
SQLALCHEMY_TRACK_MODIFICATIONS = False
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
CELERY_BROKER_URL = os.getenv("CELERY_BROKER_URL", "redis://localhost:6379/1")
CELERY_RESULT_BACKEND = os.getenv("CELERY_RESULT_BACKEND", "redis://localhost:6379/1")
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
BACKEND_URL = os.getenv("BACKEND_URL", None)
if BACKEND_URL:
backend_host = _normalize_host_entry(BACKEND_URL)
if backend_host and backend_host not in ALLOWED_HOSTS:
ALLOWED_HOSTS.append(backend_host)
SESSION_COOKIE_DOMAIN = (os.getenv("SESSION_COOKIE_DOMAIN") or "").strip() or None
SESSION_COOKIE_NAME = (
os.getenv("SESSION_COOKIE_NAME") or "remind_session"
).strip() or "remind_session"
TURNSTILE_SITE_KEY = os.getenv("TURNSTILE_SITE_KEY", "")
TURNSTILE_SECRET_KEY = os.getenv("TURNSTILE_SECRET_KEY", "")
TURNSTILE_VERIFY_URL = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
LOCALHOST_MODE = os.getenv("LOCALHOST_MODE", "False").lower() in ("1", "true", "yes")
if not SECRET_KEY:
if os.environ.get("FLASK_ENV") == "production":
raise ValueError("SECRET_KEY must be set in production environment")
import secrets
SECRET_KEY = secrets.token_hex(32)
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
GEMINI_MODEL_NAME = os.getenv("GEMINI_MODEL_NAME")
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN", "").strip()
TELEGRAM_BOT_DEFAULT_MODEL = os.getenv("TELEGRAM_BOT_DEFAULT_MODEL", "gemini").strip() or "gemini"
_flask_debug = os.getenv("FLASK_DEBUG", "0")
_is_debug = _flask_debug == "1" or _flask_debug.lower() == "true"
IS_PRODUCTION = not _is_debug and os.getenv("FLASK_ENV") != "development"
DEBUG_MODE = _is_debug
TESTING = False
try:
PIL_AVAILABLE = True
except Exception:
PIL_AVAILABLE = False
USER_AGENT: str = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/124.0 Safari/537.36 ReMindBot/1.0"
)
ALLOWED_USER_AGENT_PATTERNS = [
r"Mozilla/5\.0.*Chrome/.*Safari",
r"Mozilla/5\.0.*Firefox/",
r"Mozilla/5\.0.*Safari/.*Version/",
r"Mozilla/5\.0.*Edg/",
r"Mozilla/5\.0.*OPR/",
r"AppleWebKit/.*Safari",
]
VALIDATE_USER_AGENT: bool = os.getenv("VALIDATE_USER_AGENT", "True").lower() in (
"1",
"true",
"yes",
)
BYPASS_USER_AGENT_VALIDATION_ROUTES = [
r"^/health",
r"^/metrics",
r"^/openapi\\.json$",
r"^/status",
r"^/$",
r"^/index\.html$",
]
ALLOW_GUEST_CHATS_SAVE: bool = os.getenv("ALLOW_GUEST_CHATS_SAVE", "False").lower() in (
"1",
"true",
"yes",
)
ENABLE_STRICT_HTTPS = os.getenv(
"ENABLE_STRICT_HTTPS", "true" if IS_PRODUCTION else "false"
).lower() in ("1", "true", "yes")
RATELIMIT_ENABLED = True
RATELIMIT_STORAGE_URL = os.getenv("REDIS_URL", "memory://")