30 lines
979 B
Go
30 lines
979 B
Go
// Package http provides HTTP utility functions and handlers.
|
|
package http
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
// StatusHandler is an http.Handler that responds with a specific HTTP status code.
|
|
// Inspired by @matryer.
|
|
type StatusHandler int
|
|
|
|
// ServeHTTP writes the status code and its text representation to the response.
|
|
func (s StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
code := int(s)
|
|
w.WriteHeader(code)
|
|
_, _ = io.WriteString(w, http.StatusText(code))
|
|
}
|
|
|
|
var (
|
|
// NotFoundHandler returns HTTP 404 Not Found
|
|
NotFoundHandler = StatusHandler(http.StatusNotFound)
|
|
// NotImplementedHandler returns HTTP 501 Not Implemented
|
|
NotImplementedHandler = StatusHandler(http.StatusNotImplemented)
|
|
// NotLegalHandler returns HTTP 451 Unavailable For Legal Reasons
|
|
NotLegalHandler = StatusHandler(http.StatusUnavailableForLegalReasons)
|
|
// NotAllowedHandler returns HTTP 405 Method Not Allowed
|
|
NotAllowedHandler = StatusHandler(http.StatusMethodNotAllowed)
|
|
)
|