diff --git a/Dockerfile b/Dockerfile index 2e07dbc..483677e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" ] \ No newline at end of file diff --git a/README.md b/README.md index 9fe112b..4c3d304 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/app.py b/app/app.py index 61229aa..d519a0b 100644 --- a/app/app.py +++ b/app/app.py @@ -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) diff --git a/app/sample_registry/src/sample_registry/__init__.py b/app/sample_registry/src/sample_registry/__init__.py index c61b3b0..fd366a9 100644 --- a/app/sample_registry/src/sample_registry/__init__.py +++ b/app/sample_registry/src/sample_registry/__init__.py @@ -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)