Documentation ¶
Overview ¶
Tiger Tonic [1] is a Go framework for building JSON web services inspired by Dropwizard [2]. If HTML is your game, this will hurt a little.
Like the Go language itself, Tiger Tonic strives to keep features orthogonal. It defers what it can to the Go standard library, go-metrics [3], and a few other packages.
[1] <https://github.com/rcrowley/go-tigertonic> [2] <http://dropwizard.codahale.com> [3] <https://github.com/rcrowley/go-metrics>
Index ¶
- Constants
- Variables
- func Configure(pathname string, i interface{}) error
- func ConfigureJSON(pathname string, i interface{}) error
- func Context(r *http.Request) interface{}
- func RandomBase62Bytes(ii int) []byte
- func RandomBase62String(ii int) string
- type Accepted
- type ApacheLogger
- type BadGateway
- type BadRequest
- type CORSBuilder
- type CORSHandler
- type Conflict
- type ContextHandler
- type Continue
- type Counter
- type Created
- type ExpectationFailed
- type FirstHandler
- type Forbidden
- type Found
- type GatewayTimeout
- type Gone
- type HTTPEquivError
- type HTTPVersionNotSupported
- type HostServeMux
- func (mux HostServeMux) Handle(hostname string, handler http.Handler)
- func (mux HostServeMux) HandleFunc(hostname string, handler func(http.ResponseWriter, *http.Request))
- func (mux HostServeMux) Handler(r *http.Request) (http.Handler, string)
- func (mux HostServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type InternalServerError
- type LengthRequired
- type Logger
- type Marshaler
- type MarshalerError
- type MethodNotAllowed
- type MethodNotAllowedHandler
- type MovedPermanently
- type MultipleChoices
- type NamedError
- type NoContent
- type NonAuthoritativeInfo
- type NotAcceptable
- type NotFound
- type NotFoundHandler
- type NotImplemented
- type NotModified
- type OK
- type PartialContent
- type PaymentRequired
- type PreconditionFailed
- type ProxyAuthRequired
- type Redactor
- type RequestEntityTooLarge
- type RequestID
- type RequestIDCreator
- type RequestTimeout
- type RequestURITooLong
- type RequestedRangeNotSatisfiable
- type ResetContent
- type SeeOther
- type Server
- type ServiceUnavailable
- type SwitchingProtocols
- type Teapot
- type TemporaryRedirect
- type Timer
- type TrieServeMux
- func (mux *TrieServeMux) Handle(method, pattern string, handler http.Handler)
- func (mux *TrieServeMux) HandleFunc(method, pattern string, handler func(http.ResponseWriter, *http.Request))
- func (mux *TrieServeMux) HandleNamespace(namespace string, handler http.Handler)
- func (mux *TrieServeMux) Handler(r *http.Request) (http.Handler, string)
- func (mux *TrieServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type Unauthorized
- type UnsupportedMediaType
- type UseProxy
- Bugs
Constants ¶
const CORSAllowHeaders string = "Access-Control-Allow-Headers"
const CORSAllowMethods string = "Access-Control-Allow-Methods"
const CORSAllowOrigin string = "Access-Control-Allow-Origin"
const CORSRequestHeaders string = "Access-Control-Request-Headers"
const CORSRequestMethod string = "Access-Control-Request-Method"
const CORSRequestOrigin string = "Origin"
Variables ¶
var ConfigExt = map[string]func(string, interface{}) error{ ".json": ConfigureJSON, }
ConfigExt maps all known configuration file extensions to their read-and-unmarshal functions.
var SnakeCaseHTTPEquivErrors bool
SnakeCaseHTTPEquivErrors being true will cause tigertonic.HTTPEquivError error responses to be written as (for example) "not_found" rather than "tigertonic.NotFound".
Functions ¶
func Configure ¶
Configure delegates reading and unmarshaling of the given configuration file to the appropriate function from ConfigExt. For convenient use with the flags package, an empty pathname is not considered an error.
func ConfigureJSON ¶
ConfigureJSON reads the given configuration file and unmarshals the JSON found into the given configuration structure. For convenient use with the flags package, an empty pathname is not considered an error.
func Context ¶
Context returns the request context as an interface{} given a pointer to the request itself.
func RandomBase62Bytes ¶
func RandomBase62String ¶
Types ¶
type ApacheLogger ¶
ApacheLogger is an http.Handler that logs requests and responses in the Apache combined log format.
func ApacheLogged ¶
func ApacheLogged(handler http.Handler) *ApacheLogger
ApacheLogged returns an http.Handler that logs requests and responses in the Apache combined log format.
func (*ApacheLogger) ServeHTTP ¶
func (al *ApacheLogger) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP wraps the http.Request and http.ResponseWriter to log to standard output and pass through to the underlying http.Handler.
type BadGateway ¶
type BadGateway struct {
// contains filtered or unexported fields
}
func (BadGateway) Status ¶
func (err BadGateway) Status() int
type BadRequest ¶
type BadRequest struct {
// contains filtered or unexported fields
}
func (BadRequest) Status ¶
func (err BadRequest) Status() int
type CORSBuilder ¶
CRSBuilder facilitates the application of the same set of CORS rules to a number of endpoints. One would use CORSBuilder.Build() the same way one might wrap a handler in a call to Timed() or Logged().
func NewCORSBuilder ¶
func NewCORSBuilder() *CORSBuilder
func (*CORSBuilder) AddAllowedHeaders ¶
func (self *CORSBuilder) AddAllowedHeaders(headers ...string) *CORSBuilder
func (*CORSBuilder) Build ¶
func (self *CORSBuilder) Build(handler http.Handler) *CORSHandler
func (*CORSBuilder) SetAllowedOrigin ¶
func (self *CORSBuilder) SetAllowedOrigin(origin string) *CORSBuilder
SetAllowedOrigin sets the domain for which cross-origin requests are allowed
type CORSHandler ¶
CORSHandler wraps an http.Handler while correctly handling CORS related functionality, such as Origin headers. It also allows tigertonic core to correctly respond to OPTIONS headers for CORS-enabled endpoints
func (*CORSHandler) ServeHTTP ¶
func (self *CORSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type ContextHandler ¶
type ContextHandler struct {
// contains filtered or unexported fields
}
ContextHandler is an http.Handler that associates a context object of any type with each request it handles.
func WithContext ¶
func WithContext(handler http.Handler, i interface{}) *ContextHandler
WithContext wraps an http.Handler and associates a new context object of the same type as the second parameter with each request it handles. You must wrap any handler that uses Context or the four-parameter form of Marshaled in WithContext.
func (*ContextHandler) ServeHTTP ¶
func (ch *ContextHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP adds and removes the per-request context and calls the wrapped http.Handler in between.
type Counter ¶
type Counter struct { metrics.Counter // contains filtered or unexported fields }
Counter is an http.Handler that counts requests via go-metrics.
type ExpectationFailed ¶
type ExpectationFailed struct {
// contains filtered or unexported fields
}
func (ExpectationFailed) Status ¶
func (err ExpectationFailed) Status() int
type FirstHandler ¶
FirstHandler is an http.Handler that, for each handler in its slice of handlers, calls ServeHTTP until the first one that calls w.WriteHeader.
func First ¶
func First(handlers ...http.Handler) FirstHandler
First returns an http.Handler that, for each handler in its slice of handlers, calls ServeHTTP until the first one that calls w.WriteHeader.
func HTTPBasicAuth ¶
HTTPBasicAuth returns an http.Handler that conditionally calls another http.Handler if the request includes and Authorization header with a username and password that appear in the map of credentials. Otherwise, respond 401 Unauthorized.
func If ¶
If returns an http.Handler that conditionally calls another http.Handler unless the given function returns an error. In that case, the error is used to create a plaintext or JSON response as dictated by the Accept header.
func (FirstHandler) ServeHTTP ¶
func (fh FirstHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP calls each handler in its slice of handlers until the first one that calls w.WriteHeader.
type GatewayTimeout ¶
type GatewayTimeout struct {
// contains filtered or unexported fields
}
func (GatewayTimeout) Status ¶
func (err GatewayTimeout) Status() int
type HTTPEquivError ¶
type HTTPVersionNotSupported ¶
type HTTPVersionNotSupported struct {
// contains filtered or unexported fields
}
func (HTTPVersionNotSupported) Status ¶
func (err HTTPVersionNotSupported) Status() int
type HostServeMux ¶
HostServeMux is an HTTP request multiplexer that implements http.Handler with an API similar to http.ServeMux. It is only sensitive to the hostname and doesn't even look at the rest of the request.
func NewHostServeMux ¶
func NewHostServeMux() HostServeMux
NewHostServeMux makes a new HostServeMux.
func (HostServeMux) Handle ¶
func (mux HostServeMux) Handle(hostname string, handler http.Handler)
Handle registers an http.Handler for the given hostname.
func (HostServeMux) HandleFunc ¶
func (mux HostServeMux) HandleFunc(hostname string, handler func(http.ResponseWriter, *http.Request))
HandleFunc registers a handler function for the given hostname.
func (HostServeMux) ServeHTTP ¶
func (mux HostServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP routes an HTTP request to the http.Handler registered for the requested hostname. It responds 404 if the hostname is not registered.
type InternalServerError ¶
type InternalServerError struct {
// contains filtered or unexported fields
}
func (InternalServerError) Status ¶
func (err InternalServerError) Status() int
type LengthRequired ¶
type LengthRequired struct {
// contains filtered or unexported fields
}
func (LengthRequired) Status ¶
func (err LengthRequired) Status() int
type Logger ¶
type Logger struct { *log.Logger RequestIDCreator RequestIDCreator // contains filtered or unexported fields }
Logger is an http.Handler that logs requests and responses, complete with paths, statuses, headers, and bodies. Sensitive information may be redacted by a user-defined function.
func Logged ¶
Logged returns an http.Handler that logs requests and responses, complete with paths, statuses, headers, and bodies. Sensitive information may be redacted by a user-defined function.
func (*Logger) Print ¶
func (l *Logger) Print(v ...interface{})
Print is identical to log.Logger's Print but uses our overridden Output.
type Marshaler ¶
type Marshaler struct {
// contains filtered or unexported fields
}
Marshaler is an http.Handler that unmarshals JSON input, handles the request via a function, and marshals JSON output. It refuses to answer requests without an Accept header that includes the application/json content type.
func Marshaled ¶
func Marshaled(i interface{}) *Marshaler
Marshaled returns an http.Handler that implements its ServeHTTP method by calling the given function, the signature of which must be
func(*url.URL, http.Header, *Request) (int, http.Header, *Response)
where Request and Response may be any struct type of your choosing.
type MarshalerError ¶
type MarshalerError string
MarshalerError is the response body for some 500 responses and panics when a handler function is not suitable.
func NewMarshalerError ¶
func NewMarshalerError(format string, args ...interface{}) MarshalerError
func (MarshalerError) Error ¶
func (e MarshalerError) Error() string
type MethodNotAllowed ¶
type MethodNotAllowed struct {
// contains filtered or unexported fields
}
func (MethodNotAllowed) Status ¶
func (err MethodNotAllowed) Status() int
type MethodNotAllowedHandler ¶
type MethodNotAllowedHandler struct {
// contains filtered or unexported fields
}
MethodNotAllowedHandler responds 405 to every request with an Allow header and possibly with a JSON body.
func (MethodNotAllowedHandler) ServeHTTP ¶
func (h MethodNotAllowedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type MovedPermanently ¶
type MovedPermanently struct {
// contains filtered or unexported fields
}
func (MovedPermanently) Status ¶
func (err MovedPermanently) Status() int
type MultipleChoices ¶
type MultipleChoices struct {
// contains filtered or unexported fields
}
func (MultipleChoices) Status ¶
func (err MultipleChoices) Status() int
type NamedError ¶
type NonAuthoritativeInfo ¶
type NonAuthoritativeInfo struct {
// contains filtered or unexported fields
}
func (NonAuthoritativeInfo) Status ¶
func (err NonAuthoritativeInfo) Status() int
type NotAcceptable ¶
type NotAcceptable struct {
// contains filtered or unexported fields
}
func (NotAcceptable) Status ¶
func (err NotAcceptable) Status() int
type NotFoundHandler ¶
type NotFoundHandler struct{}
NotFoundHandler responds 404 to every request, possibly with a JSON body.
func (NotFoundHandler) ServeHTTP ¶
func (NotFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type NotImplemented ¶
type NotImplemented struct {
// contains filtered or unexported fields
}
func (NotImplemented) Status ¶
func (err NotImplemented) Status() int
type NotModified ¶
type NotModified struct {
// contains filtered or unexported fields
}
func (NotModified) Status ¶
func (err NotModified) Status() int
type PartialContent ¶
type PartialContent struct {
// contains filtered or unexported fields
}
func (PartialContent) Status ¶
func (err PartialContent) Status() int
type PaymentRequired ¶
type PaymentRequired struct {
// contains filtered or unexported fields
}
func (PaymentRequired) Status ¶
func (err PaymentRequired) Status() int
type PreconditionFailed ¶
type PreconditionFailed struct {
// contains filtered or unexported fields
}
func (PreconditionFailed) Status ¶
func (err PreconditionFailed) Status() int
type ProxyAuthRequired ¶
type ProxyAuthRequired struct {
// contains filtered or unexported fields
}
func (ProxyAuthRequired) Status ¶
func (err ProxyAuthRequired) Status() int
type Redactor ¶
A Redactor is a function that takes and returns a string. It is called to allow sensitive information to be redacted before it is logged.
type RequestEntityTooLarge ¶
type RequestEntityTooLarge struct {
// contains filtered or unexported fields
}
func (RequestEntityTooLarge) Status ¶
func (err RequestEntityTooLarge) Status() int
type RequestID ¶
type RequestID string
A unique RequestID is given to each request and is included with each line of each log entry.
func NewRequestID ¶
func NewRequestID() RequestID
NewRequestID returns a new 16-character random RequestID.
type RequestIDCreator ¶
A RequestIDCreator is a function that takes a request and returns a unique RequestID for it.
type RequestTimeout ¶
type RequestTimeout struct {
// contains filtered or unexported fields
}
func (RequestTimeout) Status ¶
func (err RequestTimeout) Status() int
type RequestURITooLong ¶
type RequestURITooLong struct {
// contains filtered or unexported fields
}
func (RequestURITooLong) Status ¶
func (err RequestURITooLong) Status() int
type RequestedRangeNotSatisfiable ¶
type RequestedRangeNotSatisfiable struct {
// contains filtered or unexported fields
}
func (RequestedRangeNotSatisfiable) Status ¶
func (err RequestedRangeNotSatisfiable) Status() int
type ResetContent ¶
type ResetContent struct {
// contains filtered or unexported fields
}
func (ResetContent) Status ¶
func (err ResetContent) Status() int
type Server ¶
Server is an http.Server with better defaults.
type ServiceUnavailable ¶
type ServiceUnavailable struct {
// contains filtered or unexported fields
}
func (ServiceUnavailable) Status ¶
func (err ServiceUnavailable) Status() int
type SwitchingProtocols ¶
type SwitchingProtocols struct {
// contains filtered or unexported fields
}
func (SwitchingProtocols) Status ¶
func (err SwitchingProtocols) Status() int
type TemporaryRedirect ¶
type TemporaryRedirect struct {
// contains filtered or unexported fields
}
func (TemporaryRedirect) Status ¶
func (err TemporaryRedirect) Status() int
type Timer ¶
type Timer struct { metrics.Timer // contains filtered or unexported fields }
Timer is an http.Handler that counts requests via go-metrics.
type TrieServeMux ¶
type TrieServeMux struct {
// contains filtered or unexported fields
}
TrieServeMux is an HTTP request multiplexer that implements http.Handler with an API similar to http.ServeMux. It is expanded to be sensitive to the HTTP method and treats URL patterns as patterns rather than simply prefixes.
Components of the URL pattern surrounded by braces (for example: "{foo}") match any string and create an entry for the string plus the string surrounded by braces in the query parameters (for example: "foo" and "{foo}").
func NewTrieServeMux ¶
func NewTrieServeMux() *TrieServeMux
NewTrieServeMux makes a new TrieServeMux.
func (*TrieServeMux) Handle ¶
func (mux *TrieServeMux) Handle(method, pattern string, handler http.Handler)
Handle registers an http.Handler for the given HTTP method and URL pattern.
func (*TrieServeMux) HandleFunc ¶
func (mux *TrieServeMux) HandleFunc(method, pattern string, handler func(http.ResponseWriter, *http.Request))
HandleFunc registers a handler function for the given HTTP method and URL pattern.
func (*TrieServeMux) HandleNamespace ¶
func (mux *TrieServeMux) HandleNamespace(namespace string, handler http.Handler)
HandleNamespace registers an http.Handler for the given URL namespace. The matching namespace is stripped from the URL before it is passed to the underlying http.Handler.
func (*TrieServeMux) Handler ¶
Handler returns the handler to use for the given HTTP request and mutates the querystring to add wildcards extracted from the URL.
Yes, it's bad that this mutates the request. On the other hand, this is a relatively standard interface and is most used in testing where behavior like this can be allowed.
func (*TrieServeMux) ServeHTTP ¶
func (mux *TrieServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP routes an HTTP request to the http.Handler registered for the URL pattern which matches the requested path. It responds 404 if there is no matching URL pattern and 405 if the requested HTTP method is not allowed.
type Unauthorized ¶
type Unauthorized struct {
// contains filtered or unexported fields
}
func (Unauthorized) Status ¶
func (err Unauthorized) Status() int
type UnsupportedMediaType ¶
type UnsupportedMediaType struct {
// contains filtered or unexported fields
}
func (UnsupportedMediaType) Status ¶
func (err UnsupportedMediaType) Status() int
Notes ¶
Bugs ¶
currently only handles Origin and header-related CORS headers. Early indication is that logic will be required for handling certain header types (the credential-related ones in particular) so we may need to come up with something more robust than just dragging an http.Header around