Introduction to App Engine Development

25
App Engine Development Ron Reiter, 2012

Transcript of Introduction to App Engine Development

Page 1: Introduction to App Engine Development

App Engine DevelopmentRon Reiter, 2012

Page 2: Introduction to App Engine Development

Agenda● What is Google App Engine● Advantages● Disadvantages● Creating your first webapp● Deployment● Features● Reducing Cost

Page 3: Introduction to App Engine Development

What is GAE● Google App Engine is a Platform-as-a-

service server for web applications● Similar to Heroku, but writing atop it

requires using specific Google APIs● Supports Python, Java and Go

Page 4: Introduction to App Engine Development

Advantages● Scales automatically● Bills by services used● Easy to deploy● Extremely high uptime of all services (web

server, database, cronjobs, mail delivery, etc)

● Free development tier

Page 5: Introduction to App Engine Development

Disadvantages● Cannot use native libraries with Python● Cannot use different runtime

environments, such as PyPy (yet).● Constrained to Google because code is

Google specific● 30 second limit per request● No websockets for now● High price because of PaaS

Page 6: Introduction to App Engine Development

Your first Web Application● Download the Google App Engine Python

SDK● Add a new application● Click "Run"● Go to

http://localhost:8080● And you're done!

Page 7: Introduction to App Engine Development

Your first Web Application● The code example contains two important

files:○ main.py - the web server code example○ app.yaml - the web application configuration

● It also contains a favicon example and a file called index.yaml, which you don't need to touch right now.

Page 8: Introduction to App Engine Development

app.yaml● The default app.yaml configuration is to

forward all requests to main.py● We will need to add a static files path, if we

want App Engine to serve some static files.

- url: /static

static_dir: static

Page 9: Introduction to App Engine Development

Deployment● Go to http://appengine.google.com● If you haven't yet verified your account

using your mobile phone, please do so● Add a new application and find an

available identifier for it● Once you've created it, change your app.

yaml file to use the same identifier● Using your Google credentials, Use the

App Engine launcher to deploy

Page 10: Introduction to App Engine Development

Deployment (cont.)● After deploying, use the GAE dashboard

for datastore access, logs, quota details, load, and more.

Page 11: Introduction to App Engine Development

main.pyimport webapp2

class MainHandler(webapp2.RequestHandler):

def get(self):

self.response.write("Hello world!")

app = webapp2.WSGIApplication([

('/', MainHandler)

], debug=True)

Page 12: Introduction to App Engine Development

Templatesfrom google.appengine.ext.webapp import templateimport webapp2

class MainHandler(webapp2.RequestHandler):

def get(self):

self.response.write(template.render("index.html", {

"message" : "Hello, World!"

})

app = webapp2.WSGIApplication([

('/', MainHandler)

], debug=True)

Page 13: Introduction to App Engine Development

GET/POST Parametersfrom google.appengine.ext.webapp import templateimport webapp2

class MainHandler(webapp2.RequestHandler):

def get(self):

self.response.write(template.render("index.html", {

"message" : self.request.get("message")

})

app = webapp2.WSGIApplication([

('/', MainHandler)

], debug=True)

Page 14: Introduction to App Engine Development

URL Parametersfrom google.appengine.ext.webapp import templateimport webapp2

class MainHandler(webapp2.RequestHandler):

def get(self, message):

self.response.write(template.render("index.html", {

"message" : message

})

app = webapp2.WSGIApplication([

('/(\w+)', MainHandler)

], debug=True)

Page 15: Introduction to App Engine Development

● NoSQL Database (BigTable)● Supports an SQL-like syntax called GQL

○ GqlQuery("SELECT * FROM Song WHERE composer = :composer", composer="Lennon, John")

● Scales Automatically● Django-like ORM (db.Model)● Map Reduce API● Expando - schema-less objects

Datastore API

Page 16: Introduction to App Engine Development

The Model Classfrom google.appengine.ext import db

class Post(db.Model):

name = db.StringProperty()

body = db.TextProperty()

created = db.DateTimeProperty(auto_now_add=True)

# create a new entry

post = Post(

name = "Ron Reiter", body = "This is a guestbook entry!")

# save it to the databasepost.put()

Page 17: Introduction to App Engine Development

The Model Class (cont.)# get the post keypost_id = post.key().id()

# using the class method to fetch the object using the IDpost = Post.get_by_id(post_id)

# update the entrypost.body = "This is the updated body"post.put()

Page 18: Introduction to App Engine Development

● An alternative to the Datastore API● Document oriented instead of column

oriented● Schema-less● Can run map reduce queries on structured

properties, like MongoDB

NDB API

Page 19: Introduction to App Engine Development

class Address(ndb.Model):

type = ndb.StringProperty() # E.g., 'home', 'work'

street = ndb.StringProperty()

city = ndb.StringProperty()

class Contact(ndb.Model):

name = ndb.StringProperty()

addresses = ndb.StructuredProperty(Address, repeated=True)

guido = Contact(name='Guido',

addresses=[Address(type='home',

city='Amsterdam'),

Address(type='work',

street='Spear St',

city='SF')])

guido.put()

NDB API (cont.)

Page 20: Introduction to App Engine Development

● Cloud Storage is similar to Amazon S3● Integrates very well with App Engine

with files.open('/gs/mybucket/myobject/', 'r') as f:

data = f.read(1)

while data != "":

print data

data = f.read(1)

print 'Done reading file!'

Cloud Storage API

Page 21: Introduction to App Engine Development

Python Service APIs● BlobStore API - Allows saving and

retrieving binary data from the datastore● Channel API - Gives server push

capabilities using long polling● Memcache API - fast key/value store● LogService API - Convenient logging● Mail API - Simple email delivery● Inbound mail handler - Use request

handlers to handle emails sent to the app

Page 22: Introduction to App Engine Development

Python Service APIs● Search API - Powerful datastore search● Task Queues API - Allows queueing

request handlers to process data in the background

● Cron jobs - Allows scheduling request handlers to be executed

● Image API - Simple image processing module, based on PIL

Page 23: Introduction to App Engine Development

Python Service APIs● URLFetch API - Alternative to urllib, use it

instead, also with asynchronous bindings● Users API - Easy integration for user

authentication using Google accounts● And more...

Page 24: Introduction to App Engine Development

Reducing Cost of GAE App● Use Go (compiled) instead of Python for

CPU intensive applications● Use Asynchronous APIs to prevent

spawning new handlers for no reason● Use memcache whenever possible● Reduce DataStore calls● Use cache headers

Page 25: Introduction to App Engine Development

Questions?Thank You!