8000 Add flex redis sample (#408) · kler/python-docs-samples@bb6b63b · GitHub
[go: up one dir, main page]

Skip to content

Commit bb6b63b

Browse files
author
Jon Wayne Parrott
authored
Add flex redis sample (GoogleCloudPlatform#408)
* Making nox ignore env and lib dirs. * Adding redis flexible sample. * Adding redis to travis
1 parent 48a09ca commit bb6b63b

File tree

6 files changed

+116
-1
lines changed

6 files changed

+116
-1
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ language: python
33
services:
44
- memcached
55
- mysql
6+
- redis-server
67
branches:
78
only:
89
- master

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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
# Set initial value if necessary
38+
if not redis_client.get('counter'):
39+
redis_client.set('counter', 0)
40+
41+
value = redis_client.incr('counter', 1)
42+
43+
return 'Value is {}'.format(value)
44+
# [END example]
45+
46+
47+
@app.errorhandler(500)
48+
def server_error(e):
49+
logging.exception('An error occurred during a request.')
50+
return """
51+
An internal error occurred: <pre>{}</pre>
52+
See logs for full stacktrace.
53+
""".format(e), 500
54+
55+
56+
if __name__ == '__main__':
57+
# This is used when running locally. Gunicorn is used to run the
58+
# application on Google App Engine. See entrypoint in app.yaml.
59+
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

nox.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,14 @@ def run_tests_in_sesssion(
137137
for reqfile in list_files(dirname, 'requirements*.txt'):
138138
session.install('-r', reqfile)
139139

140+
# Ignore lib and env directories
141+
ignore_args = [
142+
'--ignore', os.path.join(sample, 'lib'),
143+
'--ignore', os.path.join(sample, 'env')]
144+
140145
session.run(
141146
'py.test', sample,
142-
*pytest_args,
147+
*(pytest_args + ignore_args),
143148
success_codes=[0, 5]) # Treat no test collected as success.
144149

145150

0 commit comments

Comments
 (0)
0