How to write a simple webserver in Erlang?

2019-01-16 05:52发布

Using the default Erlang installation what is the minimum code needed to produce a "Hello world" producing web server?

标签: erlang
6条回答
相关推荐>>
2楼-- · 2019-01-16 05:57

Taking "produce" literally, here is a pretty small one. It doesn't even read the request (but does fork on every request, so it's not as minimal possible).

-module(hello).
-export([start/1]).

start(Port) ->
    spawn(fun () -> {ok, Sock} = gen_tcp:listen(Port, [{active, false}]), 
                    loop(Sock) end).

loop(Sock) ->
    {ok, Conn} = gen_tcp:accept(Sock),
    Handler = spawn(fun () -> handle(Conn) end),
    gen_tcp:controlling_process(Conn, Handler),
    loop(Sock).

handle(Conn) ->
    gen_tcp:send(Conn, response("Hello World")),
    gen_tcp:close(Conn).

response(Str) ->
    B = iolist_to_binary(Str),
    iolist_to_binary(
      io_lib:fwrite(
         "HTTP/1.0 200 OK\nContent-Type: text/html\nContent-Length: ~p\n\n~s",
         [size(B), B])).
查看更多
聊天终结者
3楼-- · 2019-01-16 06:01

Just one fix for Felix's answer and it addresses the issues Martin is seeing. Before closing a socket, all data being sent from the client should be received (using for example do_recv from gen_tcp description).

Otherwise there's a race condition for the browser/proxy sending the HTTP request being quick enough to send the http request before the socket is closed.

查看更多
干净又极端
4楼-- · 2019-01-16 06:06

Another way, similar to the gen_tcp example above but with less code and already offered as a suggestion, is using the inets library.

%%%
%%% A simple "Hello, world" server in the Erlang.
%%%

-module(hello_erlang).
-export([
  main/1,
  run_server/0,
  start/0
]).

main(_) ->
  start(),
  receive
    stop -> ok
  end.

run_server() ->
  ok = inets:start(),
  {ok, _} = inets:start(httpd, [
    {port, 0},
    {server_name, "hello_erlang"},
    {server_root, "/tmp"},
    {document_root, "/tmp"},
    {bind_address, "localhost"}
  ]).

start() -> run_server().                                                       

Keep in mind, this exposes your /tmp directory.

To run, simply:

$ escript ./hello_erlang.erl
查看更多
男人必须洒脱
5楼-- · 2019-01-16 06:08

Do you actually want to write a web server in Erlang, or do you want an Erlang web server so that you can create dynamic web content using Erlang?

If the latter, try YAWS. If the former, have a look at the YAWS source code for inspiration

查看更多
成全新的幸福
6楼-- · 2019-01-16 06:16

For a web server using only the built in libraries check out inets http_server. When in need of some more power but still with simplicity you should check out the mochiweb library. You can google for loads of example code.

查看更多
beautiful°
7楼-- · 2019-01-16 06:24

For a very easy to use webserver for building restful apps or such check out the gen_webserver behaviour: http://github.com/martinjlogan/gen_web_server.

查看更多
登录 后发表回答