pyserveX/pyserve/cli.py
2025-12-03 12:54:45 +03:00

54 lines
1.6 KiB
Python

import argparse
import sys
from pathlib import Path
from . import Config, PyServeServer, __version__
def main() -> None:
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()