This is a very rudimentary application—I'll eventually turn it into a Giter8 template—that is intended to demonstrate how to get Scala/Spray/Akka to work nicely with Dart. Fortunately, it's not that hard to get started. You just need to be aware of a few things.
The key (and sticking point for me initially) is to use Spray's
getFromDirectory directive to serve up the directory in which your Dart application is housed (in this case
src/main/webapp). Once you've done that, you need to navigate to the following directory in Chromium (or another browser running the Dart VM):
The one other functioning REST endpoint is the
/api/tasks endpoint, which will simply deliver up a JSON array as a string. This string will be consumed by the Dart application and used to render a simple
<ul> containing three tasks.
First things first, make sure that you've installed SBT and Chromium. If so, all you need to do is run
sbt run and then navigate to the URL mentioned above in Chromium.
Things to Notice
- This application used Thomas Lockney's Spray/Akka example as an initial template.
- The application is Akka-driven at its very core, as you can see in the
- The main server—housed in
MainServer.scala—inherits from a more general
WebServicetrait. I think that defining such a
traitis a good way to ensure that the web services that you tie together—something that Spray makes very easy—function predictably.
- The various web services (here there is only one, but others can be added) are supervised by a
ServerSupervisoractor. Within this
Actor's definition, a variety of route schemes can be concatenated and run with one
- If you're working on the Dart application separately in the Dart Editor, you will constantly see errors like the following:
XMLHttpRequest cannot load http://localhost:3000/api/tasks. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:3030' is therefore not allowed access. Uncaught Error: Instance of '_XMLHttpRequestProgressEvent@0x3918afae' Exception: Instance of '_XMLHttpRequestProgressEvent@0x3918afae' undefined (undefined:0:0)This happens because the Dart Editor expects you to be using CORS and doesn't realize that the application is being served up by something else. If you know of a good way to overcome this issue, please let me know!