emptygif
اللغة: العربية

كل اللغات وأطر العمل

Cowboy Erlang

مقتطف بلغة Erlang لـ Cowboy يستجيب لطلبات /pixel.gif بملف GIF الفارغ، الذي يُفك ترميزه مرة واحدة عند بدء التشغيل.

Cowboy Erlang

التثبيت
{deps, [cowboy]}.
الملف
pixel_handler.erl
-module(pixel_handler).
-behaviour(cowboy_handler).
-export([start/0, init/2]).

%% A binary literal is built once, at compile time.
-define(GIF, <<71, 73, 70, 56, 57, 97, 1, 0, 1, 0, 128, 0, 0, 255, 255, 255, 0, 0, 0, 33, 249, 4, 1, 0, 0, 0, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 68, 1, 0, 59>>).

%% Call from your application's start/2.
start() ->
    Dispatch = cowboy_router:compile([{'_', [{"/pixel.gif", ?MODULE, []}]}]),
    {ok, _} = cowboy:start_clear(pixel_http, [{port, 8080}],
                                 #{env => #{dispatch => Dispatch}}).

init(Req0, State) ->
    Req = cowboy_req:reply(200, #{
        <<"content-type">> => <<"image/gif">>,
        <<"cache-control">> => <<"no-store">>
    }, ?GIF, Req0),
    {ok, Req, State}.

شغّلنا هذا المقتطف وقارنّا البايتات التي أعادها.

التحقق

curl -sI http://localhost:8080/pixel.gif

الموقع الرسمي: Cowboy