My first Dancer webapp
Dancer is YAPWF (Yet Another Perl Web Framework), in the same bucket as Catalyst. To date I’ve done most of my webapp development on Catalyst, but at the last German Perl Workshop @bano99 introduced me to Dancer, so I wanted to give it a try.
This is the webapp which resulted.
The backend
Behind the scenes I have a home built micro-controller web server controlling the temperature in a refrigerator to bring our turtles into a healthy winter hibernation. A perl script regularly (once per minute) queries the current status from the micro-controller, and logs the data (temperature in the tank, the refrigerator, room, humidity, and switch state) to a (SQLite) database. So, I have some interesting data, and want to make that web accessible – how well suited is Dancer to the task?
Turns out: very suitable!
Dancer is…
- super quick and easy to get started with: 2 minutes tops to get a basic web application running, including installing Dancer with cpanplus.
- very intuitive for the basic stuff – in my case I had a template for the webpage, a database handler to get the data, and a method to do some basic analysis and pre-processing on the data before returning it to the client as json.
- a bit procedural… it is possible to develop an OOP app with Dancer, but with its background singleton and plethora of new syntax it feels like, and encourages procedural practices.
Dancer is not (IMHO)…
- a framework which will force/cajole you into using good design practices (OOP, MVC). You can be quick’n’dirty with Dancer, which depending on your standpoint can be a pro or a con.
- easy to understand the inner workings. You may hate the global $c in Catalyst to which everything is bound, but … you do know what to bind stuff to, and you do know where to find it. In Dancer there is (even) more hidden magic.
- for special wishes. For example for something as simple as having “Expires” http headers for static files, I had to add a after_file_render hook.
Better, Faster and Cooler
Some other stuff I did on the way to make it BFC:
- Used jqPlot for the graphs. I really like it! I used to be into OFC, but jqPlot is faster, more versatile, and the big kicker: doesn’t require Flash! Try out the zooming and mouse-over effects!
- Data compression: originally the data for the charts weighed in at over 2MB…
- Reduce repetition: for the graphs, the data requires an array-of-arrays with each value paired with the relevant date. Rather than transmitting the 5 finished array-of-arrays (each with its own set of date data), I now transmit just the 4 data arrays, and the date array, and then build them as jqPlot requires them with javascript clientside. This reduced the transmitted data from 2MB to ~400KB.
- Deflate the data – by using Plack::Middleware::Deflater I was able to further reduce the transmitted data to ~40KB
- Using Plack and Starman to easily put the webapp online. Wow… that makes the transition from development on localhost to full deployment serverside childsplay!
- Using Varnish to cache the pages server-side. Some people make the effort of serving the static elements of a web app via a light web server, and while this has its merits, I prefer a contiguous web app which is complete in itself, and by applying the appropriate Expires http headers, let a reverse proxy do the grunt work. I set the headers for this webapp so the worst-case load on the back-end will only be 1 request every 7 Days for any of the javascript/css files, 1 per hour for the html page, and 1 per minute for the json data. Go ahead and slashdot me… I think I can take it, even though there’s only 3 worker processes and it’s running off a SQLite database. 🙂
Summary
I will definitely be using Dancer again. For any corporate and larger web applications I’d still prefer Catalyst, but for applications like this where I want to put something small online quickly, it’s the bees knees!
You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.
January 23rd, 2012 at 07:29
[…] My first Dancer webapp […]