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!


roberto said...


? :)


Serge said...

I think there must be even more than that, everyone solves it for himself