config.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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 Annotated, 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_existing_path_env(value: str, field_name: str) -> ExistingPath:
  167. """Interpret a path environment variable value as an existing path.
  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_path_env(value: str, field_name: str) -> Path:
  181. """Interpret a path environment variable value.
  182. Args:
  183. value: The environment variable value.
  184. field_name: The field name.
  185. Returns:
  186. The interpreted value.
  187. """
  188. return Path(value)
  189. def interpret_enum_env(value: str, field_type: GenericType, field_name: str) -> Any:
  190. """Interpret an enum environment variable value.
  191. Args:
  192. value: The environment variable value.
  193. field_type: The field type.
  194. field_name: The field name.
  195. Returns:
  196. The interpreted value.
  197. Raises:
  198. EnvironmentVarValueError: If the value is invalid.
  199. """
  200. try:
  201. return field_type(value)
  202. except ValueError as ve:
  203. raise EnvironmentVarValueError(
  204. f"Invalid enum value: {value} for {field_name}"
  205. ) from ve
  206. def interpret_env_var_value(
  207. value: str, field_type: GenericType, field_name: str
  208. ) -> Any:
  209. """Interpret an environment variable value based on the field type.
  210. Args:
  211. value: The environment variable value.
  212. field_type: The field type.
  213. field_name: The field name.
  214. Returns:
  215. The interpreted value.
  216. Raises:
  217. ValueError: If the value is invalid.
  218. """
  219. field_type = value_inside_optional(field_type)
  220. if is_union(field_type):
  221. raise ValueError(
  222. f"Union types are not supported for environment variables: {field_name}."
  223. )
  224. if field_type is bool:
  225. return interpret_boolean_env(value, field_name)
  226. elif field_type is str:
  227. return value
  228. elif field_type is int:
  229. return interpret_int_env(value, field_name)
  230. elif field_type is Path:
  231. return interpret_path_env(value, field_name)
  232. elif field_type is ExistingPath:
  233. return interpret_existing_path_env(value, field_name)
  234. elif inspect.isclass(field_type) and issubclass(field_type, enum.Enum):
  235. return interpret_enum_env(value, field_type, field_name)
  236. else:
  237. raise ValueError(
  238. f"Invalid type for environment variable {field_name}: {field_type}. This is probably an issue in Reflex."
  239. )
  240. class PathExistsFlag:
  241. """Flag to indicate that a path must exist."""
  242. ExistingPath = Annotated[Path, PathExistsFlag]
  243. @dataclasses.dataclass(init=False)
  244. class EnvironmentVariables:
  245. """Environment variables class to instantiate environment variables."""
  246. # Whether to use npm over bun to install frontend packages.
  247. REFLEX_USE_NPM: bool = False
  248. # The npm registry to use.
  249. NPM_CONFIG_REGISTRY: Optional[str] = None
  250. # Whether to use Granian for the backend. Otherwise, use Uvicorn.
  251. REFLEX_USE_GRANIAN: bool = False
  252. # The username to use for authentication on python package repository. Username and password must both be provided.
  253. TWINE_USERNAME: Optional[str] = None
  254. # The password to use for authentication on python package repository. Username and password must both be provided.
  255. TWINE_PASSWORD: Optional[str] = None
  256. # Whether to use the system installed bun. If set to false, bun will be bundled with the app.
  257. REFLEX_USE_SYSTEM_BUN: bool = False
  258. # Whether to use the system installed node and npm. If set to false, node and npm will be bundled with the app.
  259. REFLEX_USE_SYSTEM_NODE: bool = False
  260. # The working directory for the next.js commands.
  261. REFLEX_WEB_WORKDIR: Path = Path(constants.Dirs.WEB)
  262. # Path to the alembic config file
  263. ALEMBIC_CONFIG: ExistingPath = Path(constants.ALEMBIC_CONFIG)
  264. # Disable SSL verification for HTTPX requests.
  265. SSL_NO_VERIFY: bool = False
  266. # The directory to store uploaded files.
  267. REFLEX_UPLOADED_FILES_DIR: Path = Path(constants.Dirs.UPLOADED_FILES)
  268. # Whether to use seperate processes to compile the frontend and how many. If not set, defaults to thread executor.
  269. REFLEX_COMPILE_PROCESSES: Optional[int] = None
  270. # Whether to use seperate threads to compile the frontend and how many. Defaults to `min(32, os.cpu_count() + 4)`.
  271. REFLEX_COMPILE_THREADS: Optional[int] = None
  272. # The directory to store reflex dependencies.
  273. REFLEX_DIR: Path = Path(constants.Reflex.DIR)
  274. # Whether to print the SQL queries if the log level is INFO or lower.
  275. SQLALCHEMY_ECHO: bool = False
  276. # Whether to ignore the redis config error. Some redis servers only allow out-of-band configuration.
  277. REFLEX_IGNORE_REDIS_CONFIG_ERROR: bool = False
  278. # Whether to skip purging the web directory in dev mode.
  279. REFLEX_PERSIST_WEB_DIR: bool = False
  280. # The reflex.build frontend host.
  281. REFLEX_BUILD_FRONTEND: str = constants.Templates.REFLEX_BUILD_FRONTEND
  282. # The reflex.build backend host.
  283. REFLEX_BUILD_BACKEND: str = constants.Templates.REFLEX_BUILD_BACKEND
  284. def __init__(self):
  285. """Initialize the environment variables."""
  286. type_hints = get_type_hints(type(self))
  287. for field in dataclasses.fields(self):
  288. raw_value = os.getenv(field.name, None)
  289. field.type = type_hints.get(field.name) or field.type
  290. value = (
  291. interpret_env_var_value(raw_value, field.type, field.name)
  292. if raw_value is not None
  293. else get_default_value_for_field(field)
  294. )
  295. setattr(self, field.name, value)
  296. environment = EnvironmentVariables()
  297. class Config(Base):
  298. """The config defines runtime settings for the app.
  299. By default, the config is defined in an `rxconfig.py` file in the root of the app.
  300. ```python
  301. # rxconfig.py
  302. import reflex as rx
  303. config = rx.Config(
  304. app_name="myapp",
  305. api_url="http://localhost:8000",
  306. )
  307. ```
  308. Every config value can be overridden by an environment variable with the same name in uppercase.
  309. For example, `db_url` can be overridden by setting the `DB_URL` environment variable.
  310. See the [configuration](https://reflex.dev/docs/getting-started/configuration/) docs for more info.
  311. """
  312. class Config:
  313. """Pydantic config for the config."""
  314. validate_assignment = True
  315. # The name of the app (should match the name of the app directory).
  316. app_name: str
  317. # The log level to use.
  318. loglevel: constants.LogLevel = constants.LogLevel.DEFAULT
  319. # The port to run the frontend on. NOTE: When running in dev mode, the next available port will be used if this is taken.
  320. frontend_port: int = constants.DefaultPorts.FRONTEND_PORT
  321. # The path to run the frontend on. For example, "/app" will run the frontend on http://localhost:3000/app
  322. frontend_path: str = ""
  323. # The port to run the backend on. NOTE: When running in dev mode, the next available port will be used if this is taken.
  324. backend_port: int = constants.DefaultPorts.BACKEND_PORT
  325. # The backend url the frontend will connect to. This must be updated if the backend is hosted elsewhere, or in production.
  326. api_url: str = f"http://localhost:{backend_port}"
  327. # The url the frontend will be hosted on.
  328. deploy_url: Optional[str] = f"http://localhost:{frontend_port}"
  329. # The url the backend will be hosted on.
  330. backend_host: str = "0.0.0.0"
  331. # The database url used by rx.Model.
  332. db_url: Optional[str] = "sqlite:///reflex.db"
  333. # The redis url
  334. redis_url: Optional[str] = None
  335. # Telemetry opt-in.
  336. telemetry_enabled: bool = True
  337. # The bun path
  338. bun_path: ExistingPath = constants.Bun.DEFAULT_PATH
  339. # List of origins that are allowed to connect to the backend API.
  340. cors_allowed_origins: List[str] = ["*"]
  341. # Tailwind config.
  342. tailwind: Optional[Dict[str, Any]] = {"plugins": ["@tailwindcss/typography"]}
  343. # Timeout when launching the gunicorn server. TODO(rename this to backend_timeout?)
  344. timeout: int = 120
  345. # Whether to enable or disable nextJS gzip compression.
  346. next_compression: bool = True
  347. # Whether to use React strict mode in nextJS
  348. react_strict_mode: bool = True
  349. # Additional frontend packages to install.
  350. frontend_packages: List[str] = []
  351. # The hosting service backend URL.
  352. cp_backend_url: str = Hosting.CP_BACKEND_URL
  353. # The hosting service frontend URL.
  354. cp_web_url: str = Hosting.CP_WEB_URL
  355. # The worker class used in production mode
  356. gunicorn_worker_class: str = "uvicorn.workers.UvicornH11Worker"
  357. # Number of gunicorn workers from user
  358. gunicorn_workers: Optional[int] = None
  359. # Number of requests before a worker is restarted
  360. gunicorn_max_requests: int = 100
  361. # Variance limit for max requests; gunicorn only
  362. gunicorn_max_requests_jitter: int = 25
  363. # Indicate which type of state manager to use
  364. state_manager_mode: constants.StateManagerMode = constants.StateManagerMode.DISK
  365. # Maximum expiration lock time for redis state manager
  366. redis_lock_expiration: int = constants.Expiration.LOCK
  367. # Token expiration time for redis state manager
  368. redis_token_expiration: int = constants.Expiration.TOKEN
  369. # Attributes that were explicitly set by the user.
  370. _non_default_attributes: Set[str] = pydantic.PrivateAttr(set())
  371. # Path to file containing key-values pairs to override in the environment; Dotenv format.
  372. env_file: Optional[str] = None
  373. def __init__(self, *args, **kwargs):
  374. """Initialize the config values.
  375. Args:
  376. *args: The args to pass to the Pydantic init method.
  377. **kwargs: The kwargs to pass to the Pydantic init method.
  378. Raises:
  379. ConfigError: If some values in the config are invalid.
  380. """
  381. super().__init__(*args, **kwargs)
  382. # Update the config from environment variables.
  383. env_kwargs = self.update_from_env()
  384. for key, env_value in env_kwargs.items():
  385. setattr(self, key, env_value)
  386. # Update default URLs if ports were set
  387. kwargs.update(env_kwargs)
  388. self._non_default_attributes.update(kwargs)
  389. self._replace_defaults(**kwargs)
  390. if (
  391. self.state_manager_mode == constants.StateManagerMode.REDIS
  392. and not self.redis_url
  393. ):
  394. raise ConfigError(
  395. "REDIS_URL is required when using the redis state manager."
  396. )
  397. @property
  398. def module(self) -> str:
  399. """Get the module name of the app.
  400. Returns:
  401. The module name.
  402. """
  403. return ".".join([self.app_name, self.app_name])
  404. def update_from_env(self) -> dict[str, Any]:
  405. """Update the config values based on set environment variables.
  406. If there is a set env_file, it is loaded first.
  407. Returns:
  408. The updated config values.
  409. """
  410. if self.env_file:
  411. try:
  412. from dotenv import load_dotenv # type: ignore
  413. # load env file if exists
  414. load_dotenv(self.env_file, override=True)
  415. except ImportError:
  416. console.error(
  417. """The `python-dotenv` package is required to load environment variables from a file. Run `pip install "python-dotenv>=1.0.1"`."""
  418. )
  419. updated_values = {}
  420. # Iterate over the fields.
  421. for key, field in self.__fields__.items():
  422. # The env var name is the key in uppercase.
  423. env_var = os.environ.get(key.upper())
  424. # If the env var is set, override the config value.
  425. if env_var is not None:
  426. if key.upper() != "DB_URL":
  427. console.info(
  428. f"Overriding config value {key} with env var {key.upper()}={env_var}",
  429. dedupe=True,
  430. )
  431. # Interpret the value.
  432. value = interpret_env_var_value(env_var, field.outer_type_, field.name)
  433. # Set the value.
  434. updated_values[key] = value
  435. return updated_values
  436. def get_event_namespace(self) -> str:
  437. """Get the path that the backend Websocket server lists on.
  438. Returns:
  439. The namespace for websocket.
  440. """
  441. event_url = constants.Endpoint.EVENT.get_url()
  442. return urllib.parse.urlsplit(event_url).path
  443. def _replace_defaults(self, **kwargs):
  444. """Replace formatted defaults when the caller provides updates.
  445. Args:
  446. **kwargs: The kwargs passed to the config or from the env.
  447. """
  448. if "api_url" not in self._non_default_attributes and "backend_port" in kwargs:
  449. self.api_url = f"http://localhost:{kwargs['backend_port']}"
  450. if (
  451. "deploy_url" not in self._non_default_attributes
  452. and "frontend_port" in kwargs
  453. ):
  454. self.deploy_url = f"http://localhost:{kwargs['frontend_port']}"
  455. if "api_url" not in self._non_default_attributes:
  456. # If running in Github Codespaces, override API_URL
  457. codespace_name = os.getenv("CODESPACE_NAME")
  458. GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN = os.getenv(
  459. "GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN"
  460. )
  461. # If running on Replit.com interactively, override API_URL to ensure we maintain the backend_port
  462. replit_dev_domain = os.getenv("REPLIT_DEV_DOMAIN")
  463. backend_port = kwargs.get("backend_port", self.backend_port)
  464. if codespace_name and GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN:
  465. self.api_url = (
  466. f"https://{codespace_name}-{kwargs.get('backend_port', self.backend_port)}"
  467. f".{GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
  468. )
  469. elif replit_dev_domain and backend_port:
  470. self.api_url = f"https://{replit_dev_domain}:{backend_port}"
  471. def _set_persistent(self, **kwargs):
  472. """Set values in this config and in the environment so they persist into subprocess.
  473. Args:
  474. **kwargs: The kwargs passed to the config.
  475. """
  476. for key, value in kwargs.items():
  477. if value is not None:
  478. os.environ[key.upper()] = str(value)
  479. setattr(self, key, value)
  480. self._non_default_attributes.update(kwargs)
  481. self._replace_defaults(**kwargs)
  482. def get_config(reload: bool = False) -> Config:
  483. """Get the app config.
  484. Args:
  485. reload: Re-import the rxconfig module from disk
  486. Returns:
  487. The app config.
  488. """
  489. sys.path.insert(0, os.getcwd())
  490. # only import the module if it exists. If a module spec exists then
  491. # the module exists.
  492. spec = importlib.util.find_spec(constants.Config.MODULE) # type: ignore
  493. if not spec:
  494. # we need this condition to ensure that a ModuleNotFound error is not thrown when
  495. # running unit/integration tests.
  496. return Config(app_name="")
  497. rxconfig = importlib.import_module(constants.Config.MODULE)
  498. if reload:
  499. importlib.reload(rxconfig)
  500. return rxconfig.config