Newer
Older
python_redirect / main.py
"""
Entry point for the redirection service.
Parses command-line arguments to execute either the CGI or FastAPI implementation.
"""

import argparse


def run():
    """
    Parses arguments and initializes the selected execution mode.
    """
    parser = argparse.ArgumentParser(description="Run the proxy redirect service.")
    parser.add_argument(
        "--mode",
        choices=["cgi", "fastapi"],
        required=True,
        help="Execution mode: 'cgi' for standard output, 'fastapi' to launch the server.",
    )
    parser.add_argument(
        "--addr",
        required=False,
        help="The IP address to bind the FastAPI server.",
    )
    parser.add_argument(
        "--port",
        type=int,
        required=False,
        help="The port number to bind the FastAPI server.",
    )

    args = parser.parse_args()

    if args.mode == "fastapi":
        if args.addr is None or args.port is None:
            parser.error("--addr and --port are required when --mode is fastapi")

        import uvicorn

        uvicorn.run("router:app", host=args.addr, port=args.port, reload=False)

    elif args.mode == "cgi":
        from cgi_redirect import CGIProxyRedirect

        app = CGIProxyRedirect()
        app.emit()


if __name__ == "__main__":
    run()