serve

package module
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: MIT Imports: 16 Imported by: 11

README

serve

Build Status Coverage Status GoDoc Release Go Report Card

A collection of basic tools for building Go based APIs.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBodyLimitExceeded = errors.New("body limit exceeded")

ErrBodyLimitExceeded is returned if a body is read beyond the set limit.

View Source
var ErrInvalidByteSize = errors.New("serve: byte size must be like 4K, 20MiB or 5GB")

ErrInvalidByteSize is returned for invalid byte sizes.

Functions

func AssetServer

func AssetServer(prefix, directory string) http.Handler

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 properly serve single page applications.

func BasicAuth

func BasicAuth(username, password, realm string) func(http.Handler) http.Handler

BasicAuth returns a middleware that enforces standard HTTP Basic Authentication.

func ByteSize added in v0.3.0

func ByteSize(str string) (int64, error)

ByteSize parses human readable byte sizes (e.g. 4K, 20MiB or 5GB) and returns the amount of bytes they represent. ErrInvalidByteSize is returned if the specified byte size is invalid.

func CORS

func CORS(policy CORSPolicy) func(http.Handler) http.Handler

CORS returns a middleware for enforcing CORS.

func Compose

func Compose(chain ...interface{}) http.Handler

Compose is a short-hand for chaining the specified middleware and handler together.

func ContentSecurity

func ContentSecurity(policy ContentPolicy) func(http.Handler) http.Handler

ContentSecurity returns a middleware for enforcing content security.

func Forwarded added in v0.3.5

func Forwarded() func(http.Handler) http.Handler

Forwarded is a middleware that will parse "X-Forwarded-X" headers and mutate the request to reflect the conditions described by the headers.

Note: This technique should only be applied to apps that are behind a load balancer that will *always* set the headers. Otherwise an attacker may be able to provide false information and circumvent security limitations.

func Hostname added in v0.1.3

func Hostname(host string) string

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 IP added in v0.3.5

func IP(addr string) string

IP will return just the IP part from an address of the form ip[:port].

func Limit added in v0.3.4

func Limit(limit int64) func(http.Handler) http.Handler

Limit will return a middleware that ensures a limited body.

func LimitBody

func LimitBody(w http.ResponseWriter, r *http.Request, limit int64)

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 later in the request processing.

func Local added in v0.3.1

func Local(handler http.Handler) http.RoundTripper

Local returns a round tripper that uses the provided handler to serve the requests. It may be used with http.Client in unit tests.

func MustByteSize added in v0.3.0

func MustByteSize(str string) int64

MustByteSize will call ByteSize and panic on 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 Recover added in v0.3.5

func Recover(reporter func(error)) func(h http.Handler) http.Handler

Recover is a middleware that recovers panics and forwards the error to the the provided reporter.

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.

func Security

func Security(allowInsecure, noFrontend bool, stsMaxAge time.Duration) func(http.Handler) http.Handler

Security will return a middleware that enforce various standard web security techniques.

func Throttle added in v0.3.3

func Throttle(concurrency int) func(http.Handler) http.Handler

Throttle returns a middleware that limits concurrent requests. The middleware will block the request and wait for a token. If the context is cancelled beforehand, "Too Many Requests" is returned.

func Timeout added in v0.3.3

func Timeout(timeout time.Duration) func(http.Handler) http.Handler

Timeout returns a middleware that ensures a request timeout.

Types

type BodyLimiter

type BodyLimiter struct {
	Length   int64
	Limit    int64
	Original io.ReadCloser
	Limited  io.ReadCloser
}

BodyLimiter wraps a io.ReadCloser and keeps a reference to the original.

func (*BodyLimiter) Close added in v0.2.0

func (l *BodyLimiter) Close() error

Close will close the body.

func (*BodyLimiter) Read added in v0.2.0

func (l *BodyLimiter) Read(p []byte) (int, error)

Read will read from the underlying io.Reader.

type CORSPolicy

type CORSPolicy = cors.Options

CORSPolicy defines the CORS policy.

type ContentPolicy

type ContentPolicy map[string][]string

ContentPolicy for defining content security.

func (ContentPolicy) String

func (p ContentPolicy) String() string

String will encode the policy as a string.

Jump to

Keyboard shortcuts

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