Tuesday, March 26, 2013

My first live Dart + Go web app (lhj.me)

Last year I wrote a url shortener using Go and Riak. Trying to eat my own dog food since I wrote a REST API for Riak in Go. It used jQuery and just so I could get this up and running as quickly as possible. I put this up on lhj.me. Partly to test my libraries goriak and httphacks that I wrote in Go. It was a fun project! I was amazed how much I used goroutines in such a small app.I used this app just for personal use and experimentation. Basically just a dev instance and it crashed and died a week ago or so. So by by database and all.
This motivated me to bring her back up again instead of ec2 I thought I would try rackspace. Replace the javascript with Dart! Well technically javascript generated by Dart. I have been writing a lot of Coffeescript lately which I don't mind but I really want to find something with DEPENDENCY MANAGEMENT! So Dart it was to see what it is like to code in.
First I made some Golang code updates to make hosting the static content a bit easier in the future. Then it was writing Dart. I tried the Chromium that is distributed with Dart to make the coding faster. I wasn't able to get it working quickly. Since it was a simple ajax request I just compiled Dart to Javascript from the command line using Dart2js tool. The Dart code: http://lhj.me/dart/ushort.dart 
I have already written about some of my complaints about Dart for a intro presentation I did last summer. The presentation and code are here. The presentation was more of an intro to the language as a general purpose language. Major complaints I had was about concurrency and some confusing OO features but mostly I found it an improvement on Javascript. Since I complain about... well ... everything it was a pretty short list of complaints.
A language that requires or is a pain to use without an IDE like Java I tend to shy away from. I mean it is hard enough to write code but to be fighting complex builds systems or tools that make coding extremely slow makes me want to run away. Vim (my editor of choice) has a bundle for syntax highlights for Dart which I installed. The indention is not so good but I assume it will get better as Dart becomes more popular. If I build anything more complex than a page of code I plan to use the IDE. The most surprising thing to me was when I went to compile it with type checking it failed to import the html package! ERROR: "Do not know how to load 'dart:html'". I guess this makes sense but I was pretty shocked thinking the types I was using would get checked. The type checks are a bit sad (dart --enable_type_checks) since it is a long command line option you pass to the dart vm. Takes digging for in the documentation to find (it will not show up if you type dart --help).
Since I know both Java and Javascript the syntax seems very natural. The API could use some help. I found the HttpRequest documentation much to be desired. It took me a lot of trial and error to figure out how to do a POST with JSON. They added some useful things like a HttpRequest.get static method. I was surprised to find out there was no HttpRequest.post static method. Giving an intro screencast on how to build a Dart web app in 10 minutes 'get' save a lot of code however for the web apps I build I use POST as much if not more than GET.
From languages like Python, Ruby, Go or Coffeescript there seems to be a lot of typing. Compared to Java on the flip side it is a very terse language. About the same as Javascript maybe a small amount more if I want to compile with types.