From 06ab2540172ffd21dbee98c91d243e39722ad05e Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Tue, 18 Sep 2018 16:59:28 +0300 Subject: [PATCH] Refactor configuration into separate module There is a good example of this in the Project Weekend GitHub profile. See: https://github.com/projectweekend/Falcon-PostgreSQL-API-Seed --- app.py | 12 +++++------- config.py | 5 +++++ 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 config.py diff --git a/app.py b/app.py index 0f16fed..1ebf1c2 100644 --- a/app.py +++ b/app.py @@ -2,13 +2,11 @@ # See DSpace Solr docs for tips about parameters # https://wiki.duraspace.org/display/DSPACE/Solr +from config import SOLR_SERVER +from config import SOLR_CORE import falcon -import os from SolrClient import SolrClient -# Check if Solr connection information was provided in the environment -solr_server = os.environ.get('SOLR_SERVER', 'http://localhost:8080/solr') -solr_core = os.environ.get('SOLR_CORE', 'statistics') class ItemResource: def on_get(self, req, resp): @@ -16,10 +14,10 @@ class ItemResource: # Return HTTPBadRequest if id parameter is not present and valid item_id = req.get_param_as_int("id", required=True, min=0) - solr = SolrClient(solr_server) + solr = SolrClient(SOLR_SERVER) # Get views - res = solr.query(solr_core, { + res = solr.query(SOLR_CORE, { 'q':'type:0', 'fq':'owningItem:{0} AND isBot:false AND statistics_type:view AND -bundleName:ORIGINAL'.format(item_id) }) @@ -27,7 +25,7 @@ class ItemResource: views = res.get_num_found() # Get downloads - res = solr.query(solr_core, { + res = solr.query(SOLR_CORE, { 'q':'type:0', 'fq':'owningItem:{0} AND isBot:false AND statistics_type:view AND -(bundleName:[* TO *] -bundleName:ORIGINAL)'.format(item_id) }) diff --git a/config.py b/config.py new file mode 100644 index 0000000..19a043e --- /dev/null +++ b/config.py @@ -0,0 +1,5 @@ +import os + +# Check if Solr connection information was provided in the environment +SOLR_SERVER = os.environ.get('SOLR_SERVER', 'http://localhost:8080/solr') +SOLR_CORE = os.environ.get('SOLR_CORE', 'statistics')