diff --git a/.gitignore b/.gitignore index 19bbcd1c6..b793f0f06 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ dist/ # python venv venv +robyn.code-workspace +hello_robyn.py +robyn.env diff --git a/robyn/__init__.py b/robyn/__init__.py index b10afe79d..ffec9281c 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -17,6 +17,7 @@ from robyn.robyn import SocketHeld from robyn.router import MiddlewareRouter, Router, WebSocketRouter from robyn.ws import WS +from robyn.env_populator import load_vars logger = logging.getLogger(__name__) @@ -39,7 +40,7 @@ def __init__(self, file_object: str) -> None: self.headers = [] self.directories = [] self.event_handlers = {} - + load_vars() self._config_logger() def _add_route(self, route_type, endpoint, handler, const=False): diff --git a/robyn/env_populator.py b/robyn/env_populator.py new file mode 100644 index 000000000..e4d0f6ec8 --- /dev/null +++ b/robyn/env_populator.py @@ -0,0 +1,48 @@ +import os +import logging +from pathlib import Path + + + +# Path to the root of the project +ROOT_DIR = Path(__file__).parent.parent + +# Path to the environment variables +CONFIG_PATH = ROOT_DIR / 'robyn.env' + +#set the logger that will log the environment variables imported from robyn.env and the ones already set +logger = logging.getLogger(__name__) +logging.basicConfig(level=logging.INFO) + +# parse the configuration file returning a list of tuples (key, value) containing the environment variables +def parser(config_path=CONFIG_PATH): + """Parse the configuration file""" + if config_path.exists(): + with open(config_path, 'r') as f: + for line in f: + if line.startswith('#'): + continue + yield line.strip().split('=') + + +# check for the environment variables set in cli and if not set them +def load_vars(variables = None): + """Main function""" + + variables = parser() + + if variables is None: + return + + for var in variables: + if var[0] in os.environ: + logger.info(f" Variable {var[0]} already set") + continue + else: + os.environ[var[0]]=var[1] + logger.info(f" Variable {var[0]} set to {var[1]}") + + + + +