Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature add support for env variables #286

Merged
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ dist/

# python venv
venv
robyn.code-workspace
hello_robyn.py
robyn.env
3 changes: 2 additions & 1 deletion robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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):
Expand Down
48 changes: 48 additions & 0 deletions robyn/env_populator.py
Original file line number Diff line number Diff line change
@@ -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]}")