8000 Make multiple services sample more consistent with other samples · docent-net/python-docs-samples@779f816 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 779f816

Browse files
author
Jon Wayne Parrott
committed
Make multiple services sample more consistent with other samples
Change-Id: I2ecd3344e1c532de37a31e065b9b69c8349be0b2
1 parent 2e862fe commit 779f816

File tree

13 files changed

+66
-62
lines changed

13 files changed

+66
-62
lines changed

appengine/flexible/multiple_services/api_gateway/README.md renamed to appengine/flexible/multiple_services/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@ This example demonstrates how to deploy multiple python services to [App Engine
44

55
## To Run Locally
66

7-
1. You will need to install Python 3 on your local machine
7+
Use [virtualenv](https://virtualenv.pypa.io/en/stable/) to set up each
8+
service's environment and start the each service in a separate terminal.
89

9-
2. Install virtualenv
10-
```Bash
11-
$ pip install virtualenv
12-
```
10+
Open a terminal and start the first service:
1311

14-
3. To setup the environment in each server's directory:
1512
```Bash
13+
$ cd gateway-service
1614
$ virtualenv -p python3 env
1715
$ source env/bin/activate
1816
$ pip install -r requirements.txt
19-
$ deactivate
17+
$ python main.py
2018
```
2119

22-
4. To start server locally:
20+
In a separate terminal, start the second service:
21+
2322
```Bash
24-
$ python <filename>.py
23+
$ cd static-service
24+
$ virtualenv -p python3 env
25+
$ source env/bin/activate
26+
$ pip install -r requirements.txt
27+
$ python main.py
2528
```
2629

2730
## To Deploy to App Engine
@@ -33,16 +36,14 @@ separate App Engine service within the same project.
3336

3437
For the gateway:
3538

36-
[Gateway <default>](gateway/app.yaml)
39+
[Gateway service <default>](gateway/app.yaml)
3740

3841
This is the `default` service. There must be one (and not more). The deployed
3942
url will be `https://<your project id>.appspot.com`
4043

4144
For the static file server:
4245

43-
[Static File Server <static>](static/app.yaml)
44-
45-
Make sure the `entrypoint` line matches the filename of the server you want to deploy.
46+
[Static file service <static>](static/app.yaml)
4647

4748
The deployed url will be `https://<service name>-dot-<your project id>.appspot.com`
4849

appengine/flexible/multiple_services/api_gateway/gateway/requirements.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

appengine/flexible/multiple_services/api_gateway/static/requirements.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

appengine/flexible/multiple_services/api_gateway/gateway/app.yaml renamed to appengine/flexible/multiple_services/gateway-service/app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
service: default
22
runtime: python
33
vm: true
4-
entrypoint: gunicorn -b :$PORT api_gateway:app
4+
entrypoint: gunicorn -b :$PORT main:app
55

66
runtime_config:
77
python_version: 3

appengine/flexible/multiple_services/api_gateway/gateway/api_gateway.py renamed to appengine/flexible/multiple_services/gateway-service/main.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,46 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
15+
from flask import Flask
1616
import requests
1717
import services_config
1818

19-
app = services_config.make_app(__name__)
19+
app = Flask(__name__)
20+
services_config.init_app(app)
21+
2022

2123
@app.route('/')
2224
def root():
23-
'''Gets index.html from the static file server'''
25+
"""Gets index.html from the static file server"""
2426
res = requests.get(app.config['SERVICE_MAP']['static'])
2527
return res.content
2628

29+
2730
@app.route('/hello/<service>')
2831
def say_hello(service):
29-
'''Recieves requests from buttons on the front end and resopnds
30-
or sends request to the static file server'''
31-
#if 'gateway' is specified return immediate
32+
"""Recieves requests from buttons on the front end and resopnds
33+
or sends request to the static file server"""
34+
# If 'gateway' is specified return immediate
3235
if service == 'gateway':
3336
return 'Gateway says hello'
34-
#otherwise send request to service indicated by URL param
37+
38+
# Otherwise send request to service indicated by URL param
3539
responses = []
3640
url = app.config['SERVICE_MAP'][service]
3741
res = requests.get(url + '/hello')
3842
responses.append(res.content)
3943
return '\n'.encode().join(responses)
4044

45+
4146
@app.route('/<path>')
4247
def static_file(path):
43-
'''Gets static files required by index.html to static file server'''
48+
"""Gets static files required by index.html to static file server"""
4449
url = app.config['SERVICE_MAP']['static']
4550
res = requests.get(url + '/' + path)
4651
return res.content, 200, {'Content-Type': res.headers['Content-Type']}
4752

53+
4854
if __name__ == '__main__':
49-
port = os.environ.get('PORT') or 8000
50-
app.run(port=int(port))
55+
# This is used when running locally. Gunicorn is used to run the
56+
# application on Google App Engine. See entrypoint in app.yaml.
57+
app.run(host='127.0.0.1', port=8000, debug=True)
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+
requests==2.11.1

appengine/flexible/multiple_services/api_gateway/gateway/services_config.py renamed to appengine/flexible/multiple_services/gateway-service/services_config.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@
1313
# limitations under the License.
1414

1515
import os
16-
from flask import Flask
1716

18-
#to add services insert key value pair of the name of the service and
19-
#the port you want it to run on when running locally
17+
# To add services insert key value pair of the name of the service and
18+
# the port you want it to run on when running locally
2019
SERVICES = {
2120
'default': 8000,
2221
'static': 8001
2322
}
2423

25-
def make_app(name):
26-
app = Flask(name)
27-
environment = 'production' if os.environ.get(
28-
'GAE_INSTANCE', os.environ.get('GAE_MODULE_INSTANCE')
29-
) else 'development'
24+
25+
def init_app(app):
26+
# The GAE_INSTANCE environment variable will be set when deployed to GAE.
27+
gae_instance = os.environ.get(
28+
'GAE_INSTANCE', os.environ.get('GAE_MODULE_INSTANCE'))
29+
environment = 'production' if gae_instance is not None else 'development'
3030
app.config['SERVICE_MAP'] = map_services(environment)
31-
return app
31+
3232

3333
def map_services(environment):
34-
'''Generates a map of services to correct urls for running locally
35-
or when deployed'''
34+
"""Generates a map of services to correct urls for running locally
35+
or when deployed."""
3636
url_map = {}
3737
for service, local_port in SERVICES.items():
3838
if environment == 'production':
@@ -41,15 +41,17 @@ def map_services(environment):
4141
url_map[service] = local_url(local_port)
4242
return url_map
4343

44+
4445
def production_url(service_name):
45-
'''Generates url for a service when deployed to App Engine'''
46+
"""Generates url for a service when deployed to App Engine."""
4647
project_id = os.environ.get('GAE_LONG_APP_ID')
4748
project_url = '{}.appspot.com'.format(project_id)
4849
if service_name == 'default':
4950
return 'https://{}'.format(project_url)
5051
else:
5152
return 'https://{}-dot-{}'.format(service_name, project_url)
5253

54+
5355
def local_url(port):
54-
'''Generates url for a service when running locally'''
56+
"""Generates url for a service when running locally"""
5557
return 'http://localhost:{}'.format(str(port))
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
service: static
22
runtime: python
33
vm: true
4-
entrypoint: gunicorn -b :$PORT static_server:app
4+
entrypoint: gunicorn -b :$PORT main:app
55

66
runtime_config:
77
python_version: 3
8-
8+
99
manual_scaling:
1010
instances: 1

appengine/flexible/multiple_services/api_gateway/static/static_server.py renamed to appengine/flexible/multiple_services/static-service/main.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,35 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
from flask import Flask
1716

1817
app = Flask(__name__)
1918

19+
2020
@app.route('/hello')
2121
def say_hello():
22-
'''responds to request from frontend via gateway'''
22+
"""responds to request from frontend via gateway"""
2323
return 'Static File Server says hello!'
2424

25+
2526
@app.route('/')
2627
def root():
27-
'''serves index.html'''
28+
"""serves index.html"""
2829
return app.send_static_file('index.html')
2930

31+
3032
@app.route('/<path:path>')
3133
def static_file(path):
32-
'''serves static files required by index.html'''
34+
"""serves static files required by index.html"""
3335
mimetype = ''
3436
if path.split('.')[1] == 'css':
3537
mimetype = 'text/css'
3638
if path.split('.')[1] == 'js':
3739
mimetype = 'application/javascript'
3840
return app.send_static_file(path), 200, {'Content-Type': mimetype}
3941

40-
if __name__ == "__main__":
41-
port = os.environ.get('PORT') or 8001
42-
app.run(port=port)
42+
43+
if __name__ == "__main__":
44+
# This is used when running locally. Gunicorn is used to run the
45+
# application on Google App Engine. See entrypoint in app.yaml.
46+
app.run(host='127.0.0.1', port=8001, debug=True)
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+
requests==2.11.1

0 commit comments

Comments
 (0)
0