From 9f2b043f1bfc4a21b63e71ad33e8bb9a9dd64f69 Mon Sep 17 00:00:00 2001 From: Bo Xu Date: Wed, 14 Oct 2020 19:35:54 -0700 Subject: [PATCH] Add an endpoint to clear the memcache (#596) Co-authored-by: beets --- server/configmodule.py | 2 ++ server/routes/dev.py | 33 ++++++++++++++++++++++++++-- server/templates/dev/clearcache.html | 31 ++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 server/templates/dev/clearcache.html diff --git a/server/configmodule.py b/server/configmodule.py index 57f106c68f..b9fde5741f 100644 --- a/server/configmodule.py +++ b/server/configmodule.py @@ -14,6 +14,7 @@ class Config: class ProductionConfig(Config): + PROJECT = 'datcom-browser-prod' API_PROJECT = 'datcom-mixer' API_ROOT = 'https://api.datacommons.org' GCS_BUCKET = 'datcom-browser-prod.appspot.com' @@ -21,6 +22,7 @@ class ProductionConfig(Config): class DevelopmentConfig(Config): + PROJECT = 'datcom-browser-staging' API_PROJECT = 'datcom-mixer-staging' API_ROOT = 'https://datacommons.endpoints.datcom-mixer-staging.cloud.goog' GCS_BUCKET = 'datcom-browser-staging.appspot.com' diff --git a/server/routes/dev.py b/server/routes/dev.py index 5ed0e40dc1..64252523df 100644 --- a/server/routes/dev.py +++ b/server/routes/dev.py @@ -13,11 +13,13 @@ # limitations under the License. import os - import flask -from flask import Blueprint +from flask import Blueprint, request +from werkzeug.utils import import_string +from cache import cache from lib.gcs import list_png +from google.cloud import secretmanager SCREENSHOT_BUCKET = 'datcom-browser-screenshot' @@ -38,3 +40,30 @@ def screenshot(folder): flask.abort(404) images = list_png(SCREENSHOT_BUCKET, folder) return flask.render_template('dev/screenshot.html', images=images) + + +@bp.route('/clearcache') +def clearcache(): + return flask.render_template('dev/clearcache.html') + + +@bp.route('/clearcache/action', methods=['POST']) +def clearcacheaction(): + user_input = request.form.get('secret') + if os.environ.get('FLASK_ENV') == 'development': + cfg = import_string('configmodule.DevelopmentConfig')() + elif os.environ.get('FLASK_ENV') == 'production': + cfg = import_string('configmodule.ProductionConfig')() + else: + cfg = None + flask.abort(500) + secret_client = secretmanager.SecretManagerServiceClient() + secret_name = secret_client.secret_version_path(cfg.PROJECT, 'clearcache', + '1') + secret_response = secret_client.access_secret_version(secret_name) + token = secret_response.payload.data.decode('UTF-8') + if user_input == token: + success = cache.clear() + if success: + return "Success" + return "Fail" \ No newline at end of file diff --git a/server/templates/dev/clearcache.html b/server/templates/dev/clearcache.html new file mode 100644 index 0000000000..203bf05ddc --- /dev/null +++ b/server/templates/dev/clearcache.html @@ -0,0 +1,31 @@ +{# + Copyright 2020 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +#} +{% extends 'base.html' %} + +{% block head %} + +{% endblock %} + +{% block content %} +

Enter the secret to clear cache

+ +
+ +

+ +
+ +{% endblock %} \ No newline at end of file