httputil

package
v0.0.0-...-63c1e96 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: GPL-3.0 Imports: 9 Imported by: 2

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 SchemeHost

func SchemeHost(r *http.Request) string

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

type ModTimeFS

type ModTimeFS struct {
	fs.FS   // must be embed.FS or fs.Sub(embed.FS, ...)
	ModTime time.Time
}

A ModTimeFS is an embed.FS with a given modification time. Else embed.FS returns an empty ModTime, resulting in http.FileServer not setting a Last-Modified header.

func (ModTimeFS) Open

func (fs ModTimeFS) Open(name string) (fs.File, error)

Jump to

Keyboard shortcuts

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