Posts Tagged ‘grails’

Bootstrapping, An Environmentalist's Perspective

Monday, September 28th, 2009 by admin

Grails gives you a handy little class called Bootstrap.groovy.  As it’s name implies this class allows you to “bootstrap” your application at startup (and shutdown).  This is pretty handy in development when you want your application to start with some sample data loaded.  For example, you might use Bootstrap.groovy to create a bunch of test user accounts (that’s what we do for Sierra).  As you start to move your application into production you might find yourself commenting out lines of code (I did) in Bootstrap.groovy so that the production version of your application does not launch with all of your sample data loaded.

This week as I was working on some administrative functions for Sierra I found myself thinking there had to be a better way, and it turns out there is.  As I’ve discussed before when Grails launches it knows what mode/environment it’s running in.  The default environments are “development”, “test”, and “production”.  The Grails configuration scripts Config.groovy, DataSource.groovy, etc… give you handy “environments” blocks, but what about classes such as Bootstrap.groovy.  What should we do there?  The answer is the grails.util.Environment class.  This class knows what the current running environment is.  If we use this in Bootstrap.groovy we now have a way to only load our sample data if we are in “development” mode.  This is how it looks:

if (Environment.getCurrent() == Environment.DEVELOPMENT) {
    // Insert sample data here
}

Tags: ,
Posted in SaffronSierra | No Comments »

Grails, The Good and The Bad

Monday, September 21st, 2009 by admin

As I’ve mentioned here before we’ve been using Grails to build the web front-end for Sierra as well as the Sierra REST API.  This week has provided a couple of great examples of the joys, and pains, of using open source software.  Let me start by saying that using Grails is mostly all joy, and not much pain.

While working on the administration console for Sierra this week I ran into a case where I needed to paginate a set of results.  Having built web applications for years now I have some pretty tried and true methods for doing this.  However, in the past while using other frameworks these methods have involved me writing a decent chunk of code.  With Grails that chunk of code is no longer needed.  Or, a least I don’t have to write it.  Let me show you a simple example.  Let’s say we have a “show” method on a UsersController that either fetches a single user (if we pass in an id), or returns a list of all users.  It might look something like this:

def show = {
    if (params.id && User.exists(params.id.toLong())) {
        def user = User.get(params.id)
        [user: user]
    } else {
        // default max num for the page to 10 if not specified
        if (!params.max) {
            params.max = 10
        }
        render view: "list", model: [userList: User.list(params)]
    }
}

Notice the call to “User.list(params)”.  This is passing in the URL parameter coming from the client into call to fetch the users.  If those parameters contain paging information then the list() method will fetch the proper subset of users.  I didn’t have to code the list() method.  Grails just gives it to me.
(more…)

Tags: , , ,
Posted in SaffronSierra | 1 Comment »

REST, jQuery, JSONP, & Grails

Monday, September 14th, 2009 by admin

As we started building the REST API for Saffron Sierra we wanted an easy way to test it from within a web browser.  REST APIs run on top of HTTP so this makes pretty good sense.  At first we used the REST Client plugin for Firefox, which was great, but as we added authentication and other things to the API using the REST Client plugin began to be a little cumbersome.  I started toying with the idea of putting together a very basic test harness using HTML and Javascript.  The hope was that it would provide a good testing tool, but also give future users of Sierra a basic tool to use and learn from.

I had been wanting to use and learn jQuery for quite some time.  We (Saffron Technology) had used Prototype in the past for a lot of our applications.  I noticed a while back that there seemed to be a lot of buzz surrounding jQuery and wanted to see what it was all about.  The hope was that jQuery would provide a quick an easy way to consume the JSON responses from the Sierra REST APIS.  It well exceeded my expectations in this department.

The development of the first few pages and API calls went very smoothly and things looked great while testing on my local development workstation.  However, as I tried to use the test harness to access remote Sierra instances I noticed I wasn’t getting the responses I would expect, in fact, I wasn’t getting responses at all.  It turns out this was due to web browsers enforcing tight security when it comes to calling other domains/sites from the domain on which you’re currently running.  This issue is well documented in this wikipedia article.
(more…)

Tags: , , , , , ,
Posted in SaffronSierra | 1 Comment »

Using Grails for SaaS Deployment

Friday, September 4th, 2009 by admin

First, a little history…

About 6 months ago we began using Grails for our new and ever growing set of REST apis.  Overall our experience has been great.  Grails has really allowed us to accelerate development and focus on the things that are important to us.  Naturally, when we started talking about Sierra and knew that we’d need to build a user dashboard, admin console, etc… we turned to Grails.

Grails is implemented using Groovy, which is a scripting language that runs in the Java JVM.  Seeing as the rest of the Saffron stack uses Java this was another strong selling point for us, but it also means that in order to stand up Sierra we’d have to jump through all of the same hoops that come along with running a Java stack.
(more…)

Tags: , , , , , , ,
Posted in SaffronSierra | 1 Comment »