Fix dotenv loading.
This commit is contained in:
parent
f9458a429e
commit
4cdbc44a13
1 changed files with 27 additions and 17 deletions
|
|
@ -25,17 +25,27 @@ def _load_dotenv_if_present() -> None:
|
||||||
if path_hint:
|
if path_hint:
|
||||||
p = Path(path_hint).resolve()
|
p = Path(path_hint).resolve()
|
||||||
if p.exists():
|
if p.exists():
|
||||||
load_dotenv(dotenv_path=p, override=False)
|
load_dotenv(dotenv_path=p, override=True)
|
||||||
|
os.environ["CRUDKIT_DOTENV_LOADED"] = str(p)
|
||||||
return
|
return
|
||||||
|
|
||||||
repo_env = Path(__file__).resolve().parents[2] / ".env"
|
repo_env = Path(__file__).resolve().parents[1] / ".env"
|
||||||
if repo_env.exists():
|
if repo_env.exists():
|
||||||
load_dotenv(dotenv_path=repo_env, override=False)
|
load_dotenv(dotenv_path=repo_env, override=True)
|
||||||
|
os.environ["CRUDKIT_DOTENV_LOADED"] = str(repo_env)
|
||||||
return
|
return
|
||||||
|
|
||||||
cwd_env = Path.cwd() / ".env"
|
cwd_env = Path.cwd() / ".env"
|
||||||
if cwd_env.exists():
|
if cwd_env.exists():
|
||||||
load_dotenv(dotenv_path=cwd_env, override=False)
|
load_dotenv(dotenv_path=cwd_env, override=True)
|
||||||
|
os.environ["CRUDKIT_DOTENV_LOADED"] = str(cwd_env)
|
||||||
|
|
||||||
|
def _getenv(name: str, default: Optional[str] = None) -> Optional[str]:
|
||||||
|
"""Treat empty strings as missing. Hekos when OS env has DB_BACKEND=''."""
|
||||||
|
val = os.getenv(name)
|
||||||
|
if val is None or val.strip() == "":
|
||||||
|
return default
|
||||||
|
return val
|
||||||
|
|
||||||
def build_database_url(
|
def build_database_url(
|
||||||
*,
|
*,
|
||||||
|
|
@ -79,7 +89,7 @@ def build_database_url(
|
||||||
qs = ""
|
qs = ""
|
||||||
if options:
|
if options:
|
||||||
qs = "?" + "&".join(f"{k}={quote_plus(v)}" for k, v in options.items())
|
qs = "?" + "&".join(f"{k}={quote_plus(v)}" for k, v in options.items())
|
||||||
return f"postgresel+{driver}://{creds}{host}:{port}/{database}{qs}"
|
return f"postgresql+{driver}://{creds}{host}:{port}/{database}{qs}"
|
||||||
|
|
||||||
if backend == "mysql":
|
if backend == "mysql":
|
||||||
driver = driver or "pymysql"
|
driver = driver or "pymysql"
|
||||||
|
|
@ -141,23 +151,23 @@ class Config:
|
||||||
|
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
TESTING = False
|
TESTING = False
|
||||||
SECRET_KEY = os.getenv("SECRET_KEY", "dev-not-secret")
|
SECRET_KEY = _getenv("SECRET_KEY", "dev-not-secret")
|
||||||
|
|
||||||
if not _dotenv_loaded:
|
if not _dotenv_loaded:
|
||||||
_load_dotenv_if_present()
|
_load_dotenv_if_present()
|
||||||
_dotenv_loaded = True
|
_dotenv_loaded = True
|
||||||
|
|
||||||
DATABASE_URL = build_database_url(
|
DATABASE_URL = build_database_url(
|
||||||
url=os.getenv("DATABASE_URL"),
|
url=_getenv("DATABASE_URL"),
|
||||||
backend=os.getenv("DB_BACKEND"),
|
backend=_getenv("DB_BACKEND"),
|
||||||
user=os.getenv("DB_USER"),
|
user=_getenv("DB_USER"),
|
||||||
password=os.getenv("DB_PASS"),
|
password=_getenv("DB_PASS"),
|
||||||
host=os.getenv("DB_HOST"),
|
host=_getenv("DB_HOST"),
|
||||||
port=os.getenv("DB_PORT"),
|
port=_getenv("DB_PORT"),
|
||||||
database=os.getenv("DB_NAME"),
|
database=_getenv("DB_NAME"),
|
||||||
driver=os.getenv("DB_DRIVER"),
|
driver=_getenv("DB_DRIVER"),
|
||||||
dsn=os.getenv("DB_DSN"),
|
dsn=_getenv("DB_DSN"),
|
||||||
trusted=bool(int(os.getenv("DB_TRUSTED", "0"))),
|
trusted=bool(int(_getenv("DB_TRUSTED", "0"))),
|
||||||
options=None,
|
options=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -220,7 +230,7 @@ class ProdConfig(Config):
|
||||||
|
|
||||||
def get_config(name: str | None) -> Type[Config]:
|
def get_config(name: str | None) -> Type[Config]:
|
||||||
"""
|
"""
|
||||||
Resolve config by name. None -> environment variable CRUDKIT_END or 'dev'.
|
Resolve config by name. None -> environment variable CRUDKIT_ENV or 'dev'.
|
||||||
"""
|
"""
|
||||||
env = (name or os.getenv("CRUDKIT_ENV") or "dev").lower()
|
env = (name or os.getenv("CRUDKIT_ENV") or "dev").lower()
|
||||||
if env in {"prod", "production"}:
|
if env in {"prod", "production"}:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue