Skip to content

Commit

Permalink
Configure choice of backend store #2714
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Jun 20, 2022
1 parent b70b6d7 commit c38a4e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
16 changes: 9 additions & 7 deletions data-serving/reusable-data-service/reusable_data_service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from . import CaseController, MongoStore

import os

import logging

app = Flask(__name__)
case_controller = None # Will be set up in main()
Expand All @@ -13,12 +13,14 @@ def get_case(id):

def set_up_controllers():
global case_controller
# TODO choose which store to load from configuration
mongo_connection_string = os.environ.get('MONGO_CONNECTION')
mongo_database = os.environ.get('MONGO_DB')
mongo_collection = os.environ.get('MONGO_CASE_COLLECTION')
mongo_store = MongoStore(mongo_connection_string, mongo_database, mongo_collection)
case_controller = CaseController(mongo_store)
store_options = { 'mongodb': MongoStore.setup }
if store_choice := os.environ.get('DATA_STORAGE_BACKEND'):
try:
store = store_options[store_choice]()
except KeyError:
logging.exception(f"Cannot configure backend data store {store_choice}")
raise
case_controller = CaseController(store)

def main():
set_up_controllers()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pymongo
from json import loads
from bson.json_util import dumps
Expand All @@ -22,4 +23,13 @@ def get_case_collection(self):
def case_by_id(self, id: str):
case = self.get_case_collection().find_one({"_id": ObjectId(id)})
# case includes BSON fields like ObjectID - convert into JSON for use by the app
return loads(dumps(case))
return loads(dumps(case))

@staticmethod
def setup():
"""Configure a store instance from the environment."""
mongo_connection_string = os.environ.get('MONGO_CONNECTION')
mongo_database = os.environ.get('MONGO_DB')
mongo_collection = os.environ.get('MONGO_CASE_COLLECTION')
mongo_store = MongoStore(mongo_connection_string, mongo_database, mongo_collection)
return mongo_store
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@pytest.fixture
def client_with_patched_mongo(monkeypatch):
# configure controllers
monkeypatch.setenv('DATA_STORAGE_BACKEND', 'mongodb')
monkeypatch.setenv('MONGO_CONNECTION_STRING', 'mongodb://localhost/27017') # will be unused
monkeypatch.setenv('MONGO_DB', 'outbreak')
monkeypatch.setenv('MONGO_CASE_COLLECTION', 'cases')
Expand Down

0 comments on commit c38a4e0

Please sign in to comment.