Friday, 20 June 2014

Ninja Web Framework: Hello World

In a little bit of spare time this week I've been trying out a new thing. We've been using the Play framework in one project, with some success. However that bit was led by someone else and while I had seen the basics I couldn't really claim to have used it myself. It was time for me to explore it a bit more deeply for something new, rather than straight servlets on Tomcat. However, it seemed to be hard work:
Java isn't first choice for Play. That's OK, but I wanted to use Java.
I have a mild preference for NetBeans, partly because I find the UI more intuitive, partly because I find getting NetBeans and Jenkins to use the same build file easier than in eclipse. NetBeans has some Scala support but doesn't fully integrate with Play.
Finally, working in an environment where I run a VM on my laptop for development testing is a bit clunky in Play. (I have grown bored of untangling the software installs for a mix of projects at the MacOS level. New project, new local linux VM.) Play just seemed to be more involved in the writing step than felt natural to me - I like to be able to pull my code back from the infrastructure.
The "hello world" step is often the hardest with a new tool, but this was clunky enough for me to look around.

I soon came across the Ninja Web Framework. Their selling points are: simple to program in Java web framework; easy to use an IDE; works with testing and CI; talks JSON and html; built-in authentication and email. Particularly attractive was the model of development which doesn't need a lot of server setup, with Maven doing all the package management beyond Java and Maven itself. The "works with testing" point merits expanding a little. In the configuration there is a built in notion that you might want different configurations for development, test and production environments. I can see that making life easier.

There was quite a lot of package management for maven to do - it spent a while being busy when creating the project and again when first building / running. Checking back with mvn -o dependency:list reveals 116 packages from a variety of sources. This complex set of dependencies worries me slightly. Projects which die or become radically different will have a knock on effect. The rate of regression testing that might arise as changes happen will make good configuration management a must. And because if any of the imports gets used again in our / some other code on the same server the opportunity for evolving to run multiple versions of one 3rd party package isn't pretty. But I'll set this aside for now and see how it goes.

As I said above, "hello world" is often the hardest step with something designed to be very general. The overhead over the simple approach is wince making. However, the documentation stepped me through the basics and I soon had a web page up. Then I changed the text. Then I added a page. Then I spoofed a session. And so on, little tweaks to learn my way around. Commenting out some HTML broke it for a while, but mostly it did what I expected! And in the end the structure is pretty logical and not over-complex, where over-complex is defined as "requires more than one page in my notebook":


Routes map URLs (optionally with regular expressions and variable parts) to controller methods.
Controllers can call other code and build a response. There are also lots of annotations, for filters, brining in config file parameters, parsing JSON, setting session cookie data etc.
The view is achieved using Freemarker templates. These can import other template files, making consistent headers and footers easy. They work with i18n properties files. The rendering of the result in the controller can pass in parameters which are picked up in the template.

Returning to the issues I had in the first paragraph:

Java just works.
NetBeans (and other IDEs) just work.
The IDE issue is at least in part fixed because of the looser coupling between code and a running Ninja environment. 

So, I haven't done anything very clever with it yet. But I think I know where I'll try using it on a proper project soon. I'll report back when I've got a fuller picture!


No comments:

Post a comment