statushook

package
v0.0.0-...-738e6fc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2021 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package statushook provides a utility to hook a http.Handler before the status code is written, giving the hook a chance to midify the response header, or replace the entire response.

A typical use case is to hook a serve handler to provide global refined 404 page:

Given a simple "foo" server:

http.HandleFunc("/foo",
		func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("This is foo."))
		})
log.Fatal(http.ListenAndServe("localhost:8181", handler))

We hook it liek this:

http.HandleFunc("/foo",
	func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("This is foo."))
	})

refined404Hook := func(code int, w http.ResponseWriter, r *http.Request) {
	if code == http.StatusNotFound {
		w.Write([]byte(fmt.Sprintf("404 Gohper is not here: %s", r.URL)))
	}
}
handler := statushook.Handler(http.DefaultServeMux, statushook.HookFunc(refined404Hook))
log.Fatal(http.ListenAndServe("localhost:8181", handler))

Then we will get the refined 404 page if we request http://localhost:8181/anything-except-foo:

404 Gohper is not here: /anything-except-foo

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(handler http.Handler, hook Hook) http.Handler

Handler function returns a wrapped http.Handler which calls hook.Hook() with the status code before the status code is written to response, giving hook.Hook() a chance to midify the response header, or replace the entire response. See the Hook interface for details.

Example
package main

import (
	"fmt"
	"github.com/mkch/burrow/statushook"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/foo",
		func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("This is foo."))
		})

	refined404Hook := func(code int, w http.ResponseWriter, r *http.Request) {
		if code == http.StatusNotFound {
			w.Write([]byte(fmt.Sprintf("404 Gohper is not here: %s", r.URL)))
		}
	}
	handler := statushook.Handler(http.DefaultServeMux, statushook.HookFunc(refined404Hook))
	log.Fatal(http.ListenAndServe("localhost:8181", handler))

	// Please access http://localhost:8181/anything-except-foo in your browser
	// to get the refined 404 page:
	//
	//		404 Gohper is not here: /anything-except-foo
}
Output:

Types

type Hook

type Hook interface {
	// Hook is called before a status code is written to w, tipically a call
	// to w.WriterHeader().
	// w can be used to write a different response to the client.
	// The original response will be completly discarded if w.Write() or
	// w.WriteHeader() is called in this function. The original response will be
	// written with the modified header if w.Header() is modified witout calling
	// w.Write() or w.WriteHeader().
	Hook(code int, w http.ResponseWriter, r *http.Request)
}

Objects implementing the Hook interface can be used by Handler function to hook http response.

type HookFunc

type HookFunc func(code int, w http.ResponseWriter, r *http.Request)

The HookFunc type is an adapter to allow the use of ordinary functions as Hook interface. If f is a function with the appropriate signature, HookFunc(f) is a Hook object that calls f.

func (HookFunc) Hook

func (f HookFunc) Hook(code int, w http.ResponseWriter, r *http.Request)

Hook calls f(code, w, r).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL