GNServer 0.0.0.0.4__py3-none-any.whl → 0.0.0.0.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- GNServer/_app.py +40 -15
- GNServer/models.py +25 -0
- {gnserver-0.0.0.0.4.dist-info → gnserver-0.0.0.0.6.dist-info}/METADATA +1 -1
- gnserver-0.0.0.0.6.dist-info/RECORD +8 -0
- gnserver-0.0.0.0.4.dist-info/RECORD +0 -7
- {gnserver-0.0.0.0.4.dist-info → gnserver-0.0.0.0.6.dist-info}/WHEEL +0 -0
- {gnserver-0.0.0.0.4.dist-info → gnserver-0.0.0.0.6.dist-info}/licenses/LICENSE +0 -0
- {gnserver-0.0.0.0.4.dist-info → gnserver-0.0.0.0.6.dist-info}/top_level.txt +0 -0
GNServer/_app.py
CHANGED
@@ -44,6 +44,12 @@ console = logging.StreamHandler()
|
|
44
44
|
console.setLevel(logging.INFO)
|
45
45
|
console.setFormatter(logging.Formatter("[GNServer] %(name)s: %(levelname)s: %(message)s"))
|
46
46
|
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
47
53
|
def guess_type(filename: str) -> str:
|
48
54
|
"""
|
49
55
|
Возвращает актуальный MIME-тип по расширению файла.
|
@@ -112,6 +118,7 @@ class Route:
|
|
112
118
|
param_types: dict[str, Callable[[str], Any]]
|
113
119
|
handler: Callable[..., Any]
|
114
120
|
name: str
|
121
|
+
cors: Optional[gn.CORSObject]
|
115
122
|
|
116
123
|
_PARAM_REGEX: dict[str, str] = {
|
117
124
|
"str": r"[^/]+",
|
@@ -193,8 +200,9 @@ def _ensure_async(fn: Callable[..., Any]) -> Callable[..., Any]:
|
|
193
200
|
class App:
|
194
201
|
def __init__(self):
|
195
202
|
self._routes: List[Route] = []
|
203
|
+
self._cors: Optional[gn.CORSObject] = None
|
196
204
|
|
197
|
-
def route(self, method: str, path: str,
|
205
|
+
def route(self, method: str, path: str, cors: Optional[gn.CORSObject] = None):
|
198
206
|
def decorator(fn: Callable[..., Any]):
|
199
207
|
regex, param_types = _compile_path(path)
|
200
208
|
self._routes.append(
|
@@ -204,26 +212,28 @@ class App:
|
|
204
212
|
regex,
|
205
213
|
param_types,
|
206
214
|
_ensure_async(fn),
|
207
|
-
|
215
|
+
fn.__name__,
|
216
|
+
cors
|
208
217
|
)
|
209
218
|
)
|
210
219
|
return fn
|
211
220
|
return decorator
|
212
221
|
|
213
|
-
def get(self, path: str, *,
|
214
|
-
return self.route("GET", path,
|
222
|
+
def get(self, path: str, *, cors: Optional[gn.CORSObject] = None):
|
223
|
+
return self.route("GET", path, cors)
|
215
224
|
|
216
|
-
def post(self, path: str, *,
|
217
|
-
return self.route("POST", path,
|
225
|
+
def post(self, path: str, *, cors: Optional[gn.CORSObject] = None):
|
226
|
+
return self.route("POST", path, cors)
|
218
227
|
|
219
|
-
def put(self, path: str, *,
|
220
|
-
return self.route("PUT", path,
|
228
|
+
def put(self, path: str, *, cors: Optional[gn.CORSObject] = None):
|
229
|
+
return self.route("PUT", path, cors)
|
221
230
|
|
222
|
-
def delete(self, path: str, *,
|
223
|
-
return self.route("DELETE", path,
|
231
|
+
def delete(self, path: str, *, cors: Optional[gn.CORSObject] = None):
|
232
|
+
return self.route("DELETE", path, cors)
|
233
|
+
|
234
|
+
def setRouteCors(self, cors: Optional[gn.CORSObject] = None):
|
235
|
+
self._cors = cors
|
224
236
|
|
225
|
-
def custom(self, method: str, path: str, *, name: str | None = None):
|
226
|
-
return self.route(method, path, name=name)
|
227
237
|
|
228
238
|
|
229
239
|
async def dispatch(
|
@@ -278,15 +288,19 @@ class App:
|
|
278
288
|
|
279
289
|
|
280
290
|
def static(self, path: str, dir_path: str):
|
281
|
-
@self.get(f"
|
291
|
+
@self.get(f"{path}/{{_path:path}}")
|
282
292
|
async def r_static(_path: str):
|
283
293
|
file_path = os.path.join(dir_path, _path)
|
294
|
+
|
295
|
+
if file_path.endswith('/'):
|
296
|
+
file_path = file_path[:-1]
|
297
|
+
|
284
298
|
if not os.path.isfile(file_path):
|
285
299
|
return gn.GNResponse('gn:backend:404')
|
286
300
|
|
287
301
|
mime_type = guess_type(file_path.split('/')[-1])
|
288
302
|
async with aiofiles.open(file_path, "rb") as f:
|
289
|
-
data = f.read()
|
303
|
+
data = await f.read()
|
290
304
|
|
291
305
|
return gn.GNResponse('ok', {'files': [{'mime-type': mime_type, 'data': data}]})
|
292
306
|
|
@@ -397,6 +411,7 @@ class App:
|
|
397
411
|
try:
|
398
412
|
|
399
413
|
response = await self._api.dispatch(request)
|
414
|
+
|
400
415
|
|
401
416
|
response = await self.resolve_extra_response(response)
|
402
417
|
|
@@ -404,18 +419,21 @@ class App:
|
|
404
419
|
if inspect.isasyncgen(response):
|
405
420
|
async for chunk in response: # type: ignore[misc]
|
406
421
|
chunk._stream = True
|
422
|
+
chunk = self.resolve_response(chunk)
|
407
423
|
self._quic.send_stream_data(request.stream_id, chunk.serialize(3), end_stream=False)
|
408
424
|
self.transmit()
|
409
425
|
|
410
426
|
l = gn.GNResponse('gn:end-stream')
|
411
427
|
l._stream = True
|
428
|
+
l = self.resolve_response(l)
|
412
429
|
self._quic.send_stream_data(request.stream_id, l.serialize(3), end_stream=True)
|
413
430
|
self.transmit()
|
414
431
|
return
|
415
432
|
|
416
433
|
|
434
|
+
response = self.resolve_response(response)
|
417
435
|
self._quic.send_stream_data(request.stream_id, response.serialize(3), end_stream=True)
|
418
|
-
logger.debug(f'Отправлен на сервер ответ -> {response.command
|
436
|
+
logger.debug(f'Отправлен на сервер ответ -> {response.command} {response.payload if response.payload and len((response.payload)) < 200 else ''}')
|
419
437
|
self.transmit()
|
420
438
|
except Exception as e:
|
421
439
|
logger.error('GNServer: error\n' + traceback.format_exc())
|
@@ -424,6 +442,13 @@ class App:
|
|
424
442
|
self._quic.send_stream_data(request.stream_id, response.serialize(3), end_stream=True)
|
425
443
|
self.transmit()
|
426
444
|
|
445
|
+
async def resolve_response(self, response: gn.GNResponse) -> gn.GNResponse:
|
446
|
+
if response._cors is None:
|
447
|
+
response._cors = self._api._cors
|
448
|
+
|
449
|
+
return response
|
450
|
+
|
451
|
+
|
427
452
|
async def resolve_extra_response(self, response: Union[gn.GNResponse, AsyncGenerator[gn.GNResponse, None]]) -> Union[gn.GNResponse, AsyncGenerator[gn.GNResponse, None]]:
|
428
453
|
|
429
454
|
file_types = (
|
GNServer/models.py
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
GNServer/__init__.py,sha256=T6kT6WoJpmIXashMSHfuafb9DSePUIzw192h27j9a4M,1364
|
2
|
+
GNServer/_app.py,sha256=8JcVzozjtdLtQjPThi-wR2OaAYNvYfaIqDReCmmFmrg,17801
|
3
|
+
GNServer/models.py,sha256=3HTbPgXMcltK3hh3RWii0z6X2qYitXmPWzud8pOc4Rk,50
|
4
|
+
gnserver-0.0.0.0.6.dist-info/licenses/LICENSE,sha256=WH_t7dKZyWJ5Ld07eYIkUG4Tv6zZWXtAdsUqYAUesn0,1084
|
5
|
+
gnserver-0.0.0.0.6.dist-info/METADATA,sha256=S5wZWPijiIAS9Kd8BkMeKs7PJtY0CMeMVvp149dQyhI,804
|
6
|
+
gnserver-0.0.0.0.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
7
|
+
gnserver-0.0.0.0.6.dist-info/top_level.txt,sha256=-UOUBuD4u7Qkb1o5PdcwyA3kx8xCH2lwy0tJHi26Wb4,9
|
8
|
+
gnserver-0.0.0.0.6.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
GNServer/__init__.py,sha256=T6kT6WoJpmIXashMSHfuafb9DSePUIzw192h27j9a4M,1364
|
2
|
-
GNServer/_app.py,sha256=Pnc0-KmepGNRsTINjSSOuSr_W9XYvbfWTeyyHYBmumk,17147
|
3
|
-
gnserver-0.0.0.0.4.dist-info/licenses/LICENSE,sha256=WH_t7dKZyWJ5Ld07eYIkUG4Tv6zZWXtAdsUqYAUesn0,1084
|
4
|
-
gnserver-0.0.0.0.4.dist-info/METADATA,sha256=FcQ0rw34w6HTyYmpqviPO3-a2cqes48-QSZienzEgok,804
|
5
|
-
gnserver-0.0.0.0.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
6
|
-
gnserver-0.0.0.0.4.dist-info/top_level.txt,sha256=-UOUBuD4u7Qkb1o5PdcwyA3kx8xCH2lwy0tJHi26Wb4,9
|
7
|
-
gnserver-0.0.0.0.4.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|