Skip to content

Commit a4d3a9b

Browse files
jsstevensonkorikuzma
authored andcommitted
feat: add python-dotenv (#494)
Add `python-dotenv` to backend entry points. This is a simple quality-of-life improvement driven by my unending frustration with the mountain of environment variables upon which metakb sits. Store a `.env` file in `server/` and it'll load env vars from there. * I have aspirations for a more featureful configuration experience. I'd made an initial attempt [here](GenomicMedLab/software-templates#78) but I'm not totally happy with it. I have an issue [here](GenomicMedLab/software-templates#106) to take up someday where I try and do something better, but I think that would entail implementation in all of the library dependencies first. For now I think this PR is just fine though.
1 parent 43e5340 commit a4d3a9b

File tree

6 files changed

+19
-2
lines changed

6 files changed

+19
-2
lines changed

server/.env.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
METAKB_EB_PROD=
2+
METAKB_DB_URL=bolt://localhost:7687
3+
METAKB_DB_USERNAME=neo4j
4+
METAKB_DB_PASSWORD=neo4j
5+
METAKB_DB_SECRET=
6+
DISEASE_NORM_DB_URL=http://localhost:8000
7+
THERAPY_NORM_DB_URL=http://localhost:8000
8+
GENE_NORM_DB_URL=http://localhost:8000
9+
UTA_DB_URL=postgresql://uta_admin:password@localhost:5432/uta/uta_20210129b
10+
SEQREPO_ROOT_DIR=/usr/local/share/seqrepo/latest

server/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies = [
4040
"boto3",
4141
"botocore",
4242
"asyncclick",
43+
"python-dotenv",
4344
]
4445
dynamic = ["version"]
4546

server/src/metakb/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import boto3
1717
from boto3.exceptions import ResourceLoadException
1818
from botocore.config import Config
19+
from dotenv import load_dotenv
1920
from neo4j import Driver
2021

2122
from metakb import APP_ROOT, DATE_FMT
@@ -38,6 +39,8 @@
3839

3940
_logger = logging.getLogger(__name__)
4041

42+
load_dotenv()
43+
4144

4245
def _echo_info(msg: str) -> None:
4346
"""Log (as INFO) and echo given message.

server/src/metakb/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def _get_credentials(
4949
:return: tuple containing host, and a second tuple containing username/password
5050
"""
5151
if not (uri and credentials[0] and credentials[1]):
52-
if "METAKB_EB_PROD" in environ:
52+
if environ.get("METAKB_EB_PROD"):
5353
secret = ast.literal_eval(_get_secret())
5454
uri = f"bolt://{secret['host']}:{secret['port']}"
5555
credentials = (secret["username"], secret["password"])

server/src/metakb/log_handle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def configure_logs(log_level: int = logging.DEBUG, quiet_upstream: bool = True)
3636
if quiet_upstream:
3737
_quiet_upstream_libs()
3838
log_filename = (
39-
"/tmp/metakb.log" if "METAKB_EB_PROD" in os.environ else "metakb.log" # noqa: S108
39+
"/tmp/metakb.log" if os.environ.get("METAKB_EB_PROD") else "metakb.log" # noqa: S108
4040
)
4141
logging.basicConfig(
4242
filename=log_filename,

server/src/metakb/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from contextlib import asynccontextmanager
55
from typing import Annotated
66

7+
from dotenv import load_dotenv
78
from fastapi import FastAPI, Query
89
from fastapi.openapi.utils import get_openapi
910

@@ -18,6 +19,8 @@
1819
ServiceMeta,
1920
)
2021

22+
load_dotenv()
23+
2124
query = QueryHandler()
2225

2326

0 commit comments

Comments
 (0)