Running Ratpack inside Grails

When I would present on Ratpack at conferences, one of the main questions I'd get would be around the migration path from Ratpack to Grails. I'd usually answer noting that you could run Ratpack along side Grails. It turns out it is incredibly simple to run a Ratpack app inside Grails. I don't think it's a long term solution and best as an intermediary step to migrate from one side to the other.
 
1. Grab and build Ratpack.
Provided you already have Gradle installed, run gradle buildDistro. The only file you need is the main Ratpack-x.x file. Grails will provide the Groovy and servlet container run time for you. Add this to the lib directory of your Grails app. 
 
2. Create your Ratpack app class. 
Putting the script into a proper class, as shown below, makes it easier to reference it from our Servlet.
 
import java.text.SimpleDateFormat
import com.bleedingwolf.ratpack.*

class SampleApp {

def app = Ratpack.app {
set 'port', 4999

get("/") {
def ua = headers['user-agent']
    "Your user-agent: ${ua} from Ratpack"
}
    
get("/foo/:name") {
"Hello, ${urlparams.name}"
}
    
get("/person/:id") {
"Person #${urlparams.id}"
}
  }

  public static void main(String[] args) {
SampleServlet.serve(new SampleApp().app)
  }
}
 
3. Subclass *RatpackServlet*.
If you don't mind getting an logging error, you can use something as simple as the following file:
import com.bleedingwolf.ratpack.*

class SampleServlet extends RatpackServlet {
            void init() {
app = new SampleApp().app
            }                    
}
 
4. Add the servlet information to your application.
There is a really confusing way to do it using the resources.groovy file but I prefer just adding it to the web.xml by hand. Run
grails install-templates
and navigate to src/templates/war/web.xml and add the following:
<servlet>
<servlet-name>SampleServlet</servlet-name>
        <servlet-class>SampleServlet</servlet-class>

       </servlet>

<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
                <url-pattern>/ratpack/*</url-pattern>
</servlet-mapping>
 
The servlet needs to be put on a url-pattern that isn't root (/*) so I used (/ratpack/*). Run grails run-app and you're all set.

 

Say No to iBooks Author

Apple, at their recent event announcement, unveiled a new innovation in the textbook market with iBooks Author. Most of the words around this event use flowery language of how groundbreaking it is and how it will change the market. I think it stands to do more damage to the market than help.
 
Let's start with the ridiculous EULA, Apple requires any work that is created with iBooks Author(IBA) to be sold only in the Apple iBooks store. Apple doesn't have the right to tell me where I can sell the work I create on my own equipment. This seems to be nothing more than a ploy to bolster the paltry selection of books in iBooks, an elite bookstore. I don't fault them for wanting to upset the publishing world because as an author, my cut of the take isn't that great with a traditional publisher. Crafting a means for authors to get a more equitable cut could only be good for them, right? In this case, I'd argue not. You would be trading masters. Get a little more money but also a master that is more ruthless. 
 
iBooks will have to be appoved just like normal iOS apps. Though much improved in recent years, releasing an app on iOS is often unpredictable. In addition to not knowing precisely when a book will be available for purchase, you are dependent on Apple. ONLY Apple.
 
As Daniel Glazman, a member of the W3C CSS Working Group, notes, IBA uses a proprietary format that is not compatible with any current software. He asserts a point that is hard to argue. Had Apple participated in the standards process openly, we could have a more robust EPUB standard AND Apple would have first to market advantage. John Gruber rebuts Daniel's post stating that it IBA need not be standards compliant and throws out the canard of Amazon's mobi format being proprietary. A key difference is the mobi format is largely open, setting aside the .azw DRM variant for a second, and many tools can create them. Good or bad, anyone can publish a book to the Kindle store and have access to hundreds of millions of devices when you count that Kindle apps exist for Android, iOS, WP7, and the Chrome Web browser. 
 
Let's go back in time a couple years to the start of the Android vs iOS debate. You'd ask devs why they didn't develop for Android and first they'd say "The iOS market is bigger." After Android activations surpassed iOS, the excuse became "The Android market is fragmented." Let's turn that argument on its head.
 
When you have 
  • more open formats like MOBI and EPUB that benefit from a number of tools that can create them, 
  • many online stores that sell books in those formats (SmashWords and Amazon to name a few), and
  • the ability to create your own e-commerce site with your content, and,
  • access to hundreds of millions devices of all shapes and sizes,

do you really want to give that all up for Apple?

HTML5 Game Programming FAQ Day 2

This week I'm answering questions about HTML5 and Game Programming in general over at CodeRanch. Wednesday's questions seems to be dominated by inquiries of cross-browser feature compatibility with several variants.
 
*Q: What are some device considerations when making HTML5 apps?*
A: The best guide to check is CanIUse.com. It lists the capabilities of the mobile browsers along side the desktop browsers. YMMV when it comes to mobile browsers.
 
*Q: What is HTML5 geolocation sometimes wrong?*
A: Geolocation can be inferred in many different ways, one of which is your IP address. Sometimes your IP address can be assigned to a location other than your physical location. For example, your company or ISP might be giving you an IP for a different locality or might be routing your traffic to another site which serves as the IP of record for your request.
 
*Q: What's the point of the audio tag?*
A:The key value in this is that it doesn't require a plugin. It's built into the browser. There are compatibility issues between browsers on what formats are supported. The three audio types that are supported in some shape or form are OGG(Ogg Vorbis), MP3, and WAV. The audio tag does have function called *canPlayType* that tells you if the specific browser might support a file type. 
 
If you want to use a library to test availability of HTML5 features, check out Modernizr(http://modernizr.com). 
 
Though not as widely supported, you should look into the WebAudio API. It picks up where the Audio tag leaves off like better support for volume/gain management, multiplexing sound, etc. 
 
*Q: What can I use for easing functions?*
A: If you aren't using RaphaelJS(SVG) which has built-in functions for easing, you could implement them yourself or try out Trident.js(https://github.com/kirillcool/trident-js) which allows you to almost any JavaScript property or element.