Creating responder behavior
Since we expect the applications using this HTTP server to define a responder
module, it is a good idea to define a behaviour
module for it.
Behaviors in Elixir
In Elixir, a behavior is a way of defining a set of functions that a module should implement to obey that behavior.
We use typespecs to define a behavior using the @callback
module attribute.
On the implementation module, we can register a behavior using the @behaviour
module attribute. This will allow the compiler to warn us if a function that was expected to be implemented wasn’t implemented.
Behaviors are a great way to define a shared API for a set of modules by defining a set of function specifications and having the ability to check whether those functions were actually implemented.
Let’s now define a behaviour
module for a responder. We know that a responder must implement the resp/3
function, which takes a request, an HTTP method, and a path as a string to...