- Building Google Cloud Platform Solutions
- Ted Hunter Steven Porter Legorie Rajan PS
- 475字
- 2021-07-02 12:24:39
Getting started
To get started with the App Engine standard environment, let's build a simple Python application. The source code for this exercise is in chapter_02/example_01 of this book's Git repository. In this directory, you'll find a few files:
- The app.yaml file is as follows:
runtime: python27
api_version: 1
threadsafe: true
default_expiration: "30s"
env_variables:
COLORS_URL: https://colors-dot-{}/colors
handlers:
- url: /admin/.*
script: main.app
login: admin
- url: /api/.*
script: main.app
- url: /
static_files: index.html
upload: index.html
The app.yaml configuration file informs App Engine how a given service should be managed. At a minimum, a configuration file will define the application runtime, the API version, and one or more handlers. The preceding example defines these values, along with a few others:
-
- runtime: python27 declares the App Engine runtime to be used
- api_version: 1 specifies the App Engine APIs version to use
- threadsafe: true enables concurrent requests
- static_dir: static specifies that requests to /static/* will be served as static content from the ./static directory
- env_variables gives key-value pairs that will be provided to our application as environment variables
- script: main.app specifies the route handler for all URLs other than /static
To serve the menu, we make a static index.html available in the /static directory, along with client-side scripts stylesheets. We'll provide the menu data at runtime through a GET request to /api/menu. Because this route matches /*, it will be directed to our main.app handler. The script provided is in Python module format (module.variable), so this handler will use WSGI. We'll be using the popular Flask framework to handle this request and return the menu data.
- The main.py is as follows:
from flask import Flask
...
base_url = os.getenv('DEFAULT_VERSION_HOSTNAME')
colors_url = os.getenv('COLORS_URL').format(base_url)
...
app = Flask(__name__)
...
@app.route('/api/colors')
def get_colors():
try:
colors = urllib2.urlopen(colors_url)
return colors.read()
except:
return 'Failed to fetch colors', 500
In the preceding code (paraphrased for brevity), we created a Flask application called app. Notice that this relates to the handler defined in app.yaml. When App Engine creates handlers for our service, it will associate script: main.app with the app variable in main.py. We go on to a Flask route annotated with @app.route(‘/api/colors'). App Engine will hand off requests to /api/* to our Flask application, which will in turn use this annotation to invoke the get_colors() method. This behavior is fairly similar across WSGI frameworks.
In this example, our application will attempt to fulfill requests to /app/colors by delegating requests to another App Engine service, colors. We construct the URL to this service using values provided by App Engine as a simple form of service discovery. This oversimplified example serves to show the microservice approach to designing App Engine applications; we'll define the colors service in just a bit. The end result of our architecture will look something like this:
