Skip to content

Commit

Permalink
Merge pull request #42 from PennChopMicrobiomeProgram/38-allow-explic…
Browse files Browse the repository at this point in the history
…it-sqlalchemy-uri-via-env

Switch to single spec for db URI
  • Loading branch information
Ulthran authored Dec 5, 2024
2 parents 9490522 + 80bf7e9 commit cbca5cb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ COPY . .
RUN pip install -r requirements.txt
RUN pip install /app/app/sample_registry/

# Define env vars for debug and connection info

ENTRYPOINT [ "python" ]
CMD [ "app/app.py" ]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export FLASK_DEBUG=1 && flask --app app/app run

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.

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.
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`.

## Using the library

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`.
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.

## Manually build Docker image

Expand Down
1 change: 0 additions & 1 deletion app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,4 @@ def index():


if __name__ == "__main__":
# port = int(os.environ.get("PORT", 80))
app.run(host="0.0.0.0", port=80)
31 changes: 10 additions & 21 deletions app/sample_registry/src/sample_registry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,25 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

__version__ = "1.0.5"
__version__ = "1.1.0"


def sample_registry_version():
sys.stderr.write(__version__)


try:
SQLALCHEMY_DATABASE_URI = os.environ["SAMPLE_REGISTRY_DB_URI"]
except KeyError:
sys.stdout.write(
"Missing database connection information in environment, using test SQLite database\n"
)
SQLALCHEMY_DATABASE_URI = f"sqlite:///{Path(__file__).parent.parent.parent.parent.parent.resolve()}/sample_registry.sqlite3"


if "PYTEST_VERSION" in os.environ:
# Set SQLALCHEMY_DATABASE_URI to an in-memory SQLite database for testing
SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"
else:
try:
db_host = os.environ["SAMPLE_REGISTRY_DB_HOST"]
db_user = os.environ["SAMPLE_REGISTRY_DB_USER"]
db_name = os.environ["SAMPLE_REGISTRY_DB_NAME"]
db_pswd = os.environ["SAMPLE_REGISTRY_DB_PSWD"]
SQLALCHEMY_DATABASE_URI = (
f"postgresql://{db_user}:{db_pswd}@{db_host}/{db_name}"
)
except KeyError:
# For development purposes, use a SQLite db prefilled with some demo data
sys.stdout.write(
"Missing database connection information in environment, using test SQLite database\n"
)
sys.stdout.write(
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"
)
SQLALCHEMY_DATABASE_URI = f"sqlite:///{Path(__file__).parent.parent.parent.parent.parent.resolve()}/sample_registry.sqlite3"

print(SQLALCHEMY_DATABASE_URI)

# Create database engine
engine = create_engine(SQLALCHEMY_DATABASE_URI, echo=False)
Expand Down

0 comments on commit cbca5cb

Please sign in to comment.