diff --git a/configs/config.yaml b/configs/config.yaml index 53faa8b..4666b6f 100644 --- a/configs/config.yaml +++ b/configs/config.yaml @@ -1 +1,6 @@ -FLOWKIT_PYTHON_API_KEY: "flowkit-python-api-key" \ No newline at end of file +FLOWKIT_PYTHON_API_KEY: flowkit-python-api-key +FLOWKIT_PYTHON_ENDPOINT: http://0.0.0.0:50052 +FLOWKIT_PYTHON_WORKERS: 2 +USE_SSL: False +#SSL_CERT_PUBLIC_KEY_FILE: +#SSL_CERT_PRIVATE_KEY_FILE: \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index 808fbd7..d2ff6da 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -16,8 +16,5 @@ RUN pip install --no-cache-dir .[all] EXPOSE 50052 -# Set default number of workers -ENV WORKERS=4 - # Use the environment variable in CMD -CMD ["sh", "-c", "allie-flowkit-python --host 0.0.0.0 --port 50052 --workers $WORKERS"] \ No newline at end of file +CMD ["sh", "-c", "allie-flowkit-python"] \ No newline at end of file diff --git a/src/allie/flowkit/__main__.py b/src/allie/flowkit/__main__.py index 7819ff8..64d5f45 100644 --- a/src/allie/flowkit/__main__.py +++ b/src/allie/flowkit/__main__.py @@ -23,20 +23,54 @@ """Main module for the FlowKit service.""" try: + from allie.flowkit.config._config import CONFIG import uvicorn except ImportError: raise ImportError("Please install uvicorn to run the service: pip install allie-flowkit-python[all]") import argparse +from urllib.parse import urlparse + + +def parse_cli_args(): + """Parse the command line arguments.""" + parser = argparse.ArgumentParser() + parser.add_argument("--host", type=str, required=False, help="The host to run the service on. By default 0.0.0.0") + parser.add_argument("--port", type=int, required=False, help="The port to run the service on. By default 50052") + parser.add_argument("--workers", type=int, required=False, help="The number of workers to use. By default 4") + parser.add_argument("--use-ssl", required=False, help="Enable SSL for the service. By default False") + parser.add_argument("--ssl-keyfile", type=str, required=False, help="The SSL key file path") + parser.add_argument("--ssl-certfile", type=str, required=False, help="The SSL certificate file path") + return parser.parse_args() + + +def substitute_empty_values(args): + """Substitute the empty values with configuration values.""" + host = args.host or urlparse(CONFIG.flowkit_python_endpoint).hostname + port = args.port or urlparse(CONFIG.flowkit_python_endpoint).port + workers = args.workers or CONFIG.flowkit_python_workers + use_ssl = (args.use_ssl.lower() == "true") if args.use_ssl is not None else CONFIG.use_ssl + ssl_keyfile = args.ssl_keyfile or CONFIG.ssl_cert_private_key_file + ssl_certfile = args.ssl_certfile or CONFIG.ssl_cert_public_key_file + return host, port, workers, use_ssl, ssl_keyfile, ssl_certfile def main(): """Run entrypoint for the FlowKit service.""" - parse = argparse.ArgumentParser() - parse.add_argument("--host", type=str, default="0.0.0.0", help="The host to run the service on. By default 0.0.0.0") - parse.add_argument("--port", type=int, default=50052, help="The port to run the service on. By default 50052") - parse.add_argument("--workers", type=int, default=4, help="The number of workers to use. By default 4") - args = parse.parse_args() - uvicorn.run("allie.flowkit:flowkit_service", host=args.host, port=args.port, workers=args.workers) + # Parse the command line arguments + args = parse_cli_args() + + # Substitute the empty values with configuration values + host, port, workers, use_ssl, ssl_keyfile, ssl_certfile = substitute_empty_values(args) + + # Run the service + uvicorn.run( + "allie.flowkit.flowkit_service:flowkit_service", + host=host, + port=port, + workers=workers, + ssl_keyfile=ssl_keyfile if use_ssl else None, + ssl_certfile=ssl_certfile if use_ssl else None, + ) if __name__ == "__main__": diff --git a/src/allie/flowkit/config/_config.py b/src/allie/flowkit/config/_config.py index de35ec7..81e9637 100644 --- a/src/allie/flowkit/config/_config.py +++ b/src/allie/flowkit/config/_config.py @@ -59,8 +59,16 @@ def __init__(self): """ config_path = os.getenv("ALLIE_CONFIG_PATH", "config.yaml") self._yaml = self._load_config(config_path) + + # Define the configuration variables to be parsed from the YAML file self.flowkit_python_api_key = self._yaml.get("FLOWKIT_PYTHON_API_KEY") + self.flowkit_python_endpoint = self._yaml.get("FLOWKIT_PYTHON_ENDPOINT", "http://localhost:50052") + self.flowkit_python_workers = self._yaml.get("FLOWKIT_PYTHON_WORKERS", 4) + self.use_ssl = self._yaml.get("USE_SSL", False) + self.ssl_cert_public_key_file = self._yaml.get("SSL_CERT_PUBLIC_KEY_FILE") + self.ssl_cert_private_key_file = self._yaml.get("SSL_CERT_PRIVATE_KEY_FILE") + # Check the mandatory configuration variables if not self.flowkit_python_api_key: raise ValueError("FLOWKIT_PYTHON_API_KEY is missing in the configuration file.")