Tuesday, July 7, 2009

Minimal web server on Erlang? Like Ruby' webrick?

I needed something nice and simple (read: stand-alone with no dependencies) to embed into the app. The good old Joe Armstrong's Pico, taken right from the distro, happened to work just fine, with one minor patch in the http_driver.erl:

< isolate_arg([$:,$ |T], L) ->
{httpd_util:to_lower(reverse(L)), T};
> isolate_arg([$:,$ |T], L) ->
{string:to_lower(reverse(L)), T};

Plus, working on Windows, I had to convert the launcher script into theexpose_folder.cmd:

@echo off
SET PA=c:\src\web_server
SET ERL="C:\Program Files\erl5.7.1\bin\erl.exe"
if %1. EQU start. (
%ERL% -boot start_sasl -sname webserver001 \
-pa %PA% -heart -detached -s web_server start %port%
) else ( if %1. EQU debug. (
%ERL% -sname webserver001
-pa %PA% -s web_server start %PORT%
) else ( if %1. EQU stop. (
%ERL% -noshell -sname webserver_stopper
-pa %PA% -s web_server stop webserver001@%HOSTNAME%
echo "Stopping Webserver"
) else (
echo "Usage: expose_folder.cmd {start|stop|debug}" )))

  1. adjust the paths in this script
  2. put this script into the folder, listed in the PATH environment variable
  3. Go to folder which you want to web-enable
  4. run the expose_folder.cmd start
  5. start browsing your folder
  6. stop it using: expose_folder.cmd stop

No, I don't want to carry the whole CEAN over and yes, MochiWeb is very cool, but is still a bit too large for my purposes.

Why "they" can store the tiny Ruby file into any folder to make it the web-enabled, and "we" cannot?

require 'webrick'
include WEBrick
s = HTTPServer.new(:Port => 9090,
:DocumentRoot => Dir::pwd )
trap("INT"){ s.shutdown }

Yes, I can work it out for myself, but this is the wheel to re-invent. The official Erlang distro might have it to make the whole Erlang experience much more fun and right away!

Monday, July 6, 2009

That was easy - Erlang and CouchDB on Ubuntu 9.04

  1. Installed Ubuntu 9.04 from ubuntu.com ISO
  2. Googled "erlang ubuntu"
  3. Figured that it wouldn't hurt to try
       sudo apt-get install erlang
  4. Went to grab some coffee
  5. Gave it a try

    user@ubuntu:~/src$ erl
    Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [kernel-poll:false]

    Eshell V5.6.5 (abort with ^G)
    1> q().
    2> user@ubuntu:~/src$
    It is not the latest one, but it is still Erlang!
  6. How about CouchDB?
       sudo apt-get install couchdb
  7. and test it: http://localhost:5984/
  8. see the output:
That was easy!