feat: Add CLI for PyServe with configuration options

- Introduced a new CLI module (`cli.py`) to manage server configurations via command line arguments.
- Added script entry point in `pyproject.toml` for easy access to the CLI.
- Enhanced `Config` class to load configurations from a YAML file.
- Updated `__init__.py` to include `__version__` in the module exports.
- Added optional dependencies for development tools in `pyproject.toml`.
- Implemented logging improvements and error handling in various modules.
- Created tests for the CLI functionality to ensure proper behavior.
- Removed the old `run.py` implementation in favor of the new CLI approach.
This commit is contained in:
Илья Глазунов
2025-09-02 00:20:40 +03:00
parent 83cb7d68b0
commit 84cd1c974f
17 changed files with 1016 additions and 232 deletions
+6 -6
View File
@@ -24,12 +24,12 @@ class Router:
if pattern.startswith("="):
exact_path = pattern[1:]
self.exact_routes[exact_path] = config
logger.debug(f"Добавлен exact маршрут: {exact_path}")
logger.debug(f"Added exact route: {exact_path}")
return
if pattern == "__default__":
self.default_route = config
logger.debug("Добавлен default маршрут")
logger.debug("Added default route")
return
if pattern.startswith("~"):
@@ -40,9 +40,9 @@ class Router:
try:
compiled_pattern = re.compile(regex_pattern, flags)
self.routes[compiled_pattern] = config
logger.debug(f"Добавлен regex маршрут: {pattern}")
logger.debug(f"Added regex route: {pattern}")
except re.error as e:
logger.error(f"Ошибка компиляции regex {pattern}: {e}")
logger.error(f"Regex compilation error {pattern}: {e}")
def match(self, path: str) -> Optional[RouteMatch]:
if path in self.exact_routes:
@@ -77,7 +77,7 @@ class RequestHandler:
try:
return await self._process_route(request, route_match)
except Exception as e:
logger.error(f"Ошибка обработки запроса {path}: {e}")
logger.error(f"Request processing error {path}: {e}")
return PlainTextResponse("500 Internal Server Error", status_code=500)
async def _process_route(self, request: Request, route_match: RouteMatch) -> Response:
@@ -169,7 +169,7 @@ class RequestHandler:
for key, value in params.items():
proxy_url = proxy_url.replace(f"{{{key}}}", value)
logger.info(f"Проксирование запроса на: {proxy_url}")
logger.info(f"Proxying request to: {proxy_url}")
return PlainTextResponse(f"Proxy to: {proxy_url}", status_code=200)