h
This commit is contained in:
parent
ff34b26072
commit
2de3558925
1 changed files with 21 additions and 10 deletions
|
@ -10,6 +10,7 @@ from asyncio.exceptions import CancelledError
|
||||||
from OpenSSL import crypto
|
from OpenSSL import crypto
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from . import logger as logging
|
||||||
from .error import GeminiError
|
from .error import GeminiError
|
||||||
from .message import Request, Response
|
from .message import Request, Response
|
||||||
from .router import Router, route
|
from .router import Router, route
|
||||||
|
@ -17,6 +18,7 @@ from .transport import AsyncTransport
|
||||||
|
|
||||||
if typing.TYPE_CHECKING:
|
if typing.TYPE_CHECKING:
|
||||||
from asyncio import StreamReader, StreamWriter
|
from asyncio import StreamReader, StreamWriter
|
||||||
|
from collections.abc import Callable
|
||||||
from .router import RouteHandler
|
from .router import RouteHandler
|
||||||
|
|
||||||
|
|
||||||
|
@ -94,7 +96,7 @@ class AsyncServer(dict):
|
||||||
SERVERS[server.name] = server
|
SERVERS[server.name] = server
|
||||||
|
|
||||||
|
|
||||||
def add_route(path: str, handler: RouteHandler) -> None:
|
def add_route(self, path: str, handler: RouteHandler) -> None:
|
||||||
self.router.add_route(path, handler)
|
self.router.add_route(path, handler)
|
||||||
|
|
||||||
|
|
||||||
|
@ -139,7 +141,7 @@ class AsyncServer(dict):
|
||||||
if self._server is not None:
|
if self._server is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
print(f"Starting server @ gemini://{self.host}:{self.port}")
|
logging.info(f"Starting server @ gemini://{self.host}:{self.port}")
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
|
@ -148,7 +150,7 @@ class AsyncServer(dict):
|
||||||
loop.add_signal_handler(sig, self.stop)
|
loop.add_signal_handler(sig, self.stop)
|
||||||
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print(f"Cannot handle signal: {sig}")
|
logging.warning(f"Cannot handle signal: {sig}")
|
||||||
|
|
||||||
self._server = await asyncio.start_server(
|
self._server = await asyncio.start_server(
|
||||||
client_connected_cb = self.handle_client,
|
client_connected_cb = self.handle_client,
|
||||||
|
@ -173,11 +175,11 @@ class AsyncServer(dict):
|
||||||
loop.add_signal_handler(sig, signal.SIG_DFL) # type: ignore
|
loop.add_signal_handler(sig, signal.SIG_DFL) # type: ignore
|
||||||
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print(f"Cannot handle signal: {sig}")
|
logging.warning(f"Cannot handle signal: {sig}")
|
||||||
|
|
||||||
await self._server.wait_closed()
|
await self._server.wait_closed()
|
||||||
|
|
||||||
print("Shutting down...")
|
logging.info("Shutting down...")
|
||||||
|
|
||||||
self._server = None
|
self._server = None
|
||||||
|
|
||||||
|
@ -202,8 +204,11 @@ class AsyncServer(dict):
|
||||||
raise GeminiError(50, "Invalid response")
|
raise GeminiError(50, "Invalid response")
|
||||||
|
|
||||||
except GeminiError as error:
|
except GeminiError as error:
|
||||||
traceback.print_exc()
|
response = Response(
|
||||||
response = Response(error.status, error.message.encode("utf-8"), "text/plain")
|
error.status,
|
||||||
|
(f"{error.status.reason}: {error.message}").encode("utf-8"),
|
||||||
|
"text/plain"
|
||||||
|
)
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
response = Response(50, b"Server Error :/", "text/plain")
|
response = Response(50, b"Server Error :/", "text/plain")
|
||||||
|
@ -211,7 +216,13 @@ class AsyncServer(dict):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await transport.write(response.build())
|
await transport.write(response.build())
|
||||||
print(f"{transport.remote_address} {request.path} {response.status} {len(response.body)}")
|
logging.info(
|
||||||
|
"ip: %s, path: %s, status: %i, length: %i",
|
||||||
|
transport.remote_address,
|
||||||
|
request.path,
|
||||||
|
response.status,
|
||||||
|
len(response.body)
|
||||||
|
)
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
@ -225,8 +236,8 @@ class AsyncServer(dict):
|
||||||
name: str,
|
name: str,
|
||||||
context: ssl.SSLContext) -> int | None:
|
context: ssl.SSLContext) -> int | None:
|
||||||
|
|
||||||
print(ssl_object)
|
logging.debug(ssl_object)
|
||||||
print(name)
|
logging.debug(name)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue