config.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. """The Reflex config."""
  2. from __future__ import annotations
  3. import dataclasses
  4. import enum
  5. import importlib
  6. import inspect
  7. import os
  8. import sys
  9. import urllib.parse
  10. from pathlib import Path
  11. from typing import Any, Dict, List, Optional, Set
  12. from typing_extensions import get_type_hints
  13. from reflex.utils.exceptions import ConfigError, EnvironmentVarValueError
  14. from reflex.utils.types import GenericType, is_union, value_inside_optional
  15. try:
  16. import pydantic.v1 as pydantic
  17. except ModuleNotFoundError:
  18. import pydantic
  19. from reflex_cli.constants.hosting import Hosting
  20. from reflex import constants
  21. from reflex.base import Base
  22. from reflex.utils import console
  23. class DBConfig(Base):
  24. """Database config."""
  25. engine: str
  26. username: Optional[str] = ""
  27. password: Optional[str] = ""
  28. host: Optional[str] = ""
  29. port: Optional[int] = None
  30. database: str
  31. @classmethod
  32. def postgresql(
  33. cls,
  34. database: str,
  35. username: str,
  36. password: str | None = None,
  37. host: str | None = None,
  38. port: int | None = 5432,
  39. ) -> DBConfig:
  40. """Create an instance with postgresql engine.
  41. Args:
  42. database: Database name.
  43. username: Database username.
  44. password: Database password.
  45. host: Database host.
  46. port: Database port.
  47. Returns:
  48. DBConfig instance.
  49. """
  50. return cls(
  51. engine="postgresql",
  52. username=username,
  53. password=password,
  54. host=host,
  55. port=port,
  56. database=database,
  57. )
  58. @classmethod
  59. def postgresql_psycopg2(
  60. cls,
  61. database: str,
  62. username: str,
  63. password: str | None = None,
  64. host: str | None = None,
  65. port: int | None = 5432,
  66. ) -> DBConfig:
  67. """Create an instance with postgresql+psycopg2 engine.
  68. Args:
  69. database: Database name.
  70. username: Database username.
  71. password: Database password.
  72. host: Database host.
  73. port: Database port.
  74. Returns:
  75. DBConfig instance.
  76. """
  77. return cls(
  78. engine="postgresql+psycopg2",
  79. username=username,
  80. password=password,
  81. host=host,
  82. port=port,
  83. database=database,
  84. )
  85. @classmethod
  86. def sqlite(
  87. cls,
  88. database: str,
  89. ) -> DBConfig:
  90. """Create an instance with sqlite engine.
  91. Args:
  92. database: Database name.
  93. Returns:
  94. DBConfig instance.
  95. """
  96. return cls(
  97. engine="sqlite",
  98. database=database,
  99. )
  100. def get_url(self) -> str:
  101. """Get database URL.
  102. Returns:
  103. The database URL.
  104. """
  105. host = (
  106. f"{self.host}:{self.port}" if self.host and self.port else self.host or ""
  107. )
  108. username = urllib.parse.quote_plus(self.username) if self.username else ""
  109. password = urllib.parse.quote_plus(self.password) if self.password else ""
  110. if username:
  111. path = f"{username}:{password}@{host}" if password else f"{username}@{host}"
  112. else:
  113. path = f"{host}"
  114. return f"{self.engine}://{path}/{self.database}"
  115. def get_default_value_for_field(field: dataclasses.Field) -> Any:
  116. """Get the default value for a field.
  117. Args:
  118. field: The field.
  119. Returns:
  120. The default value.
  121. Raises:
  122. ValueError: If no default value is found.
  123. """
  124. if field.default != dataclasses.MISSING:
  125. return field.default
  126. elif field.default_factory != dataclasses.MISSING:
  127. return field.default_factory()
  128. else:
  129. raise ValueError(
  130. f"Missing value for environment variable {field.name} and no default value found"
  131. )
  132. # TODO: Change all interpret_.* signatures to value: str, field: dataclasses.Field once we migrate rx.Config to dataclasses
  133. def interpret_boolean_env(value: str, field_name: str) -> bool:
  134. """Interpret a boolean environment variable value.
  135. Args:
  136. value: The environment variable value.
  137. field_name: The field name.
  138. Returns:
  139. The interpreted value.
  140. Raises:
  141. EnvironmentVarValueError: If the value is invalid.
  142. """
  143. true_values = ["true", "1", "yes", "y"]
  144. false_values = ["false", "0", "no", "n"]
  145. if value.lower() in true_values:
  146. return True
  147. elif value.lower() in false_values:
  148. return False
  149. raise EnvironmentVarValueError(f"Invalid boolean value: {value} for {field_name}")
  150. def interpret_int_env(value: str, field_name: str) -> int:
  151. """Interpret an integer environment variable value.
  152. Args:
  153. value: The environment variable value.
  154. field_name: The field name.
  155. Returns:
  156. The interpreted value.
  157. Raises:
  158. EnvironmentVarValueError: If the value is invalid.
  159. """
  160. try:
  161. return int(value)
  162. except ValueError as ve:
  163. raise EnvironmentVarValueError(
  164. f"Invalid integer value: {value} for {field_name}"
  165. ) from ve
  166. def interpret_path_env(value: str, field_name: str) -> Path:
  167. """Interpret a path environment variable value.
  168. Args:
  169. value: The environment variable value.
  170. field_name: The field name.
  171. Returns:
  172. The interpreted value.
  173. Raises:
  174. EnvironmentVarValueError: If the path does not exist.
  175. """
  176. path = Path(value)
  177. if not path.exists():
  178. raise EnvironmentVarValueError(f"Path does not exist: {path} for {field_name}")
  179. return path
  180. def interpret_enum_env(value: str, field_type: GenericType, field_name: str) -> Any:
  181. """Interpret an enum environment variable value.
  182. Args:
  183. value: The environment variable value.
  184. field_type: The field type.
  185. field_name: The field name.
  186. Returns:
  187. The interpreted value.
  188. Raises:
  189. EnvironmentVarValueError: If the value is invalid.
  190. """
  191. try:
  192. return field_type(value)
  193. except ValueError as ve:
  194. raise EnvironmentVarValueError(
  195. f"Invalid enum value: {value} for {field_name}"
  196. ) from ve
  197. def interpret_env_var_value(
  198. value: str, field_type: GenericType, field_name: str
  199. ) -> Any:
  200. """Interpret an environment variable value based on the field type.
  201. Args:
  202. value: The environment variable value.
  203. field_type: The field type.
  204. field_name: The field name.
  205. Returns:
  206. The interpreted value.
  207. Raises:
  208. ValueError: If the value is invalid.
  209. """
  210. field_type = value_inside_optional(field_type)
  211. if is_union(field_type):
  212. raise ValueError(
  213. f"Union types are not supported for environment variables: {field_name}."
  214. )
  215. if field_type is bool:
  216. return interpret_boolean_env(value, field_name)
  217. elif field_type is str:
  218. return value
  219. elif field_type is int:
  220. return interpret_int_env(value, field_name)
  221. elif field_type is Path:
  222. return interpret_path_env(value, field_name)
  223. elif inspect.isclass(field_type) and issubclass(field_type, enum.Enum):
  224. return interpret_enum_env(value, field_type, field_name)
  225. else:
  226. raise ValueError(
  227. f"Invalid type for environment variable {field_name}: {field_type}. This is probably an issue in Reflex."
  228. )
  229. @dataclasses.dataclass(init=False)
  230. class EnvironmentVariables:
  231. """Environment variables class to instantiate environment variables."""
  232. # Whether to use npm over bun to install frontend packages.
  233. REFLEX_USE_NPM: bool = False
  234. # The npm registry to use.
  235. NPM_CONFIG_REGISTRY: Optional[str] = None
  236. # Whether to use Granian for the backend. Otherwise, use Uvicorn.
  237. REFLEX_USE_GRANIAN: bool = False
  238. # The username to use for authentication on python package repository. Username and password must both be provided.
  239. TWINE_USERNAME: Optional[str] = None
  240. # The password to use for authentication on python package repository. Username and password must both be provided.
  241. TWINE_PASSWORD: Optional[str] = None
  242. # Whether to use the system installed bun. If set to false, bun will be bundled with the app.
  243. REFLEX_USE_SYSTEM_BUN: bool = False
  244. # Whether to use the system installed node and npm. If set to false, node and npm will be bundled with the app.
  245. REFLEX_USE_SYSTEM_NODE: bool = False
  246. # The working directory for the next.js commands.
  247. REFLEX_WEB_WORKDIR: Path = Path(constants.Dirs.WEB)
  248. # Path to the alembic config file
  249. ALEMBIC_CONFIG: Path = Path(constants.ALEMBIC_CONFIG)
  250. # Disable SSL verification for HTTPX requests.
  251. SSL_NO_VERIFY: bool = False
  252. # The directory to store uploaded files.
  253. REFLEX_UPLOADED_FILES_DIR: Path = Path(constants.Dirs.UPLOADED_FILES)
  254. # Whether to use seperate processes to compile the frontend and how many. If not set, defaults to thread executor.
  255. REFLEX_COMPILE_PROCESSES: Optional[int] = None
  256. # Whether to use seperate threads to compile the frontend and how many. Defaults to `min(32, os.cpu_count() + 4)`.
  257. REFLEX_COMPILE_THREADS: Optional[int] = None
  258. # The directory to store reflex dependencies.
  259. REFLEX_DIR: Path = Path(constants.Reflex.DIR)
  260. # Whether to print the SQL queries if the log level is INFO or lower.
  261. SQLALCHEMY_ECHO: bool = False
  262. # Whether to ignore the redis config error. Some redis servers only allow out-of-band configuration.
  263. REFLEX_IGNORE_REDIS_CONFIG_ERROR: bool = False
  264. # Whether to skip purging the web directory in dev mode.
  265. REFLEX_PERSIST_WEB_DIR: bool = False
  266. # The reflex.build frontend host.
  267. REFLEX_BUILD_FRONTEND: str = constants.Templates.REFLEX_BUILD_FRONTEND
  268. # The reflex.build backend host.
  269. REFLEX_BUILD_BACKEND: str = constants.Templates.REFLEX_BUILD_BACKEND
  270. def __init__(self):
  271. """Initialize the environment variables."""
  272. type_hints = get_type_hints(type(self))
  273. for field in dataclasses.fields(self):
  274. raw_value = os.getenv(field.name, None)
  275. field.type = type_hints.get(field.name) or field.type
  276. value = (
  277. interpret_env_var_value(raw_value, field.type, field.name)
  278. if raw_value is not None
  279. else get_default_value_for_field(field)
  280. )
  281. setattr(self, field.name, value)
  282. environment = EnvironmentVariables()
  283. class Config(Base):
  284. """The config defines runtime settings for the app.
  285. By default, the config is defined in an `rxconfig.py` file in the root of the app.
  286. ```python
  287. # rxconfig.py
  288. import reflex as rx
  289. config = rx.Config(
  290. app_name="myapp",
  291. api_url="http://localhost:8000",
  292. )
  293. ```
  294. Every config value can be overridden by an environment variable with the same name in uppercase.
  295. For example, `db_url` can be overridden by setting the `DB_URL` environment variable.
  296. See the [configuration](https://reflex.dev/docs/getting-started/configuration/) docs for more info.
  297. """
  298. class Config:
  299. """Pydantic config for the config."""
  300. validate_assignment = True
  301. # The name of the app (should match the name of the app directory).
  302. app_name: str
  303. # The log level to use.
  304. loglevel: constants.LogLevel = constants.LogLevel.DEFAULT
  305. # The port to run the frontend on. NOTE: When running in dev mode, the next available port will be used if this is taken.
  306. frontend_port: int = constants.DefaultPorts.FRONTEND_PORT
  307. # The path to run the frontend on. For example, "/app" will run the frontend on http://localhost:3000/app
  308. frontend_path: str = ""
  309. # The port to run the backend on. NOTE: When running in dev mode, the next available port will be used if this is taken.
  310. backend_port: int = constants.DefaultPorts.BACKEND_PORT
  311. # The backend url the frontend will connect to. This must be updated if the backend is hosted elsewhere, or in production.
  312. api_url: str = f"http://localhost:{backend_port}"
  313. # The url the frontend will be hosted on.
  314. deploy_url: Optional[str] = f"http://localhost:{frontend_port}"
  315. # The url the backend will be hosted on.
  316. backend_host: str = "0.0.0.0"
  317. # The database url used by rx.Model.
  318. db_url: Optional[str] = "sqlite:///reflex.db"
  319. # The redis url
  320. redis_url: Optional[str] = None
  321. # Telemetry opt-in.
  322. telemetry_enabled: bool = True
  323. # The bun path
  324. bun_path: Path = constants.Bun.DEFAULT_PATH
  325. # List of origins that are allowed to connect to the backend API.
  326. cors_allowed_origins: List[str] = ["*"]
  327. # Tailwind config.
  328. tailwind: Optional[Dict[str, Any]] = {"plugins": ["@tailwindcss/typography"]}
  329. # Timeout when launching the gunicorn server. TODO(rename this to backend_timeout?)
  330. timeout: int = 120
  331. # Whether to enable or disable nextJS gzip compression.
  332. next_compression: bool = True
  333. # Whether to use React strict mode in nextJS
  334. react_strict_mode: bool = True
  335. # Additional frontend packages to install.
  336. frontend_packages: List[str] = []
  337. # The hosting service backend URL.
  338. cp_backend_url: str = Hosting.CP_BACKEND_URL
  339. # The hosting service frontend URL.
  340. cp_web_url: str = Hosting.CP_WEB_URL
  341. # The worker class used in production mode
  342. gunicorn_worker_class: str = "uvicorn.workers.UvicornH11Worker"
  343. # Number of gunicorn workers from user
  344. gunicorn_workers: Optional[int] = None
  345. # Number of requests before a worker is restarted
  346. gunicorn_max_requests: int = 100
  347. # Variance limit for max requests; gunicorn only
  348. gunicorn_max_requests_jitter: int = 25
  349. # Indicate which type of state manager to use
  350. state_manager_mode: constants.StateManagerMode = constants.StateManagerMode.DISK
  351. # Maximum expiration lock time for redis state manager
  352. redis_lock_expiration: int = constants.Expiration.LOCK
  353. # Token expiration time for redis state manager
  354. redis_token_expiration: int = constants.Expiration.TOKEN
  355. # Attributes that were explicitly set by the user.
  356. _non_default_attributes: Set[str] = pydantic.PrivateAttr(set())
  357. # Path to file containing key-values pairs to override in the environment; Dotenv format.
  358. env_file: Optional[str] = None
  359. def __init__(self, *args, **kwargs):
  360. """Initialize the config values.
  361. Args:
  362. *args: The args to pass to the Pydantic init method.
  363. **kwargs: The kwargs to pass to the Pydantic init method.
  364. Raises:
  365. ConfigError: If some values in the config are invalid.
  366. """
  367. super().__init__(*args, **kwargs)
  368. # Update the config from environment variables.
  369. env_kwargs = self.update_from_env()
  370. for key, env_value in env_kwargs.items():
  371. setattr(self, key, env_value)
  372. # Update default URLs if ports were set
  373. kwargs.update(env_kwargs)
  374. self._non_default_attributes.update(kwargs)
  375. self._replace_defaults(**kwargs)
  376. if (
  377. self.state_manager_mode == constants.StateManagerMode.REDIS
  378. and not self.redis_url
  379. ):
  380. raise ConfigError(
  381. "REDIS_URL is required when using the redis state manager."
  382. )
  383. @property
  384. def module(self) -> str:
  385. """Get the module name of the app.
  386. Returns:
  387. The module name.
  388. """
  389. return ".".join([self.app_name, self.app_name])
  390. def update_from_env(self) -> dict[str, Any]:
  391. """Update the config values based on set environment variables.
  392. If there is a set env_file, it is loaded first.
  393. Returns:
  394. The updated config values.
  395. """
  396. if self.env_file:
  397. try:
  398. from dotenv import load_dotenv # type: ignore
  399. # load env file if exists
  400. load_dotenv(self.env_file, override=True)
  401. except ImportError:
  402. console.error(
  403. """The `python-dotenv` package is required to load environment variables from a file. Run `pip install "python-dotenv>=1.0.1"`."""
  404. )
  405. updated_values = {}
  406. # Iterate over the fields.
  407. for key, field in self.__fields__.items():
  408. # The env var name is the key in uppercase.
  409. env_var = os.environ.get(key.upper())
  410. # If the env var is set, override the config value.
  411. if env_var is not None:
  412. if key.upper() != "DB_URL":
  413. console.info(
  414. f"Overriding config value {key} with env var {key.upper()}={env_var}",
  415. dedupe=True,
  416. )
  417. # Interpret the value.
  418. value = interpret_env_var_value(env_var, field.type_, field.name)
  419. # Set the value.
  420. updated_values[key] = value
  421. return updated_values
  422. def get_event_namespace(self) -> str:
  423. """Get the path that the backend Websocket server lists on.
  424. Returns:
  425. The namespace for websocket.
  426. """
  427. event_url = constants.Endpoint.EVENT.get_url()
  428. return urllib.parse.urlsplit(event_url).path
  429. def _replace_defaults(self, **kwargs):
  430. """Replace formatted defaults when the caller provides updates.
  431. Args:
  432. **kwargs: The kwargs passed to the config or from the env.
  433. """
  434. if "api_url" not in self._non_default_attributes and "backend_port" in kwargs:
  435. self.api_url = f"http://localhost:{kwargs['backend_port']}"
  436. if (
  437. "deploy_url" not in self._non_default_attributes
  438. and "frontend_port" in kwargs
  439. ):
  440. self.deploy_url = f"http://localhost:{kwargs['frontend_port']}"
  441. if "api_url" not in self._non_default_attributes:
  442. # If running in Github Codespaces, override API_URL
  443. codespace_name = os.getenv("CODESPACE_NAME")
  444. GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN = os.getenv(
  445. "GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN"
  446. )
  447. # If running on Replit.com interactively, override API_URL to ensure we maintain the backend_port
  448. replit_dev_domain = os.getenv("REPLIT_DEV_DOMAIN")
  449. backend_port = kwargs.get("backend_port", self.backend_port)
  450. if codespace_name and GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN:
  451. self.api_url = (
  452. f"https://{codespace_name}-{kwargs.get('backend_port', self.backend_port)}"
  453. f".{GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
  454. )
  455. elif replit_dev_domain and backend_port:
  456. self.api_url = f"https://{replit_dev_domain}:{backend_port}"
  457. def _set_persistent(self, **kwargs):
  458. """Set values in this config and in the environment so they persist into subprocess.
  459. Args:
  460. **kwargs: The kwargs passed to the config.
  461. """
  462. for key, value in kwargs.items():
  463. if value is not None:
  464. os.environ[key.upper()] = str(value)
  465. setattr(self, key, value)
  466. self._non_default_attributes.update(kwargs)
  467. self._replace_defaults(**kwargs)
  468. def get_config(reload: bool = False) -> Config:
  469. """Get the app config.
  470. Args:
  471. reload: Re-import the rxconfig module from disk
  472. Returns:
  473. The app config.
  474. """
  475. sys.path.insert(0, os.getcwd())
  476. # only import the module if it exists. If a module spec exists then
  477. # the module exists.
  478. spec = importlib.util.find_spec(constants.Config.MODULE) # type: ignore
  479. if not spec:
  480. # we need this condition to ensure that a ModuleNotFound error is not thrown when
  481. # running unit/integration tests.
  482. return Config(app_name="")
  483. rxconfig = importlib.import_module(constants.Config.MODULE)
  484. if reload:
  485. importlib.reload(rxconfig)
  486. return rxconfig.config