my404

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: 3 Imported by: 0

Documentation

Overview

Package my4040 provides support for setting customized http 404 handler.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(h http.Handler, handle404 func(w io.Writer, r *http.Request)) http.Handler

Handler returns a http.Handler which calls handle404 instead of w.Write to write the response body after a 404 status code was written to w.

Example
package main

import (
	"fmt"
	"io"
	"log"
	"net/http"

	"github.com/mkch/burrow/my404"
)

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

	handle404 := func(w io.Writer, r *http.Request) {
		w.Write([]byte(fmt.Sprintf("404 Gopher is not here: %s", r.URL)))
	}
	handler := my404.Handler(http.DefaultServeMux, handle404)
	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

This section is empty.

Jump to

Keyboard shortcuts

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