Files
x/net/http/status_handler.go
Colin Henry 54aae5f242
All checks were successful
Go / build (1.23) (push) Successful in 3m51s
big updates: tests, bug fixed, documentation. oh my
2026-01-03 15:53:50 -08:00

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)
)