Documentation
¶
Index ¶
- func AssetServer(prefix, directory string) http.Handler
- func BasicAuth(username, password, realm string) func(http.Handler) http.Handler
- func CORS(policy CORSPolicy) func(http.Handler) http.Handler
- func Compose(chain ...interface{}) http.Handler
- func ContentSecurity(policy ContentPolicy) func(http.Handler) http.Handler
- func DataSize(str string) uint64
- func Hostname(host string) string
- func LimitBody(w http.ResponseWriter, r *http.Request, limit uint64)
- func RPCErrorWrite(w http.ResponseWriter, err *RPCError)
- func RPCHandler(limit uint64, reporter func(error), handler func(*RPCContext) interface{}) http.HandlerFunc
- func Record(h http.Handler, method, url string, headers map[string]string, payload string) *httptest.ResponseRecorder
- func Secure(w http.ResponseWriter, r *http.Request, allowInsecure, noFrontend bool, ...) bool
- func Security(allowInsecure, noFrontend bool, stsMaxAge time.Duration) func(http.Handler) http.Handler
- type BodyLimiter
- type CORSPolicy
- type ContentPolicy
- type RPCClient
- type RPCContext
- type RPCData
- type RPCError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssetServer ¶
AssetServer constructs an asset server handler that serves an asset directory on a specified path and serves the index file for not found paths which is needed to run single page applications like Ember.
func CORS ¶
func CORS(policy CORSPolicy) func(http.Handler) http.Handler
CORS returns a middleware for enforcing CORS.
func ContentSecurity ¶
func ContentSecurity(policy ContentPolicy) func(http.Handler) http.Handler
ContentSecurity returns a middleware for enforcing content security.
func DataSize ¶
DataSize parses human readable data sizes (e.g. 4K, 20M or 5G) and returns the amount of bytes they represent.
func Hostname ¶ added in v0.1.3
Hostname will return the hostname from the provided host string. This method should be used instead of net.SplitHostPort when attempting to clean the http/Request.Host attribute.
func LimitBody ¶
func LimitBody(w http.ResponseWriter, r *http.Request, limit uint64)
LimitBody will limit reading from the body of the supplied request to the specified amount of bytes. Earlier calls to LimitBody will be overwritten which essentially allows callers to increase the limit from a default limit.
func RPCErrorWrite ¶ added in v0.1.2
func RPCErrorWrite(w http.ResponseWriter, err *RPCError)
RPCErrorWrite will write the error to the specified response writer.
func RPCHandler ¶
func RPCHandler(limit uint64, reporter func(error), handler func(*RPCContext) interface{}) http.HandlerFunc
RPCHandler wraps a handler to simplify handling request and responses. The specified limit will be applied to the received request body. The handler is expected to return nil, JSON compatible responses, RPCError values or errors.
func Record ¶
func Record(h http.Handler, method, url string, headers map[string]string, payload string) *httptest.ResponseRecorder
Record will make a request against the specified handler and record the result.
func Secure ¶
func Secure(w http.ResponseWriter, r *http.Request, allowInsecure, noFrontend bool, stsMaxAge time.Duration) bool
Secure will enforce various common web security policies. It return whether the request is safe to be further processed. Subsequent handlers should update the headers with a more applicable content security policy.
Types ¶
type BodyLimiter ¶
type BodyLimiter struct {
io.ReadCloser
Limit uint64
Original io.ReadCloser
}
BodyLimiter wraps a io.ReadCloser and keeps a reference to the original.
type ContentPolicy ¶
ContentPolicy for defining content security.
func (ContentPolicy) String ¶
func (p ContentPolicy) String() string
String will encode the policy as a string.
type RPCClient ¶
type RPCClient struct {
// Base is the base URL of the JSON API endpoint.
Base string
// contains filtered or unexported fields
}
RPCClient is a reusable client for accessing remote JSON APIs.
type RPCContext ¶
type RPCContext struct {
// contains filtered or unexported fields
}
RPCContext allows handlers to interact with the request.
func (*RPCContext) Handle ¶
func (c *RPCContext) Handle(v interface{}, cb func() interface{}) interface{}
Handle will parse the request and yield control to the callback.
func (*RPCContext) Parse ¶
func (c *RPCContext) Parse(v interface{}) error
Parse will parse the request to the specified value.
func (*RPCContext) Request ¶
func (c *RPCContext) Request() *http.Request
Request will return the original request.
func (*RPCContext) ResponseWriter ¶
func (c *RPCContext) ResponseWriter() http.ResponseWriter
ResponseWriter will return the associated response writer.
type RPCError ¶
type RPCError struct {
// A unique identifier for this particular occurrence of the problem.
ID string `json:"id,omitempty"`
// An URL that leads to further details about this problem.
Link string `json:"link,omitempty"`
// The HTTP status code applicable to this problem.
Status int `json:"status,string,omitempty"`
// An application-specific error code.
Code string `json:"code,omitempty"`
// A short, human-readable summary of the problem.
Title string `json:"title,omitempty"`
// A human-readable explanation specific to this occurrence of the problem.
Detail string `json:"detail,omitempty"`
// A JSON pointer specifying the source of the error.
//
// See https://tools.ietf.org/html/rfc6901.
Source string `json:"source,omitempty"`
// Non-standard meta-information about the error.
Meta map[string]interface{} `json:"meta,omitempty"`
}
RPCError objects provide additional information about problems encountered while performing an RPC operation.
func RPCBadRequest ¶
RPCBadRequest returns a new bad request error with a source.
func RPCErrorFromStatus ¶
RPCErrorFromStatus will return an error that has been derived from the passed status code.
Note: If the passed status code is not a valid HTTP status code, an Internal Server RPCError status code will be used instead.
func RPCNotFound ¶
RPCNotFound returns a new not found error.