config.py 19 KB

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