We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Handler

Now that we've got a lot of the boilerplate stuff out of the way, let's define our handler function. This will allow the users of our server package to define their own logic for handling requests. Here's the function signature we're going to use:

type Handler func(w io.Writer, req *request.Request) *HandlerError

Remember, the official Go stdlib uses this:

type HandlerFunc func(w http.ResponseWriter, r *http.Request)

The main differences between ours and the official one are:

  • We'll use the more general io.Writer instead of http.ResponseWriter
  • We'll use our own request.Request type that we just built.
  • We'll return a *HandlerError. It's just a structured error type that makes it easy to always return a proper status code and error message.

Assignment

Remember, our Handler is responsible for reporting errors or writing the body. Our server implementation takes care of the rest for now.

Run and submit the CLI tests.