English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Programação de Rede do Erlang

No Erlang, a biblioteca inets pode ser usada para construir servidores web no Erlang. Vamos ver algumas funções usadas para programação web no Erlang. É possível implementar um servidor HTTP (também conhecido como httpd) para lidar com solicitações HTTP.

O servidor implementou muitas características, por exemplo-

  • Camada de Socket Segura (SSL)

  • Interface de Script do Erlang (ESI)

  • Interface de Gateway de Rede Geral (CGI)

  • Autenticação de identidade do usuário (usando Mnesia, Dets ou banco de dados de texto puro)

  • General log file format (supported or not supported disk_log(3))

  • URL alias

  • Action mapping

  • Directory listing

The first task is to start the web library via command.

inets:start()

The next step is to implement the start function of the inets library to implement the web server.

The following is an example of creating a web server process in Erlang.

For example

-module(helloworld). 
-export([start/0]). 
start() ->
   inets:start(), 
   Pid = inets:start(httpd, [{port, 8081}, {server_name,"httpd_test"}, 
   {server_root,"D://tmp"},{document_root,"D://tmp/htdocs"},
   {bind_address, "localhost"}]), io:fwrite("~p",[Pid]).

There are a few things to note about the above program.

  • the port number must be unique and cannot be used by any other program. The httpd service will be started on this port.

  • server_rootanddocument_rootis a mandatory parameter.

Output

Here is the output of the above program.

{ok,<0.42.0>}

To implement a Hello World web server in Erlang, please follow these steps-

Passo 1 −Implement the following code−

-module(helloworld). 
-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,"helloworld"}, 
      {server_root,"D://tmp"}, 
      {document_root,"D://tmp/htdocs"}, 
      {erl_script_alias, {"/erl", [helloworld]}}, 
      {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>" ]).

Passo 2−Execute o código da seguinte forma. Compile o arquivo acima e executeerlExecute a seguinte comando no

c(helloworld).

Você obterá a seguinte saída.

{ok,helloworld}

O próximo comando é-

inets:start().

Você obterá a seguinte saída.

ok

O próximo comando é-

helloworld:start().

Você obterá a seguinte saída.

{ok,<0.50.0>}

Passo 3−Você pode acessar a url agora- http://localhost:8081/erl/hello_world:service.