This repository was archived by the owner on Dec 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
352 lines (275 loc) · 8.69 KB
/
Copy pathcore.py
File metadata and controls
352 lines (275 loc) · 8.69 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import datetime
from typing import overload
import typing as t
import orjson
from collections import OrderedDict
from dotenv import load_dotenv
from quart import Quart, Blueprint, Response
import os
import uvloop
import asyncio
import multiprocessing
import logging
from colorama import Fore, Style, init
from quart_cors import cors
import xxhash
from utils_cy.validate import Validator
from io import BytesIO
from gzip import GzipFile
import brotli # type: ignore
_logger = logging.getLogger("linkverse")
worker_count = int(os.getenv('_WORKER_COUNT', '1'))
server_id = int(os.getenv("SERVER_ID", "0"))
total_servers = int(os.getenv("TOTAL_SERVERS", "1"))
init(autoreset=True)
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
T = t.TypeVar("T")
load_dotenv()
app = Quart(__name__)
compress_config = {
"mimetypes": [
"text/html",
"text/css",
"text/xml",
"application/json",
"application/javascript",
],
"compress_level": 6,
"min_size": 500
}
allow_headers = [
"Upgrade", "Connection",
"Sec-WebSocket-Key", "Sec-WebSocket-Version",
"Origin", "Sec-WebSocket-Protocol",
"Content-Type", "Authorization"
]
app = cors(
app, allow_origin=["*", "app.sharinflame.com"],
allow_headers=allow_headers,
max_age=86400
)
secret_key = os.environ["SECRET_KEY"]
secret_refresh_key = os.environ["SECRET_REFRESH_KEY"]
async def await_if_cor(v_or_cor: t.Awaitable[T] | T) -> T:
if asyncio.iscoroutine(v_or_cor):
return await v_or_cor
else:
return v_or_cor # pyright: ignore[reportReturnType]
async def compress(data, algorithm: str = "br") -> bytes:
if algorithm == "gzip":
return await asyncio.to_thread(compress_gzip, data)
elif algorithm == "br":
return await asyncio.to_thread(compress_brotli, data)
else:
raise ValueError("Unknown algorithm, available: gzip, br")
def compress_gzip(data) -> bytes:
gzip_buffer = BytesIO()
with GzipFile(
mode="wb",
compresslevel=compress_config["compress_level"],
fileobj=gzip_buffer,
) as gzip_file:
gzip_file.write(data)
return gzip_buffer.getvalue()
def compress_brotli(data) -> bytes:
return brotli.compress(data)
@overload
def response(
*, data: t.Mapping = ...
) -> Response:
...
@overload
def response(
*, error: t.Literal[True], data: t.Mapping = {},
error_msg: str,
**kwargs
) -> Response:
...
@overload
def response(
*, data: t.Mapping = ..., **kwargs
) -> Response:
...
@overload
def response(
*, is_empty: t.Literal[True], **kwargs
) -> Response:
...
def response(
*, error: bool | None = None,
data: t.Mapping = {},
error_msg: str | None = None,
cache: bool = False,
private: bool = True,
keep_none: bool = False,
is_empty: bool = False,
**kwargs
) -> Response:
if is_empty:
response = Response()
response.headers.clear()
response.set_data(b"")
return response
if not keep_none:
data = remove_none_values(data)
data = convert_datetime_to_timestamp(data)
response_data = {
"success": not error,
"data": data
}
if error_msg:
response_data["error"] = error_msg
response = Response(
orjson.dumps(response_data),
content_type="application/json",
**kwargs
)
if cache:
response.cache_control.private = True if private else None
response.cache_control.public = not private
response.cache_control.must_revalidate = True
response.set_etag(generate_etag(dict(data) | dict(response.headers)))
else:
response.cache_control.no_cache = True
response.cache_control.no_store = True
response.cache_control.must_revalidate = True
return response
def convert_datetime_to_timestamp(d: t.Any) -> t.Any:
if isinstance(d, dict):
return {k: convert_datetime_to_timestamp(v) for k, v in d.items()}
elif isinstance(d, list):
return [convert_datetime_to_timestamp(v) for v in d]
elif isinstance(d, datetime.datetime):
return int(d.timestamp())
else:
return d
def remove_none_values(d: t.Any) -> t.Any:
if isinstance(d, dict):
return {k: remove_none_values(v) for k, v in d.items()
if v is not None}
elif isinstance(d, list):
return [remove_none_values(v) for v in d]
else:
return d
def generate_etag(data: dict | str) -> str:
if isinstance(data, dict):
sorted_data = OrderedDict(sorted(data.items()))
return xxhash.xxh64(orjson.dumps(sorted_data)).hexdigest()
else:
return xxhash.xxh64(data.encode()).hexdigest()
class FunctionError(Exception):
def __init__(
self, message: str | None, code: int, data: dict | None, *args
) -> None:
self.message = message or "UNKNOWN_ERROR"
self.code = code
self.data = data
super().__init__(message, *args)
def response(self) -> tuple[Response, int]:
return response(
error=True,
data=self.data or {},
error_msg=self.message
), self.code
def get_proc_identity() -> int:
_id = multiprocessing.current_process()._identity
if _id:
return _id[0]
else:
return 0
def is_systemd() -> bool:
return os.getenv("INVOCATION_ID") is not None
class ColoredFormatter(logging.Formatter):
COLORS = {
logging.DEBUG: Fore.WHITE,
logging.INFO: Fore.GREEN,
logging.WARNING: Fore.YELLOW,
logging.ERROR: Fore.RED,
logging.CRITICAL: Fore.MAGENTA,
}
def format(self, record: logging.LogRecord) -> str:
log_color = self.COLORS.get(record.levelno, Fore.WHITE)
levelname = record.levelname[0]
record_name = record.name.removeprefix("linkverse.")
formatted_message = super().format(record)
return (
log_color +
f"{levelname}{Style.BRIGHT} [{record_name}] " +
formatted_message +
Style.RESET_ALL
)
def setup_logger(logger: logging.Logger | None = None):
global _logger
logger = logger or _logger
handler = logging.StreamHandler()
id = f"[{get_proc_identity()}/{worker_count}|" \
f"{server_id + 1}/{total_servers}]"
if is_systemd():
fmt = f'{id}: %(message)s'
else:
fmt = f'%(asctime)s [%(process)d] {id}: %(message)s'
formatter = ColoredFormatter(fmt, datefmt='%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
return logger
def get_module_name(file_path: str) -> str:
base_package = __package__.replace('.', os.sep) if __package__ else ""
relative_path = os.path.relpath(file_path, start=base_package)
module_path = os.path.splitext(relative_path)[0]
return module_path.replace(os.sep, '.')
def route(_app: Quart | Blueprint, url_rule: str, **kwargs):
url_rule = f"/v1/{url_rule.lstrip("/")}"
return _app.route(url_rule, **kwargs)
ReturnType = tuple[bool, t.Any | None]
def validate(
value: t.Any, options: dict,
is_parameters: bool = False
) -> ReturnType:
if value is None:
if options.get("allow_none"):
return True, None
return False, None
validator = Validator(options)
type = options.get("type", "str")
_validate = getattr(
validator,
(f"validate_{type}" if not is_parameters
else f"parameters_{type}"),
validator.validate_str
)
try:
_result = _validate(value)
except TypeError:
_result = False, None
return _result[0], value if _result[1] is None else _result[1]
def are_all_keys_present(source: dict, target: dict) -> bool:
return all(key in target for key in source)
def flatten_dict(
d: dict,
parent_key: str = '',
level: int = 0,
max_levels: int = 2
) -> dict:
if level >= max_levels:
return {parent_key: d} if parent_key else d
if parent_key and all(isinstance(v, dict) for v in d.values()):
new_dict = {}
for k, v in d.items():
new_key = f"{parent_key}.{k}"
new_dict.update(flatten_dict(v, new_key, level + 1, max_levels))
return new_dict
else:
result = {}
for k, v in d.items():
if isinstance(v, dict):
if all(isinstance(val, dict) for val in v.values()):
new_key = f"{parent_key}.{k}" if parent_key else k
result.update(
flatten_dict(v, new_key, level + 1, max_levels)
)
else:
result[k] = v
else:
result[k] = v
return {parent_key: result} if parent_key else result