aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/guide/rest_handlers.ezdoc
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-09-26 15:58:44 +0300
committerLoïc Hoguin <[email protected]>2014-09-26 15:58:44 +0300
commit5ce4c2bfb40ecc4b687a2941e612025a1c4ff913 (patch)
tree7094d5f9d92c9c3bac1a60ca4b4922ba035b219d /doc/src/guide/rest_handlers.ezdoc
parentfd37fad592fc96a384bcd060696194f5fe074f6f (diff)
downloadcowboy-5ce4c2bfb40ecc4b687a2941e612025a1c4ff913.tar.gz
cowboy-5ce4c2bfb40ecc4b687a2941e612025a1c4ff913.tar.bz2
cowboy-5ce4c2bfb40ecc4b687a2941e612025a1c4ff913.zip
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.
Diffstat (limited to 'doc/src/guide/rest_handlers.ezdoc')
-rw-r--r--doc/src/guide/rest_handlers.ezdoc33
1 files changed, 12 insertions, 21 deletions
diff --git a/doc/src/guide/rest_handlers.ezdoc b/doc/src/guide/rest_handlers.ezdoc
index ee3e5aa..294392a 100644
--- a/doc/src/guide/rest_handlers.ezdoc
+++ b/doc/src/guide/rest_handlers.ezdoc
@@ -8,18 +8,20 @@ The REST handler is the recommended way to handle requests.
:: Initialization
-First, the `init/3` callback is called. This callback is common
+First, the `init/2` callback is called. This callback is common
to all handlers. To use REST for the current request, this function
-must return an `upgrade` tuple.
+must return a `rest` tuple.
``` erlang
-init({tcp, http}, Req, Opts) ->
- {upgrade, protocol, cowboy_rest}.
+init(Req, Opts) ->
+ {rest, Req, Opts}.
```
Cowboy will then switch to the REST protocol and start executing
-the state machine, starting from `rest_init/2` if it's defined,
-and ending with `rest_terminate/2` also if defined.
+the state machine.
+
+After reaching the end of the flowchart, the `terminate/3` callback
+will be called if it is defined.
:: Methods
@@ -36,28 +38,17 @@ on what other defined callbacks return. The various flowcharts
in the next chapter should be a useful to determine which callbacks
you need.
-When the request starts being processed, Cowboy will call the
-`rest_init/2` function if it is defined, with the Req object
-and the handler options as arguments. This function must return
-`{ok, Req, State}` where `State` is the handler's state that all
-subsequent callbacks will receive.
-
-At the end of every request, the special callback `rest_terminate/2`
-will be called if it is defined. It cannot be used to send a reply,
-and must always return `ok`.
+All callbacks take two arguments, the Req object and the State,
+and return a three-element tuple of the form `{Value, Req, State}`.
-All other callbacks are resource callbacks. They all take two
-arguments, the Req object and the State, and return a three-element
-tuple of the form `{Value, Req, State}`.
+All callbacks can also return `{halt, Req, State}` to stop execution
+of the request.
The following table summarizes the callbacks and their default values.
If the callback isn't defined, then the default value will be used.
Please look at the flowcharts to find out the result of each return
value.
-All callbacks can also return `{halt, Req, State}` to stop execution
-of the request, at which point `rest_terminate/2` will be called.
-
In the following table, "skip" means the callback is entirely skipped
if it is undefined, moving directly to the next step. Similarly,
"none" means there is no default value for this callback.