web

package module
v0.0.4-0...-fcd4d2a Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

web

GoDoc

Package web provides a structured, scalable and easily unit-tested set of primitives for use in web applications.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteSizeToFriendlyString

func ByteSizeToFriendlyString(length int64) string

ByteSizeToFriendlyString returns the provided byte length as a human-friendly string e.g. 1024 => 1.00 kB.

func UnmarshalFromResponse

func UnmarshalFromResponse(res *http.Response, model interface{}) error

UnmarshalFromResponse unmarshals the body of an http.Response to a model.

Types

type Config

type Config struct {
	ProblemDetailsTypePrefix string
	DebuggingEnabled         bool
	JSONContentLengthLimit   int64
}

Config defines a set of configuration values that dictate how the handler behaves at a global level.

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context represents the context of a single HTTP web request. It is not thread-safe.

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request, c di.Container, config *Config) *Context

NewContext creates a new context for the provided request.

func (*Context) AssertContentLength

func (ctx *Context) AssertContentLength(max int64) bool

AssertContentLength ensures that a content length was provided, and that it is in (0, max].

func (*Context) AssertContentType

func (ctx *Context) AssertContentType(allowedContentTypes ...string) bool

AssertContentType ensures that the content type of the request matches one of the content types provided.

func (*Context) AssertMethod

func (ctx *Context) AssertMethod(allowedMethods ...string) bool

AssertMethod ensures that the incoming request is using one of the provided methods.

func (*Context) Container

func (ctx *Context) Container() di.Container

Container returns the underlying container.

func (*Context) FromJSON

func (ctx *Context) FromJSON(model Purifiable) bool

FromJSON retrieves JSON from the request body to place into the provided Purifiable.

func (*Context) GetCorrelationID

func (ctx *Context) GetCorrelationID() id.ID

GetCorrelationID returns the correlationID for the request.

func (*Context) GetMiddlewareArtifact

func (ctx *Context) GetMiddlewareArtifact(name string) interface{}

GetMiddlewareArtifact retrieves the middleware artifact with the specified name. It will return nil if the artifact does not exist.

func (*Context) GetPathParameter

func (ctx *Context) GetPathParameter(name string) string

GetPathParameter retrieves a path segment parameter from the request.

func (*Context) GetQueryParameter

func (ctx *Context) GetQueryParameter(name string) string

GetQueryParameter retrieves a query parameter from the request.

func (*Context) Header

func (ctx *Context) Header() http.Header

Header returns the set of response headers.

func (*Context) InternalServerError

func (ctx *Context) InternalServerError(err error)

InternalServerError responds to the request with an InternalServerError status code.

func (*Context) NotFound

func (ctx *Context) NotFound(subjectType string, subject string)

NotFound responds to the request with a NotFound status code.

func (*Context) Request

func (ctx *Context) Request() *http.Request

Request returns the *http.Request.

func (*Context) Resolve

func (ctx *Context) Resolve(dependencies ...interface{}) bool

Resolve resolves from the underlying container. It will return false if an error prevented the operation from completing.

func (*Context) Respond

func (ctx *Context) Respond(code int)

Respond reponds to the request with the provided HTTP code.

func (*Context) RespondWithJSON

func (ctx *Context) RespondWithJSON(code int, model interface{})

RespondWithJSON responds to the request with the provided HTTP code and model.

func (*Context) ResponseWriter

func (ctx *Context) ResponseWriter() http.ResponseWriter

ResponseWriter returns the http.ResponseWriter.

func (*Context) SetMiddlewareArtifact

func (ctx *Context) SetMiddlewareArtifact(name string, value interface{})

SetMiddlewareArtifact sets the middleware artifact for the specified name.

type ContextHandlerFunc

type ContextHandlerFunc func(ctx *Context)

ContextHandlerFunc is an alias for a function that accepts a Context as its one and only parameter.

type HandlerBuilder

type HandlerBuilder struct {
	// contains filtered or unexported fields
}

HandlerBuilder is used to build a handler that can be passed to any HTTP server. Once Build has been called, the HandlerBuilder is invalid and can no longer be used. HandlerBuilder is not thread-safe.

func NewHandlerBuilder

func NewHandlerBuilder(c di.Container, logger logging.Logger, config *Config) *HandlerBuilder

NewHandlerBuilder creates a new handler builder with the provided config and container.

func (*HandlerBuilder) Build

func (b *HandlerBuilder) Build() http.Handler

Build builds a http.Handler that can be passed to any server.

func (*HandlerBuilder) Use

func (b *HandlerBuilder) Use(route Route)

Use adds a route to the list of routes this handler should expose.

type MeasuredResponseWriter

type MeasuredResponseWriter struct {
	// contains filtered or unexported fields
}

MeasuredResponseWriter wraps a standard http.ResponseWriter with additional functionality. Specifically, it records the amount of data written, the response code, and the duration of the request/response.

func NewMeasuredResponseWriter

func NewMeasuredResponseWriter(w http.ResponseWriter) *MeasuredResponseWriter

NewMeasuredResponseWriter creates a new MeasuredResponseWriter with the provided underlying http.ResponseWriter.

func (*MeasuredResponseWriter) Duration

func (mrw *MeasuredResponseWriter) Duration() time.Duration

Duration returns the duration between the start of the request and now.

func (*MeasuredResponseWriter) HasWrittenHeaders

func (mrw *MeasuredResponseWriter) HasWrittenHeaders() bool

HasWrittenHeaders returns true if WriteHeader has been called.

func (*MeasuredResponseWriter) Header

func (mrw *MeasuredResponseWriter) Header() http.Header

Header simply returns the headers of the underlying response writer.

func (*MeasuredResponseWriter) StatusCode

func (mrw *MeasuredResponseWriter) StatusCode() int

StatusCode returns the status code that was written for the response. If the status code is yet to be written, or WriteHeader was never explicitly called, StatusCode will return http.StatusOK.

func (*MeasuredResponseWriter) Volume

func (mrw *MeasuredResponseWriter) Volume() int64

Volume returns the number of bytes written to the response writer body.

func (*MeasuredResponseWriter) Write

func (mrw *MeasuredResponseWriter) Write(b []byte) (int, error)

Write writes to the underlying response writer, recording the number of bytes successfully written.

func (*MeasuredResponseWriter) WriteHeader

func (mrw *MeasuredResponseWriter) WriteHeader(statusCode int)

WriteHeader records and writes the header if it has not already been written.

type Middleware

type Middleware interface {
	Handle(ctx *Context) bool
}

Middleware defines the methods that any HTTP middleware must implement. If the Handle method returns true, the request will continue to be propagated to subsequent middleware handlers and eventually the route handler.

type Purifiable

type Purifiable interface {
	Purify() (string, error)
}

Purifiable defines the methods that any purifiable request model must implement. The intention is to allow request methods to be able to validate themselves - keeping the validation of user provided information out of routes.

The first return value is the name of the invalid field, the second is the error describing the problem.

type Route

type Route interface {
	Method() string
	Path() string
	Middleware() []Middleware
	Handle(ctx *Context)
}

Route defines the methods that any HTTP route must implement.

Jump to

Keyboard shortcuts

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