Monday, March 3, 2014

Google App Engine

This is an example of a simple PHP app using Google's App Engine from an windows7 box not previously used for development. You'd think that Google's tutorial would lay it out for you, but the steps required for setup were non-obvious to me. That's because I was accidentally following the Python tutorial. The PHP tutorial works much better.

Download Python 2.7.6, specifically Windows X86-64 MSI Installer (2.7.6). Run it and install to C:\GoogleAppEngine\Python27\, accepting all other defaults.

Download Google's PHP SDK for App Engine. Run it, accept the license and install to C:\GoogleAppEngine\PHPforAppEngine\, accepting all other defaults.

We'll skip the database install for now.

Simplest App

C:\GoogleAppEngine\Apps\helloworld\app.yaml

application: helloworld
version: 1
runtime: php
api_version: 1

handlers:
- url: /.*
  script: main.php

C:\GoogleAppEngine\Apps\helloworld\main.php

<?php
  echo 'Hello, World!';
?>

Launch it locally from the console:

C:\Temp>C:\GoogleAppEngine\PHPforAppEngine\dev_appserver.py C:\GoogleAppEngine\Apps\helloworld
Allow dev_appserver to check for updates on startup? (Y/n): y

That gave me some warnings, but sure enough, the app is now running at http://localhost:8080

Now With Static Files

C:\GoogleAppEngine\Apps\helloworld\app.yaml

application: helloworld
version: 1
runtime: php
api_version: 1

handlers:
- url: /static
  static_dir: static

- url: /.*
  script: main.php

C:\GoogleAppEngine\Apps\helloworld\static\styles.css

body {
 font-family: Verdana, Helvetica, sans-serif;
 background-color: #DDDDDD;
}

C:\GoogleAppEngine\Apps\helloworld\static\scripts.js

function dowork()
{
  var div = document.createElement('div');
  div.innerHTML = 'work';
  document.body.appendChild(div);
}

document.addEventListener('DOMContentLoaded', dowork);

C:\GoogleAppEngine\Apps\helloworld\main.php

<!DOCTYPE html>
<html>
<head>
 <meta name="viewport" content="width=device-width, initial-scale=1" />
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 <link rel="StyleSheet" href="/static/styles.css" type="text/css" />
 <script src="/static/scripts.js" type="text/javascript"></script>
 <title>Hello World</title>
</head>
<body>
<?php
  echo 'Hello, World!';
?>
</body>
</html>

Browse to http://localhost:8080 and CTRL+F5 to see the changes.

Deploy

Go to appengine.google.com, sign in, click "Create Application" and verify with SMS or voice call. Choose an application identifier that starts with a letter, like helloworld12345. For authentication, I chose "open to all google accounts users". Then create the application.

In your app.yaml, replace application: helloworld with the application identifier you just selected.

Deploy from the command line:

C:\Temp>C:\GoogleAppEngine\PHPforAppEngine\appcfg.py update C:\GoogleAppEngine\Apps\helloworld
...
Email: you@gmail.com
Password for you@gmail.com: ******
...
Deployment successful.

Your app is now available at yourappid.appspot.com

SQL

Google has another good tutorial for Using Google Cloud SQL with App Engine PHP SDK.

Sign into the Google Cloud Console, select your application identifier, select Cloud SQL in the sidebar. Ooops, stackoverflow confirms, there is no free quota for Google Cloud SQL.

No, problem, for a simple app, we'll just save everything to a flat file. Nope, gae-php-tips confirms, you cannot write to the file-system, you can use Google Cloud Storage instead, but that also isn't free.

web
{ "loggedin": false, "owner": false, "avatar": "", "render": "nothing", "trackingID": "UA-36983794-1", "description": "", "page": { "blogIds": [ 487 ] }, "domain": "holtstrom.com", "base": "\/michael", "url": "https:\/\/holtstrom.com\/michael\/", "frameworkFiles": "https:\/\/holtstrom.com\/michael\/_framework\/_files.4\/", "commonFiles": "https:\/\/holtstrom.com\/michael\/_common\/_files.3\/", "mediaFiles": "https:\/\/holtstrom.com\/michael\/media\/_files.3\/", "tmdbUrl": "http:\/\/www.themoviedb.org\/", "tmdbPoster": "http:\/\/image.tmdb.org\/t\/p\/w342" }