serve

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2019 License: MIT Imports: 13 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

This section is empty.

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 run single page applications like Ember.

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

func RPCHandler(limit uint64, 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.

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 {
	io.ReadCloser
	Limit    uint64
	Original io.ReadCloser
}

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

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.

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.

func (*RPCClient) Call

func (c *RPCClient) Call(endpoint string, response, request interface{}) error

Call will call an endpoint with with the specified request and store the result in the specified response.

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 RPCData

type RPCData map[string]interface{}

RPCData represents arbitrary data.

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

func RPCBadRequest(detail, source string) *RPCError

RPCBadRequest returns a new bad request error with a source.

func RPCErrorFromStatus

func RPCErrorFromStatus(status int, detail string) *RPCError

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 RPCInternalServerError

func RPCInternalServerError(detail string) *RPCError

RPCInternalServerError returns na new internal server error.

func RPCNotFound

func RPCNotFound(detail string) *RPCError

RPCNotFound returns a new not found error.

func (*RPCError) Error

func (e *RPCError) Error() string

RPCError returns a string representation of the error for logging purposes.

Jump to

Keyboard shortcuts

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