GNServer 0.0.0.0.19__py3-none-any.whl → 0.0.0.0.20__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 CHANGED
@@ -46,7 +46,23 @@ console.setFormatter(logging.Formatter("[GNServer] %(name)s: %(levelname)s: %(me
46
46
 
47
47
 
48
48
 
49
+ class _BaseEXception(Exception):
50
+ def __init__(self, code: str, message=""):
51
+ self._code = code
52
+ self._message = message
49
53
 
54
+ def assembly(self):
55
+ return gn.GNResponse(f'gn:error:{self._code}', payload={'msg': self._message})
56
+
57
+
58
+ class GNExceptions:
59
+ class UnprocessableEntity(_BaseEXception):
60
+ def __init__(self):
61
+ super().__init__('422', "Unprocessable Entity")
62
+
63
+ class BadRequest(_BaseEXception):
64
+ def __init__(self):
65
+ super().__init__('400', "Bad Request")
50
66
 
51
67
 
52
68
 
@@ -169,7 +185,7 @@ def resolve_cors(origin_url: str, rules: list[str]) -> bool:
169
185
  - "*.example.com" -> wildcard (одна метка)
170
186
  - "**.example.com" -> globstar (0+ меток)
171
187
  - "pages.*.core.gn" -> смешанное
172
- - "https://*.site.tld" -> с проверкой схемы
188
+ - "gn://*.site.tld" -> с проверкой схемы
173
189
  - "!<regex>" -> полное соответствие по regex к origin_url
174
190
  """
175
191
 
@@ -391,6 +407,7 @@ class App:
391
407
  def __init__(self):
392
408
  self._routes: List[Route] = []
393
409
  self._cors: Optional[gn.CORSObject] = None
410
+ self._events: Dict[str, Dict[str, Union[str, Callable]]] = {}
394
411
 
395
412
  self.domain: str = None
396
413
 
@@ -430,8 +447,42 @@ class App:
430
447
  self._cors = cors
431
448
 
432
449
 
450
+ def addEventListener(self, name: str):
451
+ def decorator(fn: Callable[Optional[dict], Any]):
452
+ events = self._events.get(name, [])
453
+ events.append({
454
+ 'func': fn,
455
+ 'async': inspect.iscoroutinefunction(fn),
456
+ 'parameters': inspect.signature(fn).parameters
457
+ })
458
+
459
+ return fn
460
+ return decorator
461
+
462
+ async def dispatchEvent(self, name: str, payload: Optional[str] = None) -> None:
463
+ data_list = self._events.get(name, None)
464
+ if data_list:
465
+ for data in data_list:
466
+ func: Callable = data['func']
467
+ is_async = data['async']
468
+
469
+ if not is_async:
470
+ if payload in data['parameters']:
471
+ func(payload=payload)
472
+ else:
473
+ func()
474
+ else:
475
+ if payload in data['parameters']:
476
+ await func(payload=payload)
477
+ else:
478
+ await func()
479
+
480
+
481
+
482
+
433
483
 
434
- async def dispatch(
484
+
485
+ async def dispatchRequest(
435
486
  self, request: gn.GNRequest
436
487
  ) -> Union[gn.GNResponse, AsyncGenerator[gn.GNResponse, None]]:
437
488
  path = request.url.path
@@ -667,12 +718,17 @@ class App:
667
718
  logger.debug(f'Отправлен на сервер ответ -> {response.command} {response.payload if response.payload and len((response.payload)) < 200 else ''}')
668
719
  self.transmit()
669
720
  except Exception as e:
670
- logger.error('GNServer: error\n' + traceback.format_exc())
721
+ if e.__qualname__.startswith("GNExceptions."):
722
+ e: GNExceptions.UnprocessableEntity = e
723
+ r = e.assembly()
724
+ return r
725
+ else:
726
+ logger.error('GNServer: error\n' + traceback.format_exc())
671
727
 
672
- response = gn.GNResponse('gn:backend:500')
673
- self._quic.send_stream_data(request.stream_id, response.serialize(mode), end_stream=True)
674
- self.transmit()
675
-
728
+ response = gn.GNResponse('gn:backend:500')
729
+ self._quic.send_stream_data(request.stream_id, response.serialize(mode), end_stream=True)
730
+ self.transmit()
731
+
676
732
  async def resolve_response(self, response: gn.GNResponse) -> gn.GNResponse:
677
733
  await response.assembly()
678
734
 
@@ -707,6 +763,9 @@ class App:
707
763
  cfg.load_cert_chain(cert_path, key_path)
708
764
 
709
765
  async def _main():
766
+
767
+ await self.dispatchEvent('before-run')
768
+
710
769
  await serve(
711
770
  host,
712
771
  port,
@@ -718,6 +777,9 @@ class App:
718
777
  if run is not None:
719
778
  await run()
720
779
 
780
+ await self.dispatchEvent('run')
781
+
782
+
721
783
 
722
784
  if wait:
723
785
  await asyncio.Event().wait()
GNServer/_client.py CHANGED
@@ -110,7 +110,6 @@ class AsyncClient:
110
110
  self.__dns_core__ipv4 = '51.250.85.38:52943'
111
111
  self.__dns_gn__ipv4 = None
112
112
 
113
- self.__user = {}
114
113
  self.__current_session = {}
115
114
  self.__request_callbacks = {}
116
115
  self.__response_callbacks = {}
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: GNServer
3
- Version: 0.0.0.0.19
3
+ Version: 0.0.0.0.20
4
4
  Summary: GNServer
5
5
  Home-page: https://github.com/KeyisB/libs/tree/main/GNServer
6
6
  Author: KeyisB
@@ -0,0 +1,9 @@
1
+ GNServer/___client.py,sha256=hmeUL2Vqp-BnwJeRcLZAaIfRNVxBrRRB_AFk9ofkei4,25459
2
+ GNServer/__init__.py,sha256=J4bjDqXVSEgOhD-FmkhpCeU4NJkp2BKMUKgltiuSXVo,1437
3
+ GNServer/_app.py,sha256=o03to3_3PUFzKCJvVCoIatfB3G6Uazkdp5oQQp0sHKU,27568
4
+ GNServer/_client.py,sha256=lwFGsNR2RullSNCuu7WXXdYfUbsV1_ZweMv-N-5Gpv0,27371
5
+ gnserver-0.0.0.0.20.dist-info/licenses/LICENSE,sha256=WH_t7dKZyWJ5Ld07eYIkUG4Tv6zZWXtAdsUqYAUesn0,1084
6
+ gnserver-0.0.0.0.20.dist-info/METADATA,sha256=ccOeCBifIIxH-pK_Q3YN-7zv94dZ6wLbtHGOg55HG3g,805
7
+ gnserver-0.0.0.0.20.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ gnserver-0.0.0.0.20.dist-info/top_level.txt,sha256=-UOUBuD4u7Qkb1o5PdcwyA3kx8xCH2lwy0tJHi26Wb4,9
9
+ gnserver-0.0.0.0.20.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- GNServer/___client.py,sha256=hmeUL2Vqp-BnwJeRcLZAaIfRNVxBrRRB_AFk9ofkei4,25459
2
- GNServer/__init__.py,sha256=J4bjDqXVSEgOhD-FmkhpCeU4NJkp2BKMUKgltiuSXVo,1437
3
- GNServer/_app.py,sha256=R8E099VO6qAMXqfLShw1HcchNfEJa2R8ZtYJkS4cJMs,25525
4
- GNServer/_client.py,sha256=uM88y3yHBBH6qjG5L6bqFMCOVveTeyjyV-vlw1JOmG8,27397
5
- gnserver-0.0.0.0.19.dist-info/licenses/LICENSE,sha256=WH_t7dKZyWJ5Ld07eYIkUG4Tv6zZWXtAdsUqYAUesn0,1084
6
- gnserver-0.0.0.0.19.dist-info/METADATA,sha256=95tWArZAgX6HibYLhdiQUQzf9MLDvLOCZuReAH3QkGw,805
7
- gnserver-0.0.0.0.19.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
- gnserver-0.0.0.0.19.dist-info/top_level.txt,sha256=-UOUBuD4u7Qkb1o5PdcwyA3kx8xCH2lwy0tJHi26Wb4,9
9
- gnserver-0.0.0.0.19.dist-info/RECORD,,