Skip to content

Commit

Permalink
Merge 45d27a8 into db40aec
Browse files Browse the repository at this point in the history
  • Loading branch information
Avantol13 committed Mar 20, 2020
2 parents db40aec + 45d27a8 commit 94c68c2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
16 changes: 16 additions & 0 deletions indexd/app.py
@@ -1,4 +1,5 @@
import flask
from urllib.parse import urlparse
from .bulk.blueprint import blueprint as indexd_bulk_blueprint
from .index.blueprint import blueprint as indexd_index_blueprint
from .alias.blueprint import blueprint as indexd_alias_blueprint
Expand Down Expand Up @@ -48,5 +49,20 @@ def get_app(settings=None):
pass

app_init(app, settings)
_setup_redis_client(app)

return app


def _setup_redis_client(app):
"""
Sets up the redis client based on config
"""
redis_url_parts = urlparse(app.config["REDIS_HOST"])
ssl = redis_url_parts.scheme == "https"
app.redis_client = redis.Redis(
host=redis_url_parts.netloc,
port=app.config["REDIS_PORT"],
db=app.config["REDIS_DB"],
ssl=ssl,
)
4 changes: 4 additions & 0 deletions indexd/default_settings.py
Expand Up @@ -57,4 +57,8 @@

AUTH = SQLAlchemyAuthDriver("sqlite:///auth.sq3")

CONFIG["REDIS_HOST"]: "http://redis-service"
CONFIG["REDIS_PORT"]: "6379"
CONFIG["REDIS_DB"]: 0

settings = {"config": CONFIG, "auth": AUTH}
40 changes: 38 additions & 2 deletions indexd/index/blueprint.py
Expand Up @@ -220,9 +220,19 @@ def get_index_record(record):
Returns a record.
"""

ret = blueprint.index_driver.get(record)
# check redis
json_record = flask.current_app.redis_client.get(record)
if json_record:
json_record = json_record.loads(json_record)
else:
# get from db
ret = blueprint.index_driver.get(record)
json_record = flask.jsonify(ret)

return flask.jsonify(ret), 200
# update redis
flask.current_app.redis_client.set(record, json.dumps(json_record))

return json_record, 200


@blueprint.route("/index/", methods=["POST"])
Expand Down Expand Up @@ -311,6 +321,9 @@ def put_index_blank_record(record):
)
ret = {"did": did, "rev": rev, "baseid": baseid}

# invalidate redis for this did
flask.current_app.redis_client.delete(did)

return flask.jsonify(ret), 200


Expand All @@ -330,6 +343,9 @@ def put_index_record(record):

ret = {"did": did, "baseid": baseid, "rev": rev}

# invalidate redis for this did
flask.current_app.redis_client.delete(did)

return flask.jsonify(ret), 200


Expand All @@ -345,6 +361,9 @@ def delete_index_record(record):
# authorize done in delete
blueprint.index_driver.delete(record, rev)

# invalidate redis for this did
flask.current_app.redis_client.delete(record)

return "", 200


Expand Down Expand Up @@ -388,6 +407,9 @@ def add_index_record_version(record):

ret = {"did": did, "baseid": baseid, "rev": rev}

# invalidate redis for this did
flask.current_app.redis_client.delete(did)

return flask.jsonify(ret), 200


Expand Down Expand Up @@ -425,6 +447,10 @@ def append_aliases(record):

aliases = blueprint.index_driver.get_aliases_for_did(record)
aliases_payload = {"aliases": [{"value": alias} for alias in aliases]}

# invalidate redis for this did
flask.current_app.redis_client.delete(record)

return flask.jsonify(aliases_payload), 200


Expand All @@ -448,6 +474,10 @@ def replace_aliases(record):
blueprint.index_driver.replace_aliases_for_did(aliases, record)

aliases_payload = {"aliases": [{"value": alias} for alias in aliases]}

# invalidate redis for this did
flask.current_app.redis_client.delete(record)

return flask.jsonify(aliases_payload), 200


Expand All @@ -456,6 +486,9 @@ def delete_all_aliases(record):
# authorization and error handling done in driver
blueprint.index_driver.delete_all_aliases_for_did(record)

# invalidate redis for this did
flask.current_app.redis_client.delete(record)

return flask.jsonify("Aliases deleted successfully"), 200


Expand All @@ -464,6 +497,9 @@ def delete_one_alias(record, alias):
# authorization and error handling done in driver
blueprint.index_driver.delete_one_alias_for_did(alias, record)

# invalidate redis for this did
flask.current_app.redis_client.delete(record)

return flask.jsonify("Aliases deleted successfully"), 200


Expand Down
4 changes: 4 additions & 0 deletions tests/default_test_settings.py
Expand Up @@ -20,6 +20,10 @@

os.environ["PRESIGNED_FENCE_URL"] = "https://fictitious-commons.io/"
os.environ["HOSTNAME"] = "fictitious-commons.io"
CONFIG["REDIS_HOST"]: "http://redis-service"
CONFIG["REDIS_PORT"]: "6379"
CONFIG["REDIS_DB"]: 0

settings = {"config": CONFIG, "auth": AUTH}

settings["config"]["TEST_DB"] = "postgres://postgres@localhost/test_migration_db"

0 comments on commit 94c68c2

Please sign in to comment.