Saturday, February 25, 2012

Grails 2.0, Heroku and Neo4j addon


This post will help you get started with Grails 2.0 on Heroku with the Neo4j addon.
This largely follows the guide at http://devcenter.heroku.com/articles/grails
Note: This does not use the Grails Neo4j plugin (http://grails.org/plugin/neo4j), since I couldn't get it working with Grails 2.0 (Will post an update as and when I can)
(Update: here's the one with the neo4j plugin http://thought-bytes.blogspot.in/2012/03/getting-started-with-grails-neo4j.html)

Create the grails app
>grails create-app heroku-neo4j
>cd heroku-neo4j

Delete the DataSource.groovy
>del grails-app\conf\DataSource.groovy

Check into Git
Create a .gitignore file for ignored files and check into git
*.iws
*Db.properties
*Db.script
.settings
.classpath
.project
eclipse
stacktrace.log
target
/plugins
/web-app/plugins
/web-app/WEB-INF/classes
web-app/WEB-INF/tld/c.tld
web-app/WEB-INF/tld/fmt.tld


>git init
>git add .
>git commit -m init

Create a Heroku app
>heroku create --stack cedar

...and deploy
>git push heroku master

Check it out in the browser
>heroku open

Add the Neo4J addon
>heroku addons:add neo4j:test

Install the Grails Rest plugin
>grails install-plugin rest

Now you can access the Neo4J datastore on Heroku via its REST interface. For example, to create new node from a controller,


import static groovyx.net.http.ContentType.JSON
    withRest(id: "neo4j", uri: "<REST-URL>") {
        auth.basic '<login> ', '<password> '
        def response = post(contentType:JSON, requestContentType:JSON, body: ['name': 'Artichoke'] )
    }
    render response.status
    render response.data


Replace <REST-URL> , <login>  and <password>  with your Neo4J instance's settings.

-Aldrin

1 comment:

Aldrin Misquitta and Luanne Misquitta said...

Actually, on the Heroku environment, the env variable NEO4J_REST_URL is set up with the instance's url with authentication embedded, so you do not really need to hard-code datastore locations.