httputil

package
v0.0.0-...-da9e6da Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2024 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package httputil provides an easy way to chain handlers and a server with timeouts and graceful shutdown.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Forbidden

func Forbidden() http.Handler

func InternalServerError

func InternalServerError(err error) http.Handler

InternalServerError returns an handler which writes status code 500 and logs the error string.

func ListenAndServe

func ListenAndServe(addr string, handler http.Handler, stop chan<- os.Signal) func()

ListenAndServe listens on the TCP network address addr and starts an HTTP server with timeouts. It returns a shutdown function for the server.

Example:

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)

shutdown := httputil.ListenAndServe(":8080", router, stop)
defer shutdown()

<-stop

func Origin

func Origin(r *http.Request) string

Origin returns the scheme plus host (including port, if available). The scheme is https, except when the host looks like localhost or a Tor hidden service.

If you use nginx as a reverse proxy, make sure you have set "proxy_set_header Host $host;" besides proxy_pass in your configuration.

Types

type HandlerFunc

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

A HandlerFunc can return the next handler or nil, which provides easy chaining:

mux.Handle("/", httputil.HandlerFunc(func(w http.ResponseWriter, r *http.Request) http.Handler {
	// do something, then return next handler or nil
}))

You can return http.NotFoundHandler and http.RedirectHandler for not found errors and redirects.

func (HandlerFunc) ServeHTTP

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

Jump to

Keyboard shortcuts

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