httpentity

package
v0.0.0-...-00f1f15 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2015 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package httpentity provides a "framework" for exposing resources over HTTP.

Within the framework, an "Entity" is simply something that can be accessed over HTTP. A "NetEntity" is something that is capable of being transmitted as an HTTP body--most Entities will also be NetEntities. But, things like error messages are NetEntities, but aren't Entities, as it's not a normal thing that you can request.

An Entity is accessed using the method allowed in (*Entity).Methods(). An entity may also have children--these are accessed with (*Entity).Subentities(childName, request).

A Router is the entire Entity tree; it mostly just takes the root Entity of the tree. It handles dispatching Requests to the correct Entity, then formatting the Responses to the output stream.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encoder

type Encoder interface {
	Locales() []locale.Spec
	Write(io.Writer, locale.Spec) locale.Error
	IsText() bool
}

type EncoderJSON

type EncoderJSON struct {
	Data interface{}
}

func (EncoderJSON) IsText

func (e EncoderJSON) IsText() bool

func (EncoderJSON) Locales

func (e EncoderJSON) Locales() []locale.Spec

func (EncoderJSON) Write

func (e EncoderJSON) Write(w io.Writer, l locale.Spec) locale.Error

type EncoderJSONStr

type EncoderJSONStr struct {
	Data locale.Stringer
}

func (EncoderJSONStr) IsText

func (e EncoderJSONStr) IsText() bool

func (EncoderJSONStr) Locales

func (e EncoderJSONStr) Locales() []locale.Spec

func (EncoderJSONStr) Write

func (e EncoderJSONStr) Write(w io.Writer, l locale.Spec) locale.Error

type EncoderTXT

type EncoderTXT struct {
	Data locale.Stringer
}

func (EncoderTXT) IsText

func (e EncoderTXT) IsText() bool

func (EncoderTXT) Locales

func (e EncoderTXT) Locales() []locale.Spec

func (EncoderTXT) Write

func (e EncoderTXT) Write(w io.Writer, l locale.Spec) locale.Error

type Entity

type Entity interface {
	// Methods() returns a map of HTTP request methods to Handlers
	// that handle requests for this Entity.
	Methods() map[string]func(Request) Response
}

An Entity is some resource that is accessible over HTTP.

type EntityExtra

type EntityExtra interface {
	Entity
	MethodNotAllowed(request Request) Response
}

EntityExtra is an Entity that provides a custom HTTP 405 ("Method Not Allowed") handler.

type EntityGroup

type EntityGroup interface {
	Entity

	// Subentity(name, request) returns the child of this entity
	// with the name `name`, or nil if a child with that name
	// doesn't exist.
	//
	// The Request is included in the function call so that it can
	// be determined if the user has permission to access that
	// child.
	Subentity(name string, request Request) Entity

	// SubentityNotFound is called if Subentity returns nil.  If
	// the name contains a slash; it indicates that the child was
	// found, but a grandchild was requested, and the child wasn't
	// an EntityGroup.
	SubentityNotFound(name string, request Request) Response
}

An EntityGroup is an Entity that also has child entities (i.e., a directory or folder).

type Logger

type Logger interface {
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

type Middleware

type Middleware struct {
	// Outside is able to affect the entity that is looked up
	Outside func(Request, func(Request) Response) Response
	// Inside cannot affect the entity that is looked up, but it
	// gets to inspect the entity.
	Inside func(Request, Entity, func(Request, Entity) Response) Response
}

A Middleware is something that wraps the request handler.

type NetEntity

type NetEntity interface {
	// Encoders() returns a map of MIME-types to encoders that
	// serialize the NetEntity to that type.
	Encoders() map[string]Encoder
}

A NetEntity is just something that is capable of being transmitted over the network (in a variety of formats).

func ErrorToNetEntity

func ErrorToNetEntity(status int16, err locale.Error) NetEntity

type NetJSON

type NetJSON struct {
	Data interface{}
}

func (NetJSON) Encoders

func (l NetJSON) Encoders() map[string]Encoder

Encoders fulfills the httpentity.NetEntity interface.

func (NetJSON) IsText

func (l NetJSON) IsText() bool

func (NetJSON) Locales

func (l NetJSON) Locales() []locale.Spec

func (NetJSON) Write

func (l NetJSON) Write(w io.Writer, loc locale.Spec) locale.Error

type NetLocations

type NetLocations []*url.URL

func (NetLocations) Encoders

func (l NetLocations) Encoders() map[string]Encoder

Encoders fulfills the httpentity.NetEntity interface.

func (NetLocations) IsText

func (l NetLocations) IsText() bool

func (NetLocations) Locales

func (l NetLocations) Locales() []locale.Spec

type NetStringer

type NetStringer struct {
	locale.Stringer
}

NetString is a string that implements httpentity.NetEntity.

func NetPrintf

func NetPrintf(format string, a ...interface{}) NetStringer

NetPrintf is fmt.Sprintf as a NetString.

func (NetStringer) Encoders

func (s NetStringer) Encoders() map[string]Encoder

Encoders fulfills the httpentity.NetEntity interface.

type Request

type Request struct {
	Method  string
	URL     *url.URL
	Headers http.Header
	Entity  interface{}
	Things  map[string]interface{} // Objects added by middlewares
	// contains filtered or unexported fields
}

Request is an incoming HTTP request to be handled.

func (*Request) Cookie

func (req *Request) Cookie(name string) *http.Cookie

Cookie returns the cookie `name`, or nil if it isn't set.

type Response

type Response struct {
	Status                 int16
	Headers                http.Header
	Entity                 NetEntity
	InhibitNotAcceptable   bool
	InhibitMultipleChoices bool
	// contains filtered or unexported fields
}

The Response to an HTTP request.

type RootEntity

type RootEntity interface {
	Entity
	Subentity(name string, request Request) Entity
	SubentityNotFound(name string, request Request) Response
	MethodNotAllowed(request Request) Response
}

RootEntity is the union of EntityGroup and EntityExtra.

type Router

type Router struct {
	Prefix      string
	Root        RootEntity
	Decoders    map[string]func(io.Reader, map[string]string) (interface{}, locale.Error)
	Middlewares []Middleware

	// Whether to include stacktraces in HTTP 500 responses
	Stacktrace bool

	Log Logger

	// Whether to trust `X-Forwarded-Scheme:` and RFC 7239
	// `Forwarded: proto=`
	TrustForwarded bool

	MethodNotAllowed func(entity Entity, request Request) Response
	// contains filtered or unexported fields
}

A Router represents the root of an Entity tree, and handles reading and writing messages to the network socket.

func (Router) Init

func (router Router) Init() *Router

Init initializes the hidden fields; should be called before any other method.

func (*Router) Route

func (router *Router) Route(req Request) (res Response)

Route routes and handles a request, returning the response.

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP makes the Router fulfill the "net/http".Handler interface.

Directories

Path Synopsis
Package heutil provides utilities for working with the httpentity framework.
Package heutil provides utilities for working with the httpentity framework.

Jump to

Keyboard shortcuts

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