Documentation ¶
Overview ¶
Package httputils provides utility functions for HTTP servers.
Index ¶
- Constants
- func AcceptsContentType(r *http.Request, contentType string) bool
- func Atexit(f func())
- func ContainsContentType(r *http.Request, contentType string) bool
- func ExitWithStatus()
- func FileDownloadServer(root http.FileSystem) http.Handler
- func FileStaticServer(root http.FileSystem) http.Handler
- func HasDirPath(path string) bool
- func HasFilePath(path string) bool
- func LogErrorf(format string, args ...interface{})
- func LogFatalf(format string, args ...interface{})
- func LogPrintFatalf(format string, args ...interface{})
- func MakeGzipHandler(handler http.HandlerFunc) http.HandlerFunc
- func MakeWelcomeHandler(name, version string) http.HandlerFunc
- func ServeFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, path string)
- func SetExitStatus(status int)
- type Server
- func (srv *Server) Listen() error
- func (srv *Server) ListenAndServe() error
- func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error
- func (srv *Server) ListenAndServeTLSWithConfig(config *tls.Config) error
- func (srv *Server) ListenTLS(certFile, keyFile string) error
- func (srv *Server) ListenTLSWithConfig(config *tls.Config) error
- func (srv *Server) Start() error
- func (srv *Server) Stop() error
Constants ¶
const WelcomePath = "/welcome"
The conventional path at which the handler returned by MakeWelcomeHandler should be mounted.
Variables ¶
This section is empty.
Functions ¶
func AcceptsContentType ¶
AcceptsContentType returns true if contentType would be an acceptable response to r, otherwise false.
As such, it is only suitable for endpoints which serve a fixed content type.
func Atexit ¶
func Atexit(f func())
Atexit saves f to be run when ExitWithStatus is called.
This function is not guaranteed to be threadsafe.
func ContainsContentType ¶
ContainsContentType returns true if the requests content type matches contentType, otherwise false.
Note that contentType may be a wildcard.
func ExitWithStatus ¶
func ExitWithStatus()
ExitWithStatus calls os.Exit with the status provided to SetExitStatus, calling any callback functions set using Atexit.
This function is threadsafe.
func FileDownloadServer ¶
func FileDownloadServer(root http.FileSystem) http.Handler
FileDownloadServer returns a handler that serves HTTP requests with the contents of the file system rooted at root and file download headers set.
To use the operating system's file system implementation, use http.Dir:
http.Handle("/", http.FileStaticServer(http.Dir("/tmp")))
func FileStaticServer ¶
func FileStaticServer(root http.FileSystem) http.Handler
FileStaticServer returns a handler that serves HTTP requests with the contents of the file system rooted at root and far future caching headers set.
To use the operating system's file system implementation, use http.Dir:
http.Handle("/", http.FileStaticServer(http.Dir("/tmp")))
func HasDirPath ¶
HasDirPath returns true if path is openable, stat-able, and a directory.
func HasFilePath ¶
HasFilePath returns true if path is openable and stat-able, otherwise false.
func LogErrorf ¶
func LogErrorf(format string, args ...interface{})
LogErrorf logs its arguments to the standard logger, and sets the exit status of ExitWithStatus to 1.
func LogFatalf ¶
func LogFatalf(format string, args ...interface{})
LogFatalf logs its arguments to the standard logger and exits immediately.
func LogPrintFatalf ¶
func LogPrintFatalf(format string, args ...interface{})
LogPrintFatalf logs its arguments to the standard logger as well as the standard outout and exits immediately.
func MakeGzipHandler ¶
func MakeGzipHandler(handler http.HandlerFunc) http.HandlerFunc
MakeGzipHandler wraps handler such that its output will be compressed according to what the client supports.
func MakeWelcomeHandler ¶
func MakeWelcomeHandler(name, version string) http.HandlerFunc
MakeWelcomeHandler returns an HTTP handler which renders a JSON response containing the provided application name and version in the following format:
{ "<name>": "Welcome", "version": "<version>" }
func ServeFile ¶
func ServeFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, path string)
ServeFile responds to w with the contents of path within fs.
func SetExitStatus ¶
func SetExitStatus(status int)
SetExitStatus sets a process exit status which will be used by ExitWithStatus.
This function is threadsafe.
Types ¶
type Server ¶
Provide a HTTP server implementation which can listen on TPC and Unix Domain sockets.
func (*Server) ListenAndServe ¶
ListenAndServe binds sockets according to the configuration of srv and blocks until the socket closes or an exit signal is received.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS binds sockets according to the configuration of srv and blocks until the socket closes or an exit signal is received.
func (*Server) ListenAndServeTLSWithConfig ¶
ListenAndServeTLSWithConfig binds sockets according to the provided TLS config and blocks until the socket closes or an exit signal is received.
func (*Server) ListenTLSWithConfig ¶
ListenAndServeTLSWithConfig binds sockets according to the provided TLS config and blocks until the socket closes or an exit signal is received.