0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 12:40:25 +00:00
cowboy/guide/http_handlers.md
Loïc Hoguin 647e95aed1 Replace terminate/2 with terminate/3, adding a Reason
This should have been done a *long* time ago, back when I initially
added Websocket support. This is the first part of two in improving
loop handler support with regards to socket closure.

Reason may include: {normal, shutdown} for the most normal shutdown,
{normal, timeout} for a loop handler timeout shutdown, or {error, _}
if an error occured.
2013-01-22 02:34:18 +01:00

968 B

HTTP handlers

Purpose

HTTP handlers are the simplest Cowboy module to handle a request.

Usage

You need to implement three callbacks for HTTP handlers. The first, init/3, is common to all handlers. In the context of HTTP handlers this should be used for any initialization needs.

The second callback, handle/2, is where most of your code should be. As the name explains, this is where you handle the request.

The last callback, terminate/3, will be empty most of the time. It's used for any needed cleanup. If you used the process dictionary, timers, monitors then you most likely want to stop them in this callback, as Cowboy might end up reusing this process for subsequent requests. Please see the Internals chapter for more information.

Of course the general advice is to not use the process dictionary, and that any operation requiring reception of messages should be done in a loop handler, documented in its own chapter.