Friday, June 17, 2011

Once more into the breach.

This is my third Google App Engine project. The first was a toy, the second performs an essential (but simple) business function, and so I have an understanding of the basics.

A common question is, "Where to start?"

In this case, I am writing from the administration webpanel down, for a couple of reasons:
  1. A lot of features and capabilities are only accessible through the APIs. So custom code is the only good way of messing with things likes queues and data storage.
  2. Using the webpanel should be easy and fast, which means it needs the most work. Best to start now.  There's a straightforward path from rough debug lists to shiny rounded CSS layouts as you put in the hours of tuning and pixel pushing.
  3. I believe in "eat your own dogfood", which means using the tools myself duing development to accomplish my own debugging tasks. It forces me to show errors and diagnostic information in helpful ways in the web interface, rather than hiding it in log files.
  4. Done properly, it defines most of what becomes your system's public API. These days, most of what the web interface displays comes from structured AJAX calls back to the server to submit and aquire JSON strings. The difference between this and a well-structured public REST API is usually just the naming scheme.
  5. I already have a bunch of code from other projects (like my Ajax Dialog package) and experience with webpanels. They're not a problem.
  6. Nothing is nicer than a good webpanel. Sometimes they can even reach the level of "pretty", "shiny" or even "elegant". Users love that stuff.
This is the new cloud approach to software and their user interfaces. Web pages now instantiate thousands of lines of JavaScript before they show their first title banner, entire libraries of code like JQuery. And while Google has pushed the state of the art of browsers (which now run all this bloated crap with ease and aplomb) I am pained to admit that it was probably Facebook that made the conversion happen so quickly.

It's the old 'Killer App' story. Javascript has been around for years, but sadly neglected. Most people turned JavaScript off during the mid 2000's, when browsers were about as secure as liquorice suspenders. And they only turned it back on again last year to use Facebook and play Farmville. 

If only we had known.

So there is now a reliable and consistent layer of JavaScript (I refuse to call it ECMAScript) spread across the entire internet, like a thick layer of strawberry jam on whitebread. Sure there's still the occasional rough pip, bit that just proves it's natural.


This is supposed to be a blog about code, not jam, so let's see some. 

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
ServletOutputStream out = resp.getOutputStream();

// authenticate
boolean auth = authenticate();
if(auth) {
out.print("<html><head>\n");
out.print("<title>Administration</title>\n");
out.print("<!-- JavaScript -->\n");
out.print("<script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js\"></script>\n");
out.print("<script type=\"text/javascript\" src=\"/js/ipn-admin.js\"></script>\n");
out.print("<!-- Stylesheets -->\n");
out.print("<link href=\"/css/ipn-admin.css\" rel=\"stylesheet\" type=\"text/css\"></link>");
out.print("<!--[if lte IE 9]><link href=\"/css/ipn-admin.ie9.css\" rel=\"stylesheet\" type=\"text/css\"></link><![endif]-->");
out.print("</head><body>\n");
out.print("Welcome back "+this.user.getNickname()+"<br/>\n");

Ugh. I had to hand-format that. I'll have to find a better way if I want to start dumping vast blocks into here. Plus the colours aren't quite right, and the wrapping is awful, but you get the idea.

That's the top of most web pages generated from server scripts, right there. Load up JQuery, load up the local Javascript and CSS files (with the exceptions for IE) and then the page begins. All dependent on the user being authenticated, of course. I haven't even bothered to set a DOCTYPE yet, it works fine anyway.


All that is inside a Servlet, which is running in the App Engine. Hit the URL, and the page comes up. Lovely.

My next job after all that is to define my Java data objects, and create some basic AJAX scripts to manipulate them. Mostly GET scripts so I can test fresh code by just typing urls into a browser. This is trivial stuff when your backend database is a mySQL relational database server, but App Engine doesn't have that; it has Java Data Objects. 

JDO is actually better than SQL, especially in a massively distributed environment. But it sure takes some getting used to, and changes in your thinking. More on that soon.


No comments:

Post a Comment