Skip to content

Commit

Permalink
Add an endpoint to clear the memcache (#596)
Browse files Browse the repository at this point in the history
Co-authored-by: beets <[email protected]>
  • Loading branch information
shifucun and beets authored Oct 15, 2020
1 parent 8eb20a6 commit 9f2b043
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions server/configmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ 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'
GA_ACCOUNT = 'UA-117119267-1'


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'
Expand Down
33 changes: 31 additions & 2 deletions server/routes/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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"
31 changes: 31 additions & 0 deletions server/templates/dev/clearcache.html
Original file line number Diff line number Diff line change
@@ -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 %}
<link rel="stylesheet" href={{url_for('static', filename='css/dev.min.css')}}>
{% endblock %}

{% block content %}
<h4>Enter the secret to clear cache</h4>

<form action="/dev/clearcache/action" method="post">
<label for="secret">Secret </label>
<input type="text" id="secret" name="secret"><br><br>
<input type="submit" value="Submit">
</form>

{% endblock %}

0 comments on commit 9f2b043

Please sign in to comment.