Using Grails as the API server for a spine app and Spine.js as a front end framework has been going well. My Spine app has up till now lived in the web-apps directory of my Grails app. After living with this arrangement for a while I started to realize how separating the two even further would have many all-around advantages. Decoupling code makes new projects easier to manage by isolating versioning, testing, deployment/release for example. In production environments this sort of setup isnât all that uncommon, you let Apache or something like it serve static image, js or css files and use mod_jk or some proxy setup to pass the rest on to Java app server like Tomcat. My setup is a little different in that I want to serve html and js and have to make sure all relative links still work for ajax API requests, and locally there is a cross domain issue if you try to simply work from files in the browser and make calls to Grails running on Tomcat Long story short; to get my decoupled dev setup here is what I did:
- Install Apache
- Get it running
- Set up virtualhost that will proxy appropriate requests to Grails (need mod_proxy_http)
- Add alias definitions for Spine apps
Steps one and two are relatively standard stuff. On Ubuntu it is trivially easy. sudo apt-get install apache2
Step 3 was new to me. Searching turned up a good starting point on that, but simply proxy isnât what I really needed. That brings us to step 4. I needed some interceptors (Aliasâ) for my spine apps, so that relative links still played nice. For example, I wanted âhttp://localhost/grailsApp/spineApp/â to direct to my Spine app, while links within the spine app like â../api/book/5â were handled by Grails. an old forum post contained the nugget I was looking for. Basicially this is what I needed inside of my virtualhost block: [bash] ProxyPass /grailsApp/spineApp ! Alias /grailsApp/spineApp /path/to/spineApp/on/fileSystem ProxyRequests Off ProxyPreserveHost On ProxyPass /grailsApp http://127.0.0.1:8080/grailsApp ProxyPassReverse /grailsAPp http://127.0.0.1:8080/grailsApp [/bash]
Grails config will have to change to handle redirects and such in grails controllers, that setting is normally in grails-app/conf/Config.groovy [groovy] environments { development { grails.serverURL = âhttp://localhost/grailsAppâ ⌠[/groovy]