Friday, December 16, 2005

MiniRails

Lots of people are using web technologies in their desktop applications, and it is especially common to find a windows application which embeds WebBrowser control to render great GUI elements. There are some bumps, for example, you can not do disk/socket IO with javascript(even java applet), which most applications have to reply on.

To overcome this, we can either use ActiveX control(I am using it myself and am satisfied with it), or launch a web server on the background. The second has an additional advantage: the server and client are decoupled, so in the future you can easily migrate to a pure web environment without touching too much code. While this approach looks attractive, however, we have to face a reality: most web application servers can be too big for most desktop applications to distribute. Note I am taking web application servers here, not simple web servers. It is easy to find a small footprint web server or even write one by yourself, but your development style will have to go back to the 80s (CGI in C). Even though Tomcat Embedded 5.5.12 can be downloaded as a 3.1 MB zip file, your customers may not have JVM installed in the first place. Cassini suffers the same problem, Microsoft has not pushed hard enough to make .NET universal.

How about the Ruby on Rails? It turns out to be a great choice. While a complete distribution takes lots of disk space, it is easy to customize so that we only ship what we need.

Here is how I build my minimum ruby distribution that rail can run on(use windows as example):

1. Have ruby installed to C:\ruby. Later we need to copy files from it.

2. Download rails stand-alone packages, uncompress it to C:\rails, and empty doc/ folder to save space.

3. Copy ruby.exe and msvcrt-ruby18.dll from C:\ruby\bin to C:\rails.

4. Copy the following OS independent libraries (implemented in pure ruby) from C:\ruby\lib\ruby\1.8 to C:\rails\lib\ruby\1.8:

base64.rb
benchmark.rb
cgi.rb and cgi/
date.rb
date/format.rb
delegate.rb
drb.rb and drb/
e2mmap.rb
erb.rb
English.rb
fileutils.rb
ipaddr.rb
irb.rb and irb/
kconv.rb
logger.rb
mutex_m.rb
net/http.rb
net/protocol.rb
net/smtp.rb
observer.rb
optparse.rb and optparse/
parsedate.rb
pathname.rb
pstore.rb
rational.rb
rexml.rb and rexml/
set.rb
singleton.rb
soap.rb and soap/
tempfile.rb
thread.rb
time.rb
timeout.rb
tmpdir.rb
uri.rb and uri/
webrick.rb and webrick/
xmlrpc/
xsd/
yaml.rb and yaml/


5. Copy the following OS dependent libraries from C:\ruby\lib\ruby\1.8\i386-mswin32 to C:\rails\lib\ruby\1.8\i386-mswin32:

digest.so
digest/md5.so
digest/sha1.so
fcntl.so
nkf.so
rbconfig.rb
socket.so
strscan.so
stringio.so
syck.so


6. Now we may still need a database (optional). Here is how I get sqlite3 installed:
a) Download precompiled binaries. Copy sqlite3.exe and sqlite3.dll into C:\rails folder.
b) Download SQLite-Ruby. Copy sqlite3_api.so to C:\rails\lib\ruby\site_ruby\1.8\i386-msvcrt\, copy sqlite3.rb and sqlite3/ to C:\rails\lib\ruby\site_ruby\1.8
That's it! You can start WEBrick now:

C:\rails>ruby script\server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2005-12-16 15:23:09] INFO WEBrick 1.3.1
[2005-12-16 15:23:09] INFO ruby 1.8.2 (2004-12-25) [i386-mswin32]
[2005-12-16 15:23:09] INFO WEBrick::HTTPServer#start: pid=440 port=3000


And do whatever rails developement as you want.

This distribution takes 6.28M on disk. If you zip it, it shrinks to a 2.3M file.

0 Comments:

Post a Comment

<< Home