httputils

package module
v0.0.0-...-51d9aac Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2016 License: BSD-3-Clause Imports: 18 Imported by: 2

README

httputils

Go support library for HTTP servers.

Installation

$ go get github.com/strukturag/httputils

Run unit tests

$ go test -v

Contributing

  1. "Fork".
  2. Make a feature branch.
  3. Make changes.
  4. Do your commits (run go fmt before commit).
  5. Send "pull request".

License

httputils uses a BSD-style license, see our LICENSE file.

Documentation

Overview

Package httputils provides utility functions for HTTP servers.

Index

Constants

View Source
const WelcomePath = "/welcome"

The conventional path at which the handler returned by MakeWelcomeHandler should be mounted.

Variables

This section is empty.

Functions

func AcceptsContentType

func AcceptsContentType(r *http.Request, contentType string) bool

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

func ContainsContentType(r *http.Request, contentType string) bool

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

func HasDirPath(path string) bool

HasDirPath returns true if path is openable, stat-able, and a directory.

func HasFilePath

func HasFilePath(path string) bool

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

type Server struct {
	http.Server
	*log.Logger
	// contains filtered or unexported fields
}

Provide a HTTP server implementation which can listen on TPC and Unix Domain sockets.

func (*Server) Listen

func (srv *Server) Listen() error

Listen binds sockets according to the configuration of srv.

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

ListenAndServe binds sockets according to the configuration of srv and blocks until the socket closes or an exit signal is received.

func (*Server) ListenAndServeTLS

func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS binds sockets according to the configuration of srv and blocks until the socket closes or an exit signal is received.

func (*Server) ListenAndServeTLSWithConfig

func (srv *Server) ListenAndServeTLSWithConfig(config *tls.Config) error

ListenAndServeTLSWithConfig binds sockets according to the provided TLS config and blocks until the socket closes or an exit signal is received.

func (*Server) ListenTLS

func (srv *Server) ListenTLS(certFile, keyFile string) error

ListenTLS binds sockets according to the configuration of srv.

func (*Server) ListenTLSWithConfig

func (srv *Server) ListenTLSWithConfig(config *tls.Config) error

ListenAndServeTLSWithConfig binds sockets according to the provided TLS config and blocks until the socket closes or an exit signal is received.

func (*Server) Start

func (srv *Server) Start() error

Start runs a server whose sockets were previously bound by calling Listen, ListenTLS, or ListenTLSWithConfig and waits for its socket to close or Stop to be called.

Note that signals are not handled by the server when started in this manner, the caller should do so as needed.

func (*Server) Stop

func (srv *Server) Stop() error

Stop closes a previously started server.

Jump to

Keyboard shortcuts

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