Skip to content

Commit

Permalink
feat: control dev mode with 'ROBYN_DEV_MODE' env var (#877)
Browse files Browse the repository at this point in the history
* [#62] feat: control dev mode with env var


---------

Co-authored-by: Sanskar Jethi <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 19, 2024
1 parent 9a1e998 commit aac58bb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs_src/src/pages/documentation/api_reference/robyn_env.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
```

Expand Down
21 changes: 18 additions & 3 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ 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":
logger.info(
"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()
Expand All @@ -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],
Expand Down
3 changes: 2 additions & 1 deletion robyn/argument_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions robyn/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion robyn/reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit aac58bb

Please sign in to comment.