GNServer 0.0.0.0.4__tar.gz → 0.0.0.0.6__tar.gz

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.
@@ -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, *, name: str | None = None):
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
- name or fn.__name__,
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, *, name: str | None = None):
214
- return self.route("GET", path, name=name)
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, *, name: str | None = None):
217
- return self.route("POST", path, name=name)
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, *, name: str | None = None):
220
- return self.route("PUT", path, name=name)
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, *, name: str | None = None):
223
- return self.route("DELETE", path, name=name)
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"/{path}/{{_path:path}}")
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()} {response.payload if len(response.payload) < 200 else ''}')
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 = (
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: GNServer
3
- Version: 0.0.0.0.4
3
+ Version: 0.0.0.0.6
4
4
  Summary: GNServer
5
5
  Home-page: https://github.com/KeyisB/libs/tree/main/GNServer
6
6
  Author: KeyisB
@@ -5,6 +5,7 @@ GNServer/LICENSE
5
5
  GNServer/mmbConfig.json
6
6
  GNServer/GNServer/__init__.py
7
7
  GNServer/GNServer/_app.py
8
+ GNServer/GNServer/models.py
8
9
  GNServer/GNServer.egg-info/PKG-INFO
9
10
  GNServer/GNServer.egg-info/SOURCES.txt
10
11
  GNServer/GNServer.egg-info/dependency_links.txt
@@ -1,15 +1,15 @@
1
1
  Copyright (C) 2024 KeyisB. All rights reserved.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person
4
- obtaining a copy of this software and associated documentation
4
+ obtaining a copy of this software and associated
5
+ 1. Copying, modification, merging, publishing, distribution,
6
+ sublicensing, and/or selling copies of the Software are
7
+ strictly prohibited.documentation
5
8
  files (the "Software"), to use the Software exclusively for
6
9
  projects related to the GN or GW systems, including personal,
7
10
  educational, and commercial purposes, subject to the following
8
11
  conditions:
9
12
 
10
- 1. Copying, modification, merging, publishing, distribution,
11
- sublicensing, and/or selling copies of the Software are
12
- strictly prohibited.
13
13
  2. The licensee may use the Software only in its original,
14
14
  unmodified form.
15
15
  3. All copies or substantial portions of the Software must
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: GNServer
3
- Version: 0.0.0.0.4
3
+ Version: 0.0.0.0.6
4
4
  Summary: GNServer
5
5
  Home-page: https://github.com/KeyisB/libs/tree/main/GNServer
6
6
  Author: KeyisB
@@ -5,7 +5,7 @@ filesName = 'GNServer'
5
5
 
6
6
  setup(
7
7
  name=name,
8
- version='0.0.0.0.4',
8
+ version='0.0.0.0.6',
9
9
  author="KeyisB",
10
10
  author_email="keyisb.pip@gmail.com",
11
11
  description=name,
File without changes
File without changes
File without changes