Fixing .env loading.
This commit is contained in:
parent
571583bcf4
commit
1006d02d7f
2 changed files with 28 additions and 16 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import os
|
import os
|
||||||
|
import os
|
||||||
from typing import Dict, Any, Optional, Type
|
from typing import Dict, Any, Optional, Type
|
||||||
from urllib.parse import quote_plus
|
from urllib.parse import quote_plus
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
@ -25,17 +26,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 +90,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 +152,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,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ def create_app(config_cls=DevConfig) -> Flask:
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
runtime = init_app(app, config=ProdConfig)
|
runtime = init_app(app, config=ProdConfig)
|
||||||
|
print(f"Effective DB URL: {str(runtime.engine.url)}")
|
||||||
|
|
||||||
from . import models as _models
|
from . import models as _models
|
||||||
_models.Base.metadata.create_all(bind=runtime.engine)
|
_models.Base.metadata.create_all(bind=runtime.engine)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue