Recently, I purchased an iPod Touch. I was looking around for development docs, but it turns out that Apple are only letting people make webapps that run through Safari. I searched around for the best way to make apps from scratch with a webserver built in, and my searching returned me to my favourite scripting language: Ruby. Ruby has a library called WEBrick built in that is specifically tailored to serving HTTP requests.
My first venture into the world of WEBrick was to write an iTunes control program. It turned out quite nicely, but it was definitely lacking in features. Instead of continuing down the path that others had already gone and adding more features, I took a new one. I wanted to be able to read my comics on my iTouch and I could not find a solution anywhere else on the interwebs, so I decided to write my own CBR reader. CBR files are comic books compressed in a RAR archive; each image represents one page. With some Ruby and some regular expression magic, I can now read Tintin while lying on the couch!
The code is surprisingly simple. All of the webrick-handling code is less then 25 lines:
# jcomix.rb require 'webrick' include WEBrick # So we dont need the WEBrick:: namespace require 'config' require 'ComixServlet' def start_webrick(config = {}) # always listen on port 8081 config.update(:Port => $gPort) server = HTTPServer.new(config) yield server if block_given? ['INT', 'TERM'].each {|signal| trap(signal) {server.shutdown} } server.start end start_webrick {|server| server.mount("/comics", HTTPServlet::FileHandler, $gPath, {:FancyIndexing=>true}) server.mount('/', ComixServlet) }
The servlet code is also very simple, the basic framework for usage is:
class ComixServlet < HTTPServlet::AbstractServlet # Comix Servlet def do_GET(req, resp) # Parse req for the GET variable (eg req.query['id'] gives the value of ?id= in the url) resp.body = 'Hello World' # Send 'Hello World' to the browser end end
Add a bit more code and you have a webapp ready to roll! I will shortly be providing a link to my finished jComix application if anyone is interested.