resthandler

package module
v0.0.0-...-6c5d8aa Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2018 License: MIT Imports: 9 Imported by: 0

README

Go Report Card License Linux build Coverage Status godoc

resthandler

go module that implements http.Handler the restful way

resthandler implements restful routing

func main(){
    myRestImplementation := MyImplementation()

    h := resthandler.New("my-address-book", myRestImplementation)

    http.ListenAndServe(":8002", h)
}

Rester interface

Implement your business logic by implementing the Rester interface.

Response encoding (GET requests)

The response content sent to the client depends on the type your implementation of Rester returns in it's first parameter.

automatic encoding using structs

If you return a struct resthandler will encode into the requested content-type. (xml / json)

custom encoding

To implement custom encoding, return a implementation of ResponseEncoder interface.

plain encoding

If you return []byte or string the data will be written to the client, regardless of the requested content-type.

Custom HTTP Error Status Codes

If your Rester implementation returns an error, resthandler will by default return 400 - Bad Request.

If you return an error that implements ResterError, you can define the returned StatusCode.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAllowedMethods

func GetAllowedMethods() []string

GetAllowedMethods returns all allowed http methods used by REST. Useful to populate wrapping handlers (eg. CORS handler).

func New

func New(rester Rester) http.Handler

New returns a new http.Handler which handles REST requests for the given entity uri. For processing of the rest actions it calls the appropriate methods of the given Rester.

Types

type AllGetter

type AllGetter interface {
	GetAll() (interface{}, error)
}

AllGetter defines the interface to get all resources

type Builder

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

Builder holds data and methods to create a new REST flavored Router

func NewBuilder

func NewBuilder() *Builder

NewBuilder creates a new Builder. ResourcePath defines the uri-path for the resource.

func (*Builder) AddCreate

func (b *Builder) AddCreate(h Creator) *Builder

AddCreate adds a route entry and connects it with the given Creator implementation

func (*Builder) AddDelete

func (b *Builder) AddDelete(h Deleter) *Builder

AddDelete adds a route entry and connects it with the given Deleter implementation

func (*Builder) AddGetAll

func (b *Builder) AddGetAll(handler AllGetter) *Builder

AddGetAll adds a route entry and connects it with the given AllGetter implementation

func (*Builder) AddGetSingle

func (b *Builder) AddGetSingle(handler SingleGetter) *Builder

AddGetSingle adds a route entry and connects it with the given SingleGetter implementation

func (*Builder) AddIndex

func (b *Builder) AddIndex(h SingleIndexer) *Builder

AddIndex adds a route entry and connects it with the given SingleIndexer implementation

func (*Builder) AddOptions

func (b *Builder) AddOptions() *Builder

AddOptions adds a route entry to handle OPTIONS requests

func (*Builder) AddPatch

func (b *Builder) AddPatch(h Patcher) *Builder

AddPatch adds a route entry and connects it with the given Patcher implementation

func (*Builder) AddUpdate

func (b *Builder) AddUpdate(h Updater) *Builder

AddUpdate adds a route entry and connects it with the given Updater implementation

func (*Builder) Build

func (b *Builder) Build() *mux.Router

Returns the build product

type Creator

type Creator interface {
	Create(id string, rawEntity []byte) error
}

Creator defines the interface to create a new resource

type Deleter

type Deleter interface {
	Delete(id string) error
}

Deleter defines the interface to delete a single resource

type Patcher

type Patcher interface {
	Patch(id string, rawEntity []byte) error
}

Patcher defines the interface to update a single resource partially

type ResponseContentTypeSetter

type ResponseContentTypeSetter interface {
	SetContentType(accept string) string
}

ResponseContentTypeSetter can be used to set the response content-type. If the first return value of a Rester function returns implements this interface it can define the content-type that is passed to the client

type ResponseEncoder

type ResponseEncoder interface {
	Encode(contentType string) interface{}
}

ResponseEncoder can be used to implement custom response content encoding. If the first return value of a Rester function returns implements this interface the Encode function will be called and the result will be passed back to the client.

type Rester

Rester defines an full feature interface that is called per REST action

type ResterError

type ResterError interface {
	GetHTTPStatusCode() int
	Error() string
}

ResterError defines an interface that enables Rester implementations to pass back custom HTTP Status Codes to express custom error values. If an error from a Rester function implements this interface its status code will be passed back to the client.

type SingleGetter

type SingleGetter interface {
	GetSingle(id string) (interface{}, error)
}

SingleGetter defines the interface to get a single resource

type SingleIndexer

type SingleIndexer interface {
	Index(id string) (interface{}, error)
}

SingleIndexer defines the interface to get the index of a single resource

type Updater

type Updater interface {
	Update(id string, rawEntity []byte) error
}

Updater defines the interface to update a single resource

Directories

Path Synopsis
Code generated by mockery v1.0.0
Code generated by mockery v1.0.0

Jump to

Keyboard shortcuts

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