Skip to content

Commit 6283f39

Browse files
author
Jon Wayne Parrott
committed
Adding redis flexible sample.
Change-Id: I4a37c5e2ce724ef2b130f76a97ca999aebec0d01
1 parent 90f539d commit 6283f39

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

appengine/flexible/redis/app.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
runtime: python
2+
vm: true
3+
entrypoint: gunicorn -b :$PORT main:app
4+
5+
runtime_config:
6+
python_version: 3
7+
8+
# [START env_variables]
9+
env_variables:
10+
REDIS_HOST: your-redis-host
11+
REDIS_PORT: your-redis-port
12+
REDIS_PASSWORD: your-redis-password
13+
# [END env_variables]

appengine/flexible/redis/main.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
import os
17+
18+
from flask import Flask
19+
import redis
20+
21+
22+
app = Flask(__name__)
23+
24+
25+
# [START client]
26+
redis_host = os.environ.get('REDIS_HOST', 'localhost')
27+
redis_port = int(os.environ.get('REDIS_PORT', 6379))
28+
redis_password = os.environ.get('REDIS_PASSWORD', None)
29+
redis_client = redis.StrictRedis(
30+
host=redis_host, port=redis_port, password=redis_password)
31+
# [END client]
32+
33+
34+
# [START example]
35+
@app.route('/')
36+
def index():
37+
38+
# Set initial value if necessary
39+
if not redis_client.get('counter'):
40+
redis_client.set('counter', 0)
41+
42+
value = redis_client.incr('counter', 1)
43+
44+
return 'Value is {}'.format(value)
45+
# [END example]
46+
47+
48+
@app.errorhandler(500)
49+
def server_error(e):
50+
logging.exception('An error occurred during a request.')
51+
return """
52+
An internal error occurred: <pre>{}</pre>
53+
See logs for full stacktrace.
54+
""".format(e), 500
55+
56+
57+
if __name__ == '__main__':
58+
# This is used when running locally. Gunicorn is used to run the
59+
# application on Google App Engine. See entrypoint in app.yaml.
60+
app.run(host='127.0.0.1', port=8080, debug=True)

appengine/flexible/redis/main_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2015 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import main
16+
import pytest
17+
18+
19+
def test_index():
20+
try:
21+
main.redis_client.set('counter', 0)
22+
except Exception:
23+
pytest.skip('Redis is unavailable.')
24+
25+
main.app.testing = True
26+
client = main.app.test_client()
27+
28+
r = client.get('/')
29+
assert r.status_code == 200
30+
assert '1' in r.data.decode('utf-8')
31+
32+
r = client.get('/')
33+
assert r.status_code == 200
34+
assert '2' in r.data.decode('utf-8')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask==0.11.1
2+
gunicorn==19.6.0
3+
redis==2.10.5

0 commit comments

Comments
 (0)