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 830d363

Browse filesBrowse files
nareddytleahecoleDoug Mahugh
authored
endpoints: Modify gRPC bookstore to support gRPC on Cloud Run (GoogleCloudPlatform#2764)
* endpoints: Modify gRPC bookstore to support gRPC on Cloud Run - Allow client to make TLS calls - Server determines port from env var Signed-off-by: Teju Nareddy <nareddyt@google.com> * Modify dockerfile, move roots.pem Signed-off-by: Teju Nareddy <nareddyt@google.com> Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Co-authored-by: Doug Mahugh <dmahugh@google.com>
1 parent 96a08de commit 830d363
Copy full SHA for 830d363

File tree

Expand file treeCollapse file tree

5 files changed

+4677
-7
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+4677
-7
lines changed

‎endpoints/README.md

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Google Cloud Endpoints Examples
2+
3+
The files in this directory are referenced from [Google Cloud Endpoints Documentation](https://cloud.google.com/endpoints/docs/).
4+
5+
### CA File
6+
7+
`roots.pem` is copied from [github.com/grpc/grpc](https://github.com/grpc/grpc/blob/master/etc/roots.pem).
8+
This file should be updated regularly.

‎endpoints/bookstore-grpc/Dockerfile

Copy file name to clipboardExpand all lines: endpoints/bookstore-grpc/Dockerfile
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@ ADD . /bookstore/
1717
WORKDIR /bookstore
1818
RUN pip install -r requirements.txt
1919

20-
EXPOSE 8000
21-
2220
ENTRYPOINT ["python", "/bookstore/bookstore_server.py"]

‎endpoints/bookstore-grpc/bookstore_client.py

Copy file name to clipboardExpand all lines: endpoints/bookstore-grpc/bookstore_client.py
+11-3Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@
2222
import bookstore_pb2_grpc
2323

2424

25-
def run(host, port, api_key, auth_token, timeout):
25+
def run(host, port, api_key, auth_token, timeout, use_tls):
2626
"""Makes a basic ListShelves call against a gRPC Bookstore server."""
2727

28-
channel = grpc.insecure_channel('{}:{}'.format(host, port))
28+
if use_tls:
29+
with open('../roots.pem', 'rb') as f:
30+
creds = grpc.ssl_channel_credentials(f.read())
31+
channel = grpc.secure_channel('{}:{}'.format(host, port), creds)
32+
else:
33+
channel = grpc.insecure_channel('{}:{}'.format(host, port))
2934

3035
stub = bookstore_pb2_grpc.BookstoreStub(channel)
3136
metadata = []
@@ -52,5 +57,8 @@ def run(host, port, api_key, auth_token, timeout):
5257
parser.add_argument(
5358
'--auth_token', default=None,
5459
help='The JWT auth token to use for the call')
60+
parser.add_argument(
61+
'--use_tls', type=bool, default=False,
62+
help='Enable when the server requires TLS')
5563
args = parser.parse_args()
56-
run(args.host, args.port, args.api_key, args.auth_token, args.timeout)
64+
run(args.host, args.port, args.api_key, args.auth_token, args.timeout, args.use_tls)

‎endpoints/bookstore-grpc/bookstore_server.py

Copy file name to clipboardExpand all lines: endpoints/bookstore-grpc/bookstore_server.py
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import argparse
1818
from concurrent import futures
1919
import time
20+
import os
2021

2122
from google.protobuf import struct_pb2
2223
import grpc
@@ -109,6 +110,8 @@ def serve(port, shutdown_grace_duration):
109110
server.add_insecure_port('[::]:{}'.format(port))
110111
server.start()
111112

113+
print('Listening on port {}'.format(port))
114+
112115
try:
113116
while True:
114117
time.sleep(_ONE_DAY_IN_SECONDS)
@@ -121,11 +124,20 @@ def serve(port, shutdown_grace_duration):
121124
description=__doc__,
122125
formatter_class=argparse.RawDescriptionHelpFormatter)
123126
parser.add_argument(
124-
'--port', type=int, default=8000, help='The port to listen on')
127+
'--port', type=int, default=None,
128+
help='The port to listen on.'
129+
'If arg is not set, will listen on the $PORT env var.'
130+
'If env var is empty, defaults to 8000.')
125131
parser.add_argument(
126132
'--shutdown_grace_duration', type=int, default=5,
127133
help='The shutdown grace duration, in seconds')
128134

129135
args = parser.parse_args()
130136

131-
serve(args.port, args.shutdown_grace_duration)
137+
port = args.port
138+
if not port:
139+
port = os.environ.get('PORT')
140+
if not port:
141+
port = 8000
142+
143+
serve(port, args.shutdown_grace_duration)

0 commit comments

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