From 5ce4c2bfb40ecc4b687a2941e612025a1c4ff913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 26 Sep 2014 15:58:44 +0300 Subject: Unify the init and terminate callbacks This set of changes is the first step to simplify the writing of handlers, by removing some extraneous callbacks and making others optional. init/3 is now init/2, its first argument being removed. rest_init/2 and rest_terminate/2 have been removed. websocket_init/3 and websocket_terminate/3 have been removed. terminate/3 is now optional. It is called regardless of the type of handler, including rest and websocket. The return value of init/2 changed. It now returns {Mod, Req, Opts} with Mod being either one of the four handler type or a custom module. It can also return extra timeout and hibernate options. The signature for sub protocols has changed, they now receive these extra timeout and hibernate options. Loop handlers are now implemented in cowboy_long_polling, and will be renamed throughout the project in a future commit. --- examples/chunked_hello_world/src/toppage_handler.erl | 10 +++------- examples/compress_response/src/toppage_handler.erl | 10 +++------- examples/cookie/src/toppage_handler.erl | 10 +++------- examples/echo_get/src/toppage_handler.erl | 10 +++------- examples/echo_post/src/toppage_handler.erl | 10 +++------- examples/eventsource/src/eventsource_handler.erl | 10 +++------- examples/hello_world/src/toppage_handler.erl | 10 +++------- examples/rest_basic_auth/src/toppage_handler.erl | 6 +++--- examples/rest_hello_world/src/toppage_handler.erl | 6 +++--- examples/rest_pastebin/src/toppage_handler.erl | 10 ++++------ examples/rest_stream_response/src/toppage_handler.erl | 10 +++------- examples/ssl_hello_world/src/toppage_handler.erl | 10 +++------- examples/upload/src/upload_handler.erl | 14 ++++++-------- examples/web_server/src/directory_handler.erl | 10 +++------- examples/websocket/src/ws_handler.erl | 15 +++------------ 15 files changed, 49 insertions(+), 102 deletions(-) (limited to 'examples') diff --git a/examples/chunked_hello_world/src/toppage_handler.erl b/examples/chunked_hello_world/src/toppage_handler.erl index 4e6b4e4..6b47156 100644 --- a/examples/chunked_hello_world/src/toppage_handler.erl +++ b/examples/chunked_hello_world/src/toppage_handler.erl @@ -3,12 +3,11 @@ %% @doc Chunked hello world handler. -module(toppage_handler). --export([init/3]). +-export([init/2]). -export([handle/2]). --export([terminate/3]). -init(_Transport, Req, []) -> - {ok, Req, undefined}. +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> Req2 = cowboy_req:chunked_reply(200, Req), @@ -18,6 +17,3 @@ handle(Req, State) -> timer:sleep(1000), cowboy_req:chunk("Chunked!\r\n", Req2), {ok, Req2, State}. - -terminate(_Reason, _Req, _State) -> - ok. diff --git a/examples/compress_response/src/toppage_handler.erl b/examples/compress_response/src/toppage_handler.erl index 7c1569a..09c8689 100644 --- a/examples/compress_response/src/toppage_handler.erl +++ b/examples/compress_response/src/toppage_handler.erl @@ -3,12 +3,11 @@ %% @doc Compress response handler. -module(toppage_handler). --export([init/3]). +-export([init/2]). -export([handle/2]). --export([terminate/3]). -init(_Transport, Req, []) -> - {ok, Req, undefined}. +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> BigBody = @@ -26,6 +25,3 @@ in many other parts of the world, particularly South America and Australia, who perform work similar to the cowboy in their respective nations.\n">>, Req2 = cowboy_req:reply(200, [], BigBody, Req), {ok, Req2, State}. - -terminate(_Reason, _Req, _State) -> - ok. diff --git a/examples/cookie/src/toppage_handler.erl b/examples/cookie/src/toppage_handler.erl index 837f83b..d1a1126 100644 --- a/examples/cookie/src/toppage_handler.erl +++ b/examples/cookie/src/toppage_handler.erl @@ -3,12 +3,11 @@ %% @doc Cookie handler. -module(toppage_handler). --export([init/3]). +-export([init/2]). -export([handle/2]). --export([terminate/3]). -init(_Transport, Req, []) -> - {ok, Req, undefined}. +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> NewValue = integer_to_list(random:uniform(1000000)), @@ -24,6 +23,3 @@ handle(Req, State) -> [{<<"content-type">>, <<"text/html">>}], Body, Req2), {ok, Req3, State}. - -terminate(_Reason, _Req, _State) -> - ok. diff --git a/examples/echo_get/src/toppage_handler.erl b/examples/echo_get/src/toppage_handler.erl index 50c5985..be657c6 100644 --- a/examples/echo_get/src/toppage_handler.erl +++ b/examples/echo_get/src/toppage_handler.erl @@ -3,12 +3,11 @@ %% @doc GET echo handler. -module(toppage_handler). --export([init/3]). +-export([init/2]). -export([handle/2]). --export([terminate/3]). -init(_Transport, Req, []) -> - {ok, Req, undefined}. +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> Method = cowboy_req:method(Req), @@ -25,6 +24,3 @@ echo(<<"GET">>, Echo, Req) -> echo(_, _, Req) -> %% Method not allowed. cowboy_req:reply(405, Req). - -terminate(_Reason, _Req, _State) -> - ok. diff --git a/examples/echo_post/src/toppage_handler.erl b/examples/echo_post/src/toppage_handler.erl index de1f2ad..2bafed0 100644 --- a/examples/echo_post/src/toppage_handler.erl +++ b/examples/echo_post/src/toppage_handler.erl @@ -3,12 +3,11 @@ %% @doc POST echo handler. -module(toppage_handler). --export([init/3]). +-export([init/2]). -export([handle/2]). --export([terminate/3]). -init(_Transport, Req, []) -> - {ok, Req, undefined}. +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> Method = cowboy_req:method(Req), @@ -32,6 +31,3 @@ echo(Echo, Req) -> cowboy_req:reply(200, [ {<<"content-type">>, <<"text/plain; charset=utf-8">>} ], Echo, Req). - -terminate(_Reason, _Req, _State) -> - ok. diff --git a/examples/eventsource/src/eventsource_handler.erl b/examples/eventsource/src/eventsource_handler.erl index a184807..3aa60e7 100644 --- a/examples/eventsource/src/eventsource_handler.erl +++ b/examples/eventsource/src/eventsource_handler.erl @@ -3,24 +3,20 @@ %% @doc EventSource emitter. -module(eventsource_handler). --export([init/3]). +-export([init/2]). -export([info/3]). --export([terminate/3]). -init(_Transport, Req, []) -> +init(Req, Opts) -> Headers = [{<<"content-type">>, <<"text/event-stream">>}], Req2 = cowboy_req:chunked_reply(200, Headers, Req), erlang:send_after(1000, self(), {message, "Tick"}), - {loop, Req2, undefined, 5000}. + {long_polling, Req2, Opts, 5000}. info({message, Msg}, Req, State) -> cowboy_req:chunk(["id: ", id(), "\ndata: ", Msg, "\n\n"], Req), erlang:send_after(1000, self(), {message, "Tick"}), {loop, Req, State}. -terminate(_Reason, _Req, _State) -> - ok. - id() -> {Mega, Sec, Micro} = erlang:now(), Id = (Mega * 1000000 + Sec) * 1000000 + Micro, diff --git a/examples/hello_world/src/toppage_handler.erl b/examples/hello_world/src/toppage_handler.erl index 1b82cb2..18a6343 100644 --- a/examples/hello_world/src/toppage_handler.erl +++ b/examples/hello_world/src/toppage_handler.erl @@ -3,18 +3,14 @@ %% @doc Hello world handler. -module(toppage_handler). --export([init/3]). +-export([init/2]). -export([handle/2]). --export([terminate/3]). -init(_Type, Req, []) -> - {ok, Req, undefined}. +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> Req2 = cowboy_req:reply(200, [ {<<"content-type">>, <<"text/plain">>} ], <<"Hello world!">>, Req), {ok, Req2, State}. - -terminate(_Reason, _Req, _State) -> - ok. diff --git a/examples/rest_basic_auth/src/toppage_handler.erl b/examples/rest_basic_auth/src/toppage_handler.erl index f5544f8..59c5888 100644 --- a/examples/rest_basic_auth/src/toppage_handler.erl +++ b/examples/rest_basic_auth/src/toppage_handler.erl @@ -3,13 +3,13 @@ %% @doc Handler with basic HTTP authorization. -module(toppage_handler). --export([init/3]). +-export([init/2]). -export([content_types_provided/2]). -export([is_authorized/2]). -export([to_text/2]). -init(_Transport, _Req, []) -> - {upgrade, protocol, cowboy_rest}. +init(Req, Opts) -> + {rest, Req, Opts}. is_authorized(Req, State) -> case cowboy_req:parse_header(<<"authorization">>, Req) of diff --git a/examples/rest_hello_world/src/toppage_handler.erl b/examples/rest_hello_world/src/toppage_handler.erl index c41cf91..5b0dfc8 100644 --- a/examples/rest_hello_world/src/toppage_handler.erl +++ b/examples/rest_hello_world/src/toppage_handler.erl @@ -3,14 +3,14 @@ %% @doc Hello world handler. -module(toppage_handler). --export([init/3]). +-export([init/2]). -export([content_types_provided/2]). -export([hello_to_html/2]). -export([hello_to_json/2]). -export([hello_to_text/2]). -init(_Transport, _Req, []) -> - {upgrade, protocol, cowboy_rest}. +init(Req, Opts) -> + {rest, Req, Opts}. content_types_provided(Req, State) -> {[ diff --git a/examples/rest_pastebin/src/toppage_handler.erl b/examples/rest_pastebin/src/toppage_handler.erl index 506fc82..89fd786 100644 --- a/examples/rest_pastebin/src/toppage_handler.erl +++ b/examples/rest_pastebin/src/toppage_handler.erl @@ -4,7 +4,7 @@ -module(toppage_handler). %% Standard callbacks. --export([init/3]). +-export([init/2]). -export([allowed_methods/2]). -export([content_types_provided/2]). -export([content_types_accepted/2]). @@ -15,11 +15,9 @@ -export([paste_html/2]). -export([paste_text/2]). -init(_Transport, _Req, []) -> - % For the random number generator: - {X, Y, Z} = now(), - random:seed(X, Y, Z), - {upgrade, protocol, cowboy_rest}. +init(Req, Opts) -> + random:seed(now()), + {rest, Req, Opts}. allowed_methods(Req, State) -> {[<<"GET">>, <<"POST">>], Req, State}. diff --git a/examples/rest_stream_response/src/toppage_handler.erl b/examples/rest_stream_response/src/toppage_handler.erl index 71e0d50..6c66d21 100644 --- a/examples/rest_stream_response/src/toppage_handler.erl +++ b/examples/rest_stream_response/src/toppage_handler.erl @@ -3,16 +3,12 @@ %% @doc Streaming handler. -module(toppage_handler). --export([init/3]). --export([rest_init/2]). +-export([init/2]). -export([content_types_provided/2]). -export([streaming_csv/2]). -init(_Transport, _Req, _Table) -> - {upgrade, protocol, cowboy_rest}. - -rest_init(Req, Table) -> - {ok, Req, Table}. +init(Req, Table) -> + {rest, Req, Table}. content_types_provided(Req, State) -> {[ diff --git a/examples/ssl_hello_world/src/toppage_handler.erl b/examples/ssl_hello_world/src/toppage_handler.erl index 80fe1d5..18a6343 100644 --- a/examples/ssl_hello_world/src/toppage_handler.erl +++ b/examples/ssl_hello_world/src/toppage_handler.erl @@ -3,18 +3,14 @@ %% @doc Hello world handler. -module(toppage_handler). --export([init/3]). +-export([init/2]). -export([handle/2]). --export([terminate/3]). -init(_Transport, Req, []) -> - {ok, Req, undefined}. +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> Req2 = cowboy_req:reply(200, [ {<<"content-type">>, <<"text/plain">>} ], <<"Hello world!">>, Req), {ok, Req2, State}. - -terminate(_Reason, _Req, _State) -> - ok. diff --git a/examples/upload/src/upload_handler.erl b/examples/upload/src/upload_handler.erl index 0895d78..63cda96 100644 --- a/examples/upload/src/upload_handler.erl +++ b/examples/upload/src/upload_handler.erl @@ -1,12 +1,13 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Upload handler. -module(upload_handler). --behaviour(cowboy_http_handler). --export([init/3]). +-export([init/2]). -export([handle/2]). --export([terminate/3]). -init(_, Req, _Opts) -> - {ok, Req, undefined}. +init(Req, Opts) -> + {http, Req, Opts}. handle(Req, State) -> {ok, Headers, Req2} = cowboy_req:part(Req), @@ -16,6 +17,3 @@ handle(Req, State) -> io:format("Received file ~p of content-type ~p as follow:~n~p~n~n", [Filename, ContentType, Data]), {ok, Req3, State}. - -terminate(_Reason, _Req, _State) -> - ok. diff --git a/examples/web_server/src/directory_handler.erl b/examples/web_server/src/directory_handler.erl index ed342b5..5863bfa 100644 --- a/examples/web_server/src/directory_handler.erl +++ b/examples/web_server/src/directory_handler.erl @@ -4,8 +4,7 @@ -module(directory_handler). %% REST Callbacks --export([init/3]). --export([rest_init/2]). +-export([init/2]). -export([allowed_methods/2]). -export([resource_exists/2]). -export([content_types_provided/2]). @@ -14,11 +13,8 @@ -export([list_json/2]). -export([list_html/2]). -init(_Transport, _Req, _Paths) -> - {upgrade, protocol, cowboy_rest}. - -rest_init(Req, Paths) -> - {ok, Req, Paths}. +init(Req, Paths) -> + {rest, Req, Paths}. allowed_methods(Req, State) -> {[<<"GET">>], Req, State}. diff --git a/examples/websocket/src/ws_handler.erl b/examples/websocket/src/ws_handler.erl index bbbf716..18f9526 100644 --- a/examples/websocket/src/ws_handler.erl +++ b/examples/websocket/src/ws_handler.erl @@ -1,18 +1,12 @@ -module(ws_handler). --behaviour(cowboy_websocket_handler). --export([init/3]). --export([websocket_init/3]). +-export([init/2]). -export([websocket_handle/3]). -export([websocket_info/3]). --export([websocket_terminate/3]). -init({tcp, http}, _Req, _Opts) -> - {upgrade, protocol, cowboy_websocket}. - -websocket_init(_TransportName, Req, _Opts) -> +init(Req, Opts) -> erlang:start_timer(1000, self(), <<"Hello!">>), - {ok, Req, undefined_state}. + {ws, Req, Opts}. websocket_handle({text, Msg}, Req, State) -> {reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State}; @@ -24,6 +18,3 @@ websocket_info({timeout, _Ref, Msg}, Req, State) -> {reply, {text, Msg}, Req, State}; websocket_info(_Info, Req, State) -> {ok, Req, State}. - -websocket_terminate(_Reason, _Req, _State) -> - ok. -- cgit v1.2.3