Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 603bfc0

Browse filesBrowse files
authored
Add samples for Redis Labs redis for standard py37 (GoogleCloudPlatform#1628)
1 parent 2968c74 commit 603bfc0
Copy full SHA for 603bfc0

File tree

Expand file treeCollapse file tree

4 files changed

+86
-0
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+86
-0
lines changed
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
runtime: python37
2+
3+
env_variables:
4+
REDIS_HOST: your-redis-host
5+
REDIS_PORT: your-redis-port
6+
REDIS_PASSWORD: your-redis-password
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2018 Google LLC.
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 os
16+
17+
from flask import Flask
18+
import redis
19+
20+
21+
app = Flask(__name__)
22+
23+
24+
# [START gae_py37_redis_client]
25+
redis_host = os.environ.get('REDIS_HOST', 'localhost')
26+
redis_port = int(os.environ.get('REDIS_PORT', 6379))
27+
redis_password = os.environ.get('REDIS_PASSWORD', None)
28+
redis_client = redis.StrictRedis(
29+
host=redis_host, port=redis_port, password=redis_password)
30+
# [END gae_py37_redis_client]
31+
32+
33+
# [START gae_py37_redis_example]
34+
@app.route('/')
35+
def index():
36+
value = redis_client.incr('counter', 1)
37+
return 'Value is {}'.format(value)
38+
# [END gae_py37_redis_example]
39+
40+
41+
if __name__ == '__main__':
42+
# This is used when running locally.
43+
app.run(host='127.0.0.1', port=8080, debug=True)
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2018 Google LLC.
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 pytest
16+
17+
import main
18+
19+
20+
def test_index():
21+
try:
22+
main.redis_client.set('counter', 0)
23+
except Exception:
24+
pytest.skip('Redis is unavailable.')
25+
26+
main.app.testing = True
27+
client = main.app.test_client()
28+
29+
r = client.get('/')
30+
assert r.status_code == 200
31+
assert '1' in r.data.decode('utf-8')
32+
33+
r = client.get('/')
34+
assert r.status_code == 200
35+
assert '2' in r.data.decode('utf-8')
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask==1.0.2
2+
redis==2.10.6

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.