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
+1 -60
View File
@@ -1,63 +1,4 @@
import sys
import argparse
from pathlib import Path
from pyserve import PyServeServer, Config
def main():
parser = argparse.ArgumentParser(description="PyServe - HTTP веб-сервер")
parser.add_argument(
"-c", "--config",
default="config.yaml",
help="Путь к конфигурационному файлу (по умолчанию: config.yaml)"
)
parser.add_argument(
"--host",
help="Хост для привязки сервера"
)
parser.add_argument(
"--port",
type=int,
help="Порт для привязки сервера"
)
parser.add_argument(
"--debug",
action="store_true",
help="Включить отладочный режим"
)
args = parser.parse_args()
config_path = args.config
if not Path(config_path).exists():
print(f"Конфигурационный файл {config_path} не найден")
print("Используется конфигурация по умолчанию")
config = Config()
else:
try:
config = Config.from_yaml(config_path)
except Exception as e:
print(f"Ошибка загрузки конфигурации: {e}")
sys.exit(1)
if args.host:
config.server.host = args.host
if args.port:
config.server.port = args.port
if args.debug:
config.logging.level = "DEBUG"
server = PyServeServer(config)
try:
server.run()
except KeyboardInterrupt:
print("\nСервер остановлен пользователем")
except Exception as e:
print(f"Ошибка запуска сервера: {e}")
sys.exit(1)
from pyserve.cli import main
if __name__ == "__main__":
main()