serve

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2020 License: MIT Imports: 12 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.

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 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 DataSize

func DataSize(str string) uint64

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

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

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.

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.

Types

type BodyLimiter

type BodyLimiter struct {
	Length   int64
	Limit    uint64
	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