- 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.
73 lines
1.7 KiB
Python
73 lines
1.7 KiB
Python
import sys
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
from . import PyServeServer, Config, __version__
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="PyServe - HTTP web server",
|
|
prog="pyserve"
|
|
)
|
|
parser.add_argument(
|
|
"-c", "--config",
|
|
default="config.yaml",
|
|
help="Path to configuration file (default: config.yaml)"
|
|
)
|
|
parser.add_argument(
|
|
"--host",
|
|
help="Host to bind the server to"
|
|
)
|
|
parser.add_argument(
|
|
"--port",
|
|
type=int,
|
|
help="Port to bind the server to"
|
|
)
|
|
parser.add_argument(
|
|
"--debug",
|
|
action="store_true",
|
|
help="Enable debug mode"
|
|
)
|
|
parser.add_argument(
|
|
"--version",
|
|
action="version",
|
|
version=f"%(prog)s {__version__}"
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
config_path = args.config
|
|
if not Path(config_path).exists():
|
|
print(f"Configuration file {config_path} not found")
|
|
print("Using default configuration")
|
|
config = Config()
|
|
else:
|
|
try:
|
|
config = Config.from_yaml(config_path)
|
|
except Exception as e:
|
|
print(f"Configuration loading error: {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:
|
|
print(f"Starting PyServe server on {config.server.host}:{config.server.port}")
|
|
server.run()
|
|
except KeyboardInterrupt:
|
|
print("\nServer stopped by user")
|
|
except Exception as e:
|
|
print(f"Server startup error: {e}")
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|