diff --git a/docs_src/src/pages/documentation/api_reference/robyn_env.mdx b/docs_src/src/pages/documentation/api_reference/robyn_env.mdx index c0eed44cd..681440330 100644 --- a/docs_src/src/pages/documentation/api_reference/robyn_env.mdx +++ b/docs_src/src/pages/documentation/api_reference/robyn_env.mdx @@ -16,6 +16,9 @@ Batman wanted to configure the server through an environment file. Changing code - `ROBYN_BROWSER_OPEN`: Open the browser on successful start. - Default: `False` - Example: `ROBYN_BROWSER_OPEN=True` + - `ROBYN_DEV_MODE`: Configures the dev mode + - Default: `False` + - Example: `ROBYN_DEV_MODE=True` - `ROBYN_MAX_PAYLOAD_SIZE`: Sets the maximum payload size for requests in bytes. - Default: `1000000` bytes - Example: `ROBYN_MAX_PAYLOAD_SIZE=1000000` @@ -40,6 +43,7 @@ ROBYN_PORT=8080 ROBYN_HOST=127.0.0.1 RANDOM_ENV=123 ROBYN_BROWSER_OPEN=True +ROBYN_DEV_MODE=True ROBYN_MAX_PAYLOAD_SIZE=1000000 ``` diff --git a/robyn/__init__.py b/robyn/__init__.py index 656f1a91f..e051e2116 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -48,7 +48,12 @@ def __init__( self.config = config self.dependencies = dependencies - load_vars(project_root=directory_path) + if not bool(os.environ.get("ROBYN_CLI", False)): + # the env variables are already set when are running through the cli + load_vars(project_root=directory_path) + + self._handle_dev_mode() + logging.basicConfig(level=self.config.log_level) if self.config.log_level.lower() != "warn": @@ -56,8 +61,6 @@ def __init__( "SERVER IS RUNNING IN VERBOSE/DEBUG MODE. Set --log-level to WARN to run in production mode.", color=Colors.BLUE, ) - if self.config.dev: - exit("Dev mode is not supported in the python wrapper. Please use the CLI. e.g. python3 -m robyn app.py --dev ") self.router = Router() self.middleware_router = MiddlewareRouter() @@ -69,6 +72,18 @@ def __init__( self.exception_handler: Optional[Callable] = None self.authentication_handler: Optional[AuthenticationHandler] = None + def _handle_dev_mode(self): + cli_dev_mode = self.config.dev # --dev + env_dev_mode = os.getenv("ROBYN_DEV_MODE", "False").lower() == "true" # ROBYN_DEV_MODE=True + is_robyn = os.getenv("ROBYN_CLI", False) + + if cli_dev_mode and not is_robyn: + raise SystemExit("Dev mode is not supported in the python wrapper. Please use the Robyn CLI. e.g. python3 -m robyn app.py --dev") + + if env_dev_mode and not is_robyn: + logger.error("Ignoring ROBYN_DEV_MODE environment variable. Dev mode is not supported in the python wrapper.") + raise SystemExit("Dev mode is not supported in the python wrapper. Please use the Robyn CLI. e.g. python3 -m robyn app.py") + def add_route( self, route_type: Union[HttpMethod, str], diff --git a/robyn/argument_parser.py b/robyn/argument_parser.py index a1b532f9a..23b4ac92b 100644 --- a/robyn/argument_parser.py +++ b/robyn/argument_parser.py @@ -23,7 +23,7 @@ def __init__(self) -> None: "--dev", dest="dev", action="store_true", - default=False, + default=None, help="Development mode. It restarts the server based on file changes.", ) parser.add_argument( @@ -81,6 +81,7 @@ def __init__(self) -> None: self.version = args.version self.compile_rust_path = args.compile_rust_path self.create_rust_file = args.create_rust_file + self.file_path = None # find something that ends with .py in unknown_args for arg in unknown_args: diff --git a/robyn/cli.py b/robyn/cli.py index 385348890..86f925d18 100644 --- a/robyn/cli.py +++ b/robyn/cli.py @@ -6,6 +6,7 @@ from InquirerPy.base.control import Choice from .argument_parser import Config from .reloader import create_rust_file, setup_reloader +from robyn.env_populator import load_vars from robyn.robyn import get_version from pathlib import Path import shutil @@ -104,6 +105,16 @@ def start_app_normally(config: Config): def run(): config = Config() + + if not config.file_path: + config.file_path = f"{os.getcwd()}/{__name__}" + + load_vars(project_root=os.path.dirname(os.path.abspath(config.file_path))) + os.environ["ROBYN_CLI"] = "True" + + if config.dev is None: + config.dev = os.getenv("ROBYN_DEV_MODE", False) == "True" + if config.create: create_robyn_app() diff --git a/robyn/reloader.py b/robyn/reloader.py index 9a93df40f..82deeea16 100644 --- a/robyn/reloader.py +++ b/robyn/reloader.py @@ -106,11 +106,12 @@ def stop_server(self): def reload(self): self.stop_server() + print("Reloading the server") new_env = os.environ.copy() new_env["IS_RELOADER_RUNNING"] = "True" # This is used to check if a reloader is already running + # IS_RELOADER_RUNNING is specifically used for IPC between the reloader and the server - print(f"Reloading {self.file_path}...") arguments = [arg for arg in sys.argv[1:] if not arg.startswith("--dev")] clean_rust_binaries(self.built_rust_binaries)