Google App Engine is a complete development stack that uses familiar technologies to build and host applications on the same infrastructure used at Google.
Python
Java
Go
PHP
Google App Engine is a complete development stack that uses familiar technologies to build and host applications on the same infrastructure used at Google.
Secure environment that provides limited access to the underlying operating system.
The sandbox allows App Engine to
Three options for data storage
App Engine Datastore a NoSQL schemaless data storage built in GAE
Google Cloud SQL a relational SQL database service based on MySQL
Google Cloud Storage file based storage
Go to https://developers.google.com/appengine/downloads and grab the Python installer.
You should also have Python by now, but in case you do not, grab it from here. (Your platform might offer smarter ways to install it.)
All configuration settings are stored in a file called app.yml
application: example version: 1 runtime: python27 api_version: 1 threadsafe: yes handlers: - url: .* script: main.app libraries: - name: webapp2 version: "2.5.2"
App Engine supports webapp2 by default:
import webapp2 class MainHandler(webapp2.RequestHandler): def get(self): name = self.request.get('name') self.response.write('Hello %s!' % name) app = webapp2.WSGIApplication([ ('/', MainHandler) ], debug=True)
In addition to webapp2, the environment includes WebOb and Django.
google.appengine.ext.db
google.appengine.ext.ndb
Creating an entity type is as easy as creating a Python class.
from google.appengine.ext import ndb class Contact(ndb.Model): name = ndb.StringProperty() email = ndb.StringProperty() birth_date = ndb.DateProperty()
The Datastore API is Object Oriented.
from google.appengine.ext import ndb def StoresByCity(city, limit): query = Store.query(Store.city == city).order(Store.name) return query.fetch(limit, projection=[Store.name, Store.address])
You can write queries with GQL, a SQL-like language.
from google.appengine.ext import ndb qry = ndb.gql("SELECT * FROM Account WHERE balance < :1", 100)
GQL is translated to NDB's native query API. (This is the opposite of what traditional ORM libraries do!)
If you use webapp2, Django templates are supported by default.
<html> <body> {% for contact in contacts %} {% if contact.name %} <b>{{ contact.name }}</b> {% else %} Unnamed contact {% endif %} <{{ contact.email }}> {% endfor %} <a href="{{ url }}">{{ url_linktext }}</a> </body> </html>
Alternatively, App Engine ships with jinja2, too.
Here is how to render a template in with webapp2:
def render_template(self, view_filename, params=None): if not params: params = {} path = os.path.join(os.path.dirname(__file__), 'views', view_filename) self.response.out.write(template.render(path, params))
Memcache is a high-performance, distributed memory object caching system.
from google.appengine.api import memcache def get_data(): data = memcache.get('key') if data is not None: return data else: data = self.query_for_data() memcache.add('key', data, 60) return data
Can be used to minimize hits to the Datastore or to save transient state information.
channel
directory of the package you downloaded earlier and implement the missing methods.http://localhost:$APP_PORT/channel
to test your server.A channel connects two ends:
There is a service for that! Enable presence in your app.yaml
inbound_services: - channel_presence
and implement two handlers that can respond to POST
at
/_ah/channel/connected/
/_ah/channel/disconnected/
Requires action on both ends. The server sends a token to the client:
def get(self): client_id = uuid.uuid4().hex token = channel.create_channel(client_id) template_values = {'client': client_id, 'token': token} self._render_template(template_values)
The client uses the token to create a channel
var client = "{{ client }}"; var channel = new goog.appengine.Channel('{{ token }}'); function setUpChannel() { var socket = channel.open(); socket.onmessage = onMessage; }
The client can only read messages from the client.
function onMessage(message) { data = $.parseJSON(message.data); /* do something with data */ }
It can send data to the server via HTTP requests:
function sendCoordinates() {$.post('/channel', { x: current_x, y: current_y, client: client }); }
endpoints
directory of the package you downloaded earlier and implement the missing methods.http://localhost:$APP_PORT/endpoints
to test your server.Google Cloud Endpoints consists of tools, libraries and capabilities that allow you to generate APIs and client libraries from an App Engine application.
Annotate the API implementation class with the endpoints decorators,
@endpoints.api(name='myapi', version='v1', description='My New API') class MyNewApi(remote.Service): ...
create an API server instance,
application = endpoints.api_server([MyNewApi], restricted=False)
and map the instance to the API URL
handlers: # Endpoints handler - url: /_ah/spi/.* script: services.application
Annotate methods with
@endpoints.method(RequestMessageClass, ResponseMessageClass, name='foo.bar', path='/bar', http_method='GET', ...) def bar(self, request): ...
RequestMessageClass
and ResponseMessageClass
are ProtoRPC definitions of the request and response classes,path
is the URL path of the endpoint,http_method
allows to map different functions to different HTTP methodshttps://your_app_id.appspot.com/_ah/api/discovery/v1/apis
https://your_app_id.appspot.com/_ah/api/explorer