Sunday, May 4, 2008

"Hello World" Webapp in Erlang

When I first started using Erlang it took a fair bit of trial and error to create a server that generated dynamic HTTP content. The main problem is the lack of good tutorials out there for doing these kinds of things. The documentation that comes with Erlang is good for reference, but is not that helpful if you're just learning the language.

In particular, there didn't seem to be a basic "Hello World" example for building web applications. So here's my attempt to fix that. Below is some code that starts up an HTTP server, and dynamically generates a simple "Hello, World" page:


-module(hello_world).
-export([start/0,service/3]).

start() ->
inets:start(httpd, [
{modules, [
mod_alias,
mod_auth,
mod_esi,
mod_actions,
mod_cgi,
mod_dir,
mod_get,
mod_head,
mod_log,
mod_disk_log
]},
{port,8081},
{server_name,"hello_world"},
{server_root,"log"},
{document_root,"www"},
{erl_script_alias, {"/erl", [hello_world]}},
{error_log, "error.log"},
{security_log, "security.log"},
{transfer_log, "transfer.log"},
{mime_types,[
{"html","text/html"},
{"css","text/css"},
{"js","application/x-javascript"}
]}
]).

service(SessionID, _Env, _Input) ->
mod_esi:deliver(SessionID, [
"Content-Type: text/html\r\n\r\n",
"<html><body>Hello, World!</body></html>"
]).

To run it, save the code to a file called hello_world.erl, and create two subdirectories next to it called "www" and "log" (these subdirectories can be empty, but they need to be there for the server to start). Then fire up erl and run the following three commands:

Eshell V5.6 (abort with ^G)
1> c(hello_world).
{ok,hello_world}
2> inets:start().
ok
3> hello_world:start().
{ok,<0.51.0>}

You should now be able to browse to the following URL and see your message (if for some reason it doesn't work for you please let me know):

http://localhost:8081/erl/hello_world:service

For more info, here are the reference docs for the relevant Erlang modules:

9 comments:

Viking said...

Thank you. I've been banging my head against the wall trying to figure out inets without much luck.

Witek Baryluk said...

I was fighting with ESI 2 hours, and was still having 404 errors, with info in log that it was trying to access file directly from disk. Then for the sake couriosity used hello_world.erl and it worked, it was practically the same. Other paths, other module names, some additionall options, same modules.



After next hour of searching, i found that mod_* entries order is important, and nobody warns about it. After switching it to be the third, after mod_alias and mod_auth it started working. This is particularry strange given that in documentation there is only notice that it uses mod_auth functionality. There is also notice that order is important, but doesn't explain why (is request passed throught all this modules in this order?). Mayby i had mod_get befor mod_esi, and it was giving this 404, i don't know.



Start with this hello_world.erl and then modify it in small steps, it will work.

Jean-Lou Dupont said...

Good stuff!

BTW, if you ever wanted an Erlang Syntax Highlighter for your blog, check out: http://jldupont.blogspot.com/2009/06/erlang-syntax-highlighter.html

Cheers,
Jean-Lou Dupont

Colin said...

Hi, Thank you for posting this! Much appreciated.

Anonymous said...

Thanks a lot, been fighting with this for a while now :)

Dan said...

Following the instructions, I get a 403:

Forbidden

You don't have permission to access /erl/hello_world:service on this server.

Gonna dig into mod_esi and mod_auth to try to figure out why.

Simon said...

Nice,

thank you Matthew for very helpful tutorial

Mahaveer Bubanale said...

WHile running above program i found below error

=ERROR REPORT==== 27-Sep-2016::14:42:44 ===
Failed initiating web server:
undefined
{invalid_option,{non_existing,{server_root,"log"}}}

Parvin Jadon said...

I m proud of myself for every single decision I have ever made, from cutting off people, to changing my priorities.
http://goatripsindia.com/goa-honeymoon-package