14 August 2012

Google App Engine for static websites

Nowadays, there are many options to host a static website (portfolios, generated weblogs, front-end demos, etc.) for free. One of the best ways is to push your static files to a Github's repo and publish them through Github Page.

A lesser known solution is to make use of Google App Engine (GAE). Actually, GAE is designed for building and publishing high-performance, highly scalable web applications written in Python, Java or the newly-introduced Go language. However, by tinkering with the set up and deployment process, I figured out that it is also great for hosting static website with no server side coding at all. Like Github Page, your static website will be served through http://[your-app].appspot.com or you can customize it with a (sub)domain.

The advantages of GAE over Github Page are: higher static files quota (known in GAE as blobstore, known in Github as repository size); allowed SSL; better performance (since GAE app is hosted with reserved thread and bandwidth); possible to supplement data and dynamic server-side scripting later.


I won't explain details about setting up environment and deployment as you can easily follow the getting-started guides at: https://developers.google.com/appengine/

GAE for Python

As you go through the getting-started tutorial for Python, it becomes clear that GAE Python app won't serve your static files without specific instructions in the app.yaml file. However, I have successfully tweaked the app.yaml file to serve a whole static website without any Python handlers.

Let's take the helloworld app from the tutorial for reference. Here are what you need to do from there:
  • Remove all other files except app.yaml
  • Copy all static files into a subfolder. For example: helloworld/www
  • Edit the app.yaml file as following:
    application: my-app
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: yes
    # making index.html as default page for root
    - url: /
      static_files: www/index.html
      upload: www/index.html
    # begin any static files request from www folder
    - url: /
      static_dir: www
  • However this won't make any of index.html files in the subfolder as default pages. To have default page for subfolders, you have to prepend a static_files handler for each one:
    # default page for /projects subfolder, note the order in the handler list
    - url: /projects/?
      static_files: www/projects/index.html
      upload: www/projects/index.html
    # making index.html as default page for root
    - url: /
      static_files: www/index.html
      upload: www/index.html
    # begin any static files request from www folder
    - url: /
      static_dir: www
Then you can deploy your static website on GAE right away.

GAE for Java

For Java App Engine, it is much easier to prepare the application to deploy. All static files deployed with the application (even in the app root folder) will be served automatically. Best of all, index.html/.htm files are automatically default pages of root and subfolder URLs.

What you have to do:
  • Copy WEB-INF from appengine's demos/new_project_template to your website's folder
  • Edit appengine-web.xml with your application's name and version
  • Edit web.xml by removing the sample servlet & servlet-mapping tags.
  • Deploy your application with your website's folder in place of "war" in the command appcfg.sh update war from the instructions.

Where to go from here:

After successfully deployed your application on appspot.com you may want to customize a domain for your website and know your free quota limits.

There's one of the topics interests me but I have no time to experiment yet is serving you app with SSL. Please check it for yourself.

If you don't have a need to host website, GAE SDK is still useful as a localhost HTTP server to test front-end projects. Follow the links for how to use development server for Python & Java.

No comments:

Post a Comment