">
 

HTTP Server in Erlang with Cowboy

Iniciado por joomlamz, 29 de Maio de 2026, 10:00

Respostas: 1   |   Visualizações: 13

Tópico anterior - Tópico seguinte

0 Membros e 2 Visitantes estão a ver este tópico.

Olá, comunidade de webmastersmz.com! Hoje, vamos discutir sobre o uso de Inteligência Artificial Física (Physical AI) nas empresas. De acordo com os dados apresentados, 79% das empresas já estão implantando essa tecnologia. Isso é um indicador de que a Physical AI está se tornando cada vez mais importante no mundo dos negócios.

A Physical AI refere-se ao uso de algoritmos de inteligência artificial para controlar e otimizar sistemas físicos, como robôs, drones, veículos autônomos, entre outros. Essa tecnologia tem o potencial de revolucionar a forma como as empresas operam, melhorando a eficiência, reduzindo custos e aumentando a produtividade.

Os pontos principais que devemos considerar ao discutir sobre Physical AI incluem:

* A capacidade de automatizar processos manuais, liberando os funcionários para tarefas mais complexas e criativas;
* A possibilidade de melhorar a segurança e a precisão em processos industriais, como a manufatura e a logística;
* A necessidade de investir em treinamento e desenvolvimento de habilidades para que os funcionários possam trabalhar em conjunto com a Physical AI;
* A importância de garantir a segurança e a privacidade dos dados coletados e processados pela Physical AI.

É fundamental que as empresas estejam preparadas para adotar essa tecnologia e aproveitar seus benefícios. No entanto, também é importante considerar os desafios e os riscos associados à implementação da Physical AI, como a necessidade de investir em infraestrutura e tecnologia adequadas, bem como a possibilidade de perda de empregos devido à automação.

Para discutir mais sobre esse tópico e compartilhar experiências e conhecimentos, convido todos a participar do fórum webmastersmz.com. Vamos debater sobre as oportunidades e os desafios da Physical AI e como podemos aproveitar essa tecnologia para melhorar nossos negócios e serviços.

Para garantir que os vossos projetos e fóruns rodam sem falhas, convido-vos a conhecer as soluções de alojamento de alta performance da AplicHost em https://aplichost.com. Com a AplicHost, você pode ter certeza de que seus projetos estão em boas mãos, com infraestrutura de ponta e suporte técnico especializado. Além disso, a AplicHost oferece soluções personalizadas para atender às necessidades específicas de cada cliente, garantindo que seus projetos sejam executados com eficiência e segurança. Então, não perca mais tempo e descubra como a AplicHost pode ajudar a levar seus projetos ao próximo nível!

HTTP Server in Erlang with Cowboy



Tópico: HTTP Server in Erlang with Cowboy
Categoria: Tutoriais | Programação & Tecnologia
Idioma Principal: Português (Conteúdo de Tecnologia)

Descrição do Conteúdo / Informações:
-------------------------------------------------------------------------
Finally, a bit of Erlang! Instead of writing the same 101 introduction to this marvelous language, we will switch directly on the most used HTTP Web Server on this ecosystem: Cowboy. The goal will be to create a flexible module letting us configuring HTTP/1.1, HTTP/2, HTTP/3 and WebSocket easily. Why do I need that? For some of my local test, especially for Dart/Flutter.

Anyway, it's time to create a new fresh project to explain that, let call it Buckaroo.



Project Bootstrapping with rebar3


Just a quick reminder for the newcomers who never tried Erlang, below, the command to generate a new app project.

$ rebar3 new app name=buckaroo

All the magic will happen in the src/ directory but cowboy must be added in the dependencies list in rebar.config.

{erl_opts, [debug_info]}.
{deps, [cowboy]}.
{shell, [{apps, [buckaroo]}]}.

Let fetch everything.

$ rebar3 get-deps
$ rebar3 compile



Starting Cowboy


To make things easier, a dedicated module to manage cowboy called buckaroo_cowboy is created. And to avoid too much complexity, only 2 functions will be exported

• start_link/0 to start the cowboy listener;

• stop/0 to stop the cowboy listener.

-module(buckaroo_cowboy).
-export([start_link/0, stop/0]).

A cowboy listener needs a name, let create a function called name/0 to deal with that. It will return the listener name.

name() ->
buckaroo_listener.

Now, the HTTP API routes are required, the cowboy routing documentation is complete and should be way enough to understand how to routes requests with cowboy. 2 routes will be created there, the default one (/) will be used for HTTP requests, and (/ws) for the WebSockets. In both case, the last element of the tuple passed by the router will be a map(), this value will be used as connection state.

routes() ->
[
{'_', [
{"/", buckaroo_handler, #{}},
{"/ws", buckaroo_websocket_handler, #{}}
]
}].

The routes, then, must be compiled with the help of the cowboy_router:compile/1 function.

dispatch() ->
cowboy_router:compile(
routes()
).

A cowboy listener will also require 2 kinds of options, the transport options (TCP) and the protocol options (including HTTP and HTTP/2 options). In our case, only the listening port (TCP/8081) will be set.

transport_options() ->
[
{port, 8081}
].

The protocol options can be defined, in this case, only the routing will be configured via the dispatch key.

protocol_options() ->
#{
env => #{
dispatch => dispatch()
}
}.

To start a cowboy listener, the (cowboy:start_clear/3)(https://ninenines.eu/docs/en/cowboy/2.15/manual/cowboy.start_clear/) function can be used. If a TLS certificate is configured, the cowboy:start_tls/3 must be used instead. The first argument is the name of the listener, the second the transport options and the last one the protocol options.

start_link() ->
cowboy:start_clear(
name(),
transport_options(),
protocol_options()
).

Finally, the stop() function can be created to help shutdown cowboy, using simply the name previously configured and with the cowboy:stop_listener/1 function.

stop() ->
cowboy:stop_listener(name()).

A quick remark regarding my coding style. I like creating really small functions and call them in some part of the code. It can help to document but also to test the module. We will see that in another publication about finite state machines.

The final modification, is to add the cowboy listener in the supervision tree by editing src/buckaroo_sup.erl.

-module(buckaroo_sup).
-behavior(supervisor).
-export([start_link/0, init/1]).

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init(_) ->
{ok, {supervisor_flags(), child_specs()}}.

supervisor_flags() ->
#{
strategy => one_for_all,
intensity => 0,
period => 1
}.

child_specs() ->
[
#{
id => buckaroo_cowboy,
start => {buckaroo_cowboy, start_link, []}
}
].

Now, the application can be started, for development and debug purpose only, it will be started with an Erlang shell.

$ rebar3 shell
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling buckaroo
Erlang/OTP 29 [erts-17.0] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Eshell V17.0 (press Ctrl+G to abort, type help(). for help)
===> Booted cowlib
===> Booted ranch
===> Booted cowboy
===> Booted buckaroo

Well done, but it will not work correctly, because the handlers are missing... Let fix that in the next sections of this article.



Dealing with HTTP Requests


An handler can usually deal with HTTP requests but also with WebSocket ones. Let

-module(buckaroo_handler).
-export([init/2]).
-include_lib("kernel/include/logger.hrl").

init(Req, State) ->
Reply = cowboy_req:reply(200,
#{
<<"content-type">> => <<"text/plain">>
},
<<"hello">>,
Req
),
{ok, Reply, State}.

The cowboy request object is the core data structure used by any handlers, here a quick view of its content when using curl locally.

#{
pid => <0.1260.0>,
port => 8081,
scheme => <<"http">>,
version => 'HTTP/1.1',
path => <<"/">>,
host => <<"localhost">>,
peer => {{127,0,0,1},50986},
bindings => #{},
cert => undefined,
headers => #{
<<"accept">> => <<"*/*">>,
<<"host">> => <<"localhost:8081">>,
<<"user-agent">> => <<"curl/8.14.1">>
},
ref => buckaroo_listener,
host_info => undefined,
path_info => undefined,
streamid => 1,
method => <<"GET">>,
body_length => 0,
has_body => false,
qs => <<>>,
sock => {{127,0,0,1},8081}}
}.

As you can see, this is a simple map() containing specific keys. The value of those keys can be retrieved using Erlang pattern matching, or by using the cowboy functions helpers from the module cowboy_req. This data structure will be used to route the request and alter the final response, so, let check them one by one.

• pid is the process id of the process in charge of the request;

• port is the port from the listener and can also be extracted using cowboy_req:port/1 function;

• scheme is the scheme using, usually http or https. This information can be obtained using cowboy_req:scheme/1 function as well;

• version is the protocol version used, it can be HTTP/1.0, HTTP/1.1, HTTP/2 or HTTP/3. It can be extracted with the help of cowboy_req:version/1 function;

• path represents the full path requested by the client, this value is more or less related to the routing defined when starting cowboy, it can be extracted using cowboy_req:path/1 function;

• host represents the host name requested by the client, it is also a part of the routing configuration, this value can also be extracted using cowboy_req:host/1 function;

• peer contains the IP address/port of the client, it can also be extracted using cowboy_req:peer/1 function;

• bindings is set when the route is configured with pattern matching, if it was the case, the variables defined in the route would have been converted into bindings;

• cert is set when a certificate is sent by the client, it can be extracted using cowboy_req:cert/1 function;

• headers are the HTTP Headers sent by the client, an header value can be extracted using cowboy_req:header/2 or  cowboy_req:header/3 functions;

• ref (internal usage) stores the name of the listener;

• host_info returns the routing pattern matching result, it can also be accessed using cowboy_req:host_info/1 function;

• path_info returns the routing pattern matching result, it can also be extracted using cowboy_req:path_info/1 function;

• streamid (internal usage) defines the stream id of the connection;

• method defines the HTTP method used by the client for the request, it can also be retrieved using cowboy_req:method/1 function;

• body_length defines the size of the data sent by the client in the HTTP body, the value of this key can be also fetched using cowboy_req:body_length/1 function;

• has_body defines if the request contains a body or not. Mostly used with POST, PUT and other HTTP methods sending data. It can also be checked using cowboy_req:has_body/1 function;

• qs is the raw query string, it should be parsed using uri_string:dissect_query/1 function or with your own method. The value can also be retrieved using cowboy_req:qs/1;

• sock contains the IP address and port of the server, it can be extracted using cowboy_req:sock/1 function.

The handler is done, it's a really simple one, but just to check if it's working, we can use curl.

$ curl -kv http://localhost:8081/; echo
* Host localhost:8081 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* HTTPS-RR: -
*   Trying [::1]:8081...
* connect to ::1 port 8081 from ::1 port 48828 failed: Connection refused
*   Trying 127.0.0.1:8081...
* Established connection to localhost (127.0.0.1 port 8081) from 127.0.0.1 port 48800
* using HTTP/1.x
> GET / HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/8.20.0
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< content-length: 5
< content-type: text/plain
< date: Wed, 27 May 2026 12:20:21 GMT
< server: Cowboy
<
* Connection #0 to host localhost:8081 left intact
hello



Dealing with WebSockets


Cowboy can also managed WebSocket connections using websocket handlers. The idea is to create 4 callback functions, one for the HTTP request and three for the WebSocket events.

-module(buckaroo_websocket_handler).
-export([init/2]).
-export([websocket_init/1, websocket_handle/2, websocket_info/2]).
-include_lib("kernel/include/logger.hrl").

When a client wants to use WebSocket protocol, it will first send an HTTP request with some specific information, then the server will start the negotiation, this step, is mostly done in init/2 callback.

init(Req, State) ->
WebSocketOpts = #{
active_n => 1,
compress => false,
data_delivuery => stream_handlers,
data_delivery_flow => 1,
deflate_opts => #{},
dynamic_buffer => {1024, 131_072},
idle_timeout => 60_000,
max_frame_size => 120_000,
req_filter => fun(Req) -> Req end,
validate_utf8 => true
},
?LOG_DEBUG("~p:~p: ~p~n", [
?MODULE,
?FUNCTION_NAME,
WebSocketOpts,
Req
]),
{cowboy_websocket, Req, State, WebSocketOpts}.

At the end of the negociation, the server can do some initialization state using the websocket_init/1 callback. In our case, the text event hey\n will send to the client by the server.

websocket_init(State) ->
?LOG_DEBUG("~p:~p: ~p~n", [?MODULE, ?FUNCTION_NAME]),
{[{text, <<"hey\n">>}], State}.

To handle messages sent by the client, a websocket_handle/2 function must be created and exported by the module. The first argument received is a tuple containing the type of data (defined in the websocket_handle callback and also defined as cow_ws:frame() type), and its content. The second argument is the connection state.

websocket_handle(Frame = {text, Message}, State) ->
?LOG_DEBUG("~p:~p: ~p, ~p~n", [?MODULE, ?FUNCTION_NAME, Frame]),
{[{text, Message}], State};
websocket_handle(Frame = {binary, Message}, State) ->
?LOG_DEBUG("~p:~p: ~p, ~p~n", [?MODULE, ?FUNCTION_NAME, Frame]),
{[{binary, Message}], State};
websocket_handle(Frame, State) ->
?LOG_DEBUG("~p:~p: ~p, ~p~n", [?MODULE, ?FUNCTION_NAME, Frame]),
{[Frame], State}.

The websocket handler is running in a process, and this process can receive message from another running process from the system. In this case, the message will be handled by websocket_info/2 function callback.

websocket_info(Event, State) ->
?LOG_DEBUG("~p:~p: ~p, ~p~n", [?MODULE, ?FUNCTION_NAME, Event]),
{ok, State}.

Let check that with curl to see what will happen.

$ curl -N -T. --no-progress-meter -v ws://localhost:8081/ws
* Host localhost:8081 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* HTTPS-RR: -
*   Trying [::1]:8081...
* connect to ::1 port 8081 from ::1 port 45396 failed: Connection refused
*   Trying 127.0.0.1:8081...
* Established connection to localhost (127.0.0.1 port 8081) from 127.0.0.1 port 43458
* using HTTP/1.x
> GET /ws HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/8.20.0
> Accept: */*
> Upgrade: websocket
> Sec-WebSocket-Version: 13
> Sec-WebSocket-Key: +t+tuHiQUK6MMQySUqDGtQ==
> Connection: Upgrade
>
* Request completely sent off
< HTTP/1.1 101 Switching Protocols
< connection: Upgrade
< date: Wed, 27 May 2026 12:21:53 GMT
< sec-websocket-accept: F87i2EOBLl+kwTQ3PCdZqjjcxuA=
< server: Cowboy
< upgrade: websocket
<
* Received 101, Switching to WebSocket
* [WS] Received 101, switch to WebSocket
{ [5 bytes data]
hey

hello!
hello!
test1
test1
* upload completely sent off: 32 bytes

The websocket handler correctly acts as an echo server, it looks good!



Conclusion


It's always a pleasure to write Erlang code, even more after many weeks where I passed all my time learning Dart/Flutter. Erlang code is concise and efficient. Cowboy is following the same philosophy there, and give us a good idea how to manage requests.

This project example can be seen on niamokik/buckaroo repository at Github.

Have fun!



References and Resources



Cowboy Source Code on Github

Cover Image by JJ Ying on Unsplash


Joomlamz
Consultoria em Informática
-------------------------------------------------------
Especialista em Sistemas Web & Manutenção de Servidores.
A desenvolver o novo AplPortal com suporte a PHP 8.
Precisa de ajuda profissional? Contacte-me.

Tags: