Skip to content

Commit cbca5cb

Browse files
authored
Merge pull request #42 from PennChopMicrobiomeProgram/38-allow-explicit-sqlalchemy-uri-via-env
Switch to single spec for db URI
2 parents 9490522 + 80bf7e9 commit cbca5cb

4 files changed

Lines changed: 14 additions & 24 deletions

File tree

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ COPY . .
77
RUN pip install -r requirements.txt
88
RUN pip install /app/app/sample_registry/
99

10+
# Define env vars for debug and connection info
11+
1012
ENTRYPOINT [ "python" ]
1113
CMD [ "app/app.py" ]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export FLASK_DEBUG=1 && flask --app app/app run
2727

2828
How you want to deploy this will depend on your needs, facilities, and ability. We have it deployed by a Kubernetes cluster but you could also 1) just run it in development mode from a lab computer or 2) setup Nginx/Apache on a dedicated server or 3) run it serverlessly in the cloud (e.g. with [Zappa](https://github.com/zappa/Zappa) on AWS) or 4) do something else. There are lots of well documented examples of deploying Flask sites out there, look around and find what works best for you.
2929

30-
When running, it will default to using a SQLite3 database located in the root of this repository (automatically created if it doesn't already exist). You can change to a PostgreSQL backend by providing the environment variables SAMPLE_REGISTRY_DB_HOST, SAMPLE_REGISTRY_DB_NAME, SAMPLE_REGISTRY_DB_USER, and SAMPLE_REGISTRY_DB_PSWD. If you want to use a different backend, you'll have to do a bit of modification to ``app/sample_registry/src/sample_registry/__init__.py`` and be somewhat familiar with SQLAlchemy URI strings.
30+
When running, it will default to using a SQLite3 database located in the root of this repository (automatically created if it doesn't already exist). You can change to use a different backend by setting the `SAMPLE_REGISTRY_DB_URI` environment variable before running the app. For example, another sqlite database could be specified with a URI like this: `export SAMPLE_REGISTRY_DB_URI=sqlite:////path/to/db.sqlite3`.
3131

3232
## Using the library
3333

34-
The `sample_registry` library can be installed and run anywhere by following the instructions in Development (you don't need to do the `create_test_db` and running the site (bottom two commands)). To connect it to a Postgres backend, you'll need to also set the environment variables `SAMPLE_REGISTRY_DB_HOST`, `SAMPLE_REGISTRY_DB_USER`, `SAMPLE_REGISTRY_DB_NAME`, and `SAMPLE_REGISTRY_DB_PSWD`.
34+
The `sample_registry` library can be installed and run anywhere by following the instructions in Development (you don't need to do the `create_test_db` and running the site (bottom two commands)). To connect to a non-dev backend, see the above on SQLAlchemy URIs.
3535

3636
## Manually build Docker image
3737

app/app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,5 +346,4 @@ def index():
346346

347347

348348
if __name__ == "__main__":
349-
# port = int(os.environ.get("PORT", 80))
350349
app.run(host="0.0.0.0", port=80)

app/sample_registry/src/sample_registry/__init__.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,25 @@
44
from sqlalchemy import create_engine
55
from sqlalchemy.orm import sessionmaker
66

7-
__version__ = "1.0.5"
7+
__version__ = "1.1.0"
88

99

1010
def sample_registry_version():
1111
sys.stderr.write(__version__)
1212

1313

14+
try:
15+
SQLALCHEMY_DATABASE_URI = os.environ["SAMPLE_REGISTRY_DB_URI"]
16+
except KeyError:
17+
sys.stdout.write(
18+
"Missing database connection information in environment, using test SQLite database\n"
19+
)
20+
SQLALCHEMY_DATABASE_URI = f"sqlite:///{Path(__file__).parent.parent.parent.parent.parent.resolve()}/sample_registry.sqlite3"
21+
22+
1423
if "PYTEST_VERSION" in os.environ:
1524
# Set SQLALCHEMY_DATABASE_URI to an in-memory SQLite database for testing
1625
SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"
17-
else:
18-
try:
19-
db_host = os.environ["SAMPLE_REGISTRY_DB_HOST"]
20-
db_user = os.environ["SAMPLE_REGISTRY_DB_USER"]
21-
db_name = os.environ["SAMPLE_REGISTRY_DB_NAME"]
22-
db_pswd = os.environ["SAMPLE_REGISTRY_DB_PSWD"]
23-
SQLALCHEMY_DATABASE_URI = (
24-
f"postgresql://{db_user}:{db_pswd}@{db_host}/{db_name}"
25-
)
26-
except KeyError:
27-
# For development purposes, use a SQLite db prefilled with some demo data
28-
sys.stdout.write(
29-
"Missing database connection information in environment, using test SQLite database\n"
30-
)
31-
sys.stdout.write(
32-
f"SAMPLE_REGISTRY_DB_HOST: {os.environ.get('SAMPLE_REGISTRY_DB_HOST')}\nSAMPLE_REGISTRY_DB_USER: {os.environ.get('SAMPLE_REGISTRY_DB_USER')}\nSAMPLE_REGISTRY_DB_NAME: {os.environ.get('SAMPLE_REGISTRY_DB_NAME')}\nSAMPLE_REGISTRY_DB_PSWD: {os.environ.get('SAMPLE_REGISTRY_DB_PSWD')}\n"
33-
)
34-
SQLALCHEMY_DATABASE_URI = f"sqlite:///{Path(__file__).parent.parent.parent.parent.parent.resolve()}/sample_registry.sqlite3"
35-
36-
print(SQLALCHEMY_DATABASE_URI)
3726

3827
# Create database engine
3928
engine = create_engine(SQLALCHEMY_DATABASE_URI, echo=False)

0 commit comments

Comments
 (0)