resozyme: github.com/fivestar/resozyme Index | Files | Directories

package resozyme

import "github.com/fivestar/resozyme"

Package resozyme provides a resource-oriented http handler interface.

Index

Package Files

base.go binder.go container.go controller.go doc.go error_handler.go hal_renderer.go json_renderer.go logger.go mux.go resource.go

func ActivateResource Uses

func ActivateResource(ctx context.Context, resc Resource) error

ActivateResource sets a resource to ActiveResourceContainer.

func Route Uses

func Route(mux Mux, path string, fac ResourceFactory)

Route binds a ResourceFactory to the router.

func WithActiveResourceContainer Uses

func WithActiveResourceContainer(ctx context.Context, rcont *ResourceContainer) context.Context

WithActiveResourceContainer sets a resource container.

func WithController Uses

func WithController(ctx context.Context, rcont Controller) context.Context

WithController sets a controller to the context.

type ActiveResourceContainerContextKey Uses

type ActiveResourceContainerContextKey struct{}

ActiveResourceContainerContextKey is a context key.

type Base Uses

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

Base provides the core behavior as resource.Resource. In principle, this struct should be embedded when defining a new resource.

And then to complete to implement resource.Resource, the concrete resource need to be implemented `Href` method.

Also, as the resource almost certainly has own view struct, needs to be implemented `View` method.

func NewBase Uses

func NewBase(ctx context.Context) *Base

NewBase creates a new Base.

func (resc *Base) AddLink(rel string, l Link)

AddLink implements resource.Resource.

func (*Base) AddLinkCollection Uses

func (resc *Base) AddLinkCollection(rel string, l []Link)

AddLinkCollection implements resource.Resource.

func (*Base) Bind Uses

func (resc *Base) Bind(i interface{})

Bind implements resource.Resource.

func (*Base) Code Uses

func (resc *Base) Code() int

Code implements resource.Resource.

func (*Base) Context Uses

func (resc *Base) Context() context.Context

Context implements resource.Resource.

func (*Base) Embed Uses

func (resc *Base) Embed(rel string, er Resource)

Embed implements resource.Resource.

func (*Base) EmbedCollection Uses

func (resc *Base) EmbedCollection(rel string, er []Resource)

EmbedCollection implements resource.Resource.

func (*Base) Embedded Uses

func (resc *Base) Embedded() map[string]Resource

Embedded implements resource.Resource.

func (*Base) EmbeddedCollection Uses

func (resc *Base) EmbeddedCollection() map[string][]Resource

EmbeddedCollection implements resource.Resource.

func (*Base) Error Uses

func (resc *Base) Error() error

Error implements resource.Resource.

func (*Base) Header Uses

func (resc *Base) Header() http.Header

Header implements resource.Resource.

func (*Base) LinkResource Uses

func (resc *Base) LinkResource(rel string, aResc Resource)

LinkResource implements resource.Resource.

func (*Base) LinkResourceCollection Uses

func (resc *Base) LinkResourceCollection(rel string, aResc []Resource)

LinkResourceCollection implements resource.Resource.

func (resc *Base) Links() map[string]Link

Links implements resource.Resource.

func (*Base) LinksCollection Uses

func (resc *Base) LinksCollection() map[string][]Link

LinksCollection implements resource.Resource.

func (*Base) OnDelete Uses

func (resc *Base) OnDelete(w http.ResponseWriter, r *http.Request)

OnDelete implements resource.Resource. If the resource needs to handle the DELETE request, please override this. Unless defined, it responds the status 405 Method Not Allowed.

func (*Base) OnGet Uses

func (resc *Base) OnGet(w http.ResponseWriter, r *http.Request)

OnGet implements resource.Resource. If the resource needs to handle the GET request, please override this. Unless defined, it responds the status 405 Method Not Allowed.

func (*Base) OnPatch Uses

func (resc *Base) OnPatch(w http.ResponseWriter, r *http.Request)

OnPatch implements resource.Resource. If the resource needs to handle the PATCH request, please override this. Unless defined, it responds the status 405 Method Not Allowed.

func (*Base) OnPost Uses

func (resc *Base) OnPost(w http.ResponseWriter, r *http.Request)

OnPost implements resource.Resource. If the resource needs to handle the POST request, please override this. Unless defined, it responds the status 405 Method Not Allowed.

func (*Base) OnPut Uses

func (resc *Base) OnPut(w http.ResponseWriter, r *http.Request)

OnPut implements resource.Resource. If the resource needs to handle the PUT request, please override this. Unless defined, it responds the status 405 Method Not Allowed.

func (*Base) Renderer Uses

func (resc *Base) Renderer() Renderer

Renderer implements resource.Resource.

func (*Base) SetCode Uses

func (resc *Base) SetCode(code int)

SetCode implements resource.Resource.

func (*Base) SetError Uses

func (resc *Base) SetError(err error)

SetError implements resource.Resource.

func (*Base) SetRenderer Uses

func (resc *Base) SetRenderer(renderer Renderer)

SetRenderer implements resource.Resource.

func (*Base) View Uses

func (resc *Base) View() interface{}

View implements resource.Resource.

type Controller Uses

type Controller interface {
    http.Handler

    // Dispatch dispatches a resource.
    Dispatch(resc Resource, w http.ResponseWriter, r *http.Request)

    // SetDefaultRenderer sets a default renderer.
    SetDefaultRenderer(renderer Renderer)

    // IsError checks whether a resource has an error or not.
    IsError(resc Resource) bool

    // HandleError handles an error resource.
    HandleError(resc Resource, w http.ResponseWriter, r *http.Request) Resource

    // Logger returns a logger.
    Logger() Logger
}

Controller provides a resource controller interface.

func GetController Uses

func GetController(ctx context.Context) Controller

GetController gets a controller from the context.

type ControllerContextKey Uses

type ControllerContextKey struct{}

ControllerContextKey is a context key.

type Dispatcher Uses

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

Dispatcher handles HTTP request and response.

func NewController Uses

func NewController(mux Mux, logger Logger, debug bool) *Dispatcher

NewController creates a controller.

func (*Dispatcher) Dispatch Uses

func (dispatcher *Dispatcher) Dispatch(resc Resource, w http.ResponseWriter, r *http.Request)

Dispatch implements Controller.

func (*Dispatcher) HandleError Uses

func (dispatcher *Dispatcher) HandleError(resc Resource, w http.ResponseWriter, r *http.Request) Resource

HandleError implements Controller.

func (*Dispatcher) IsError Uses

func (dispatcher *Dispatcher) IsError(resc Resource) bool

IsError implements Controller.

func (*Dispatcher) Logger Uses

func (dispatcher *Dispatcher) Logger() Logger

Logger implements Controller.

func (*Dispatcher) ServeHTTP Uses

func (dispatcher *Dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

func (*Dispatcher) SetDefaultRenderer Uses

func (dispatcher *Dispatcher) SetDefaultRenderer(renderer Renderer)

SetDefaultRenderer implements Controller.

type ErrorHandler Uses

type ErrorHandler interface {
    // IsError checks whether the given resource has the error.
    IsError(resc Resource) bool

    // HandleError transforms the resource that has the error to another resource
    // that represents the error.
    HandleError(resc Resource, w http.ResponseWriter, r *http.Request) Resource
}

ErrorHandler is an error handler interface.

type ErrorResource Uses

type ErrorResource struct {
    *Base
    // contains filtered or unexported fields
}

ErrorResource is an resource to represent the error.

func (*ErrorResource) Href Uses

func (resc *ErrorResource) Href() string

Href implements resource.Resource. This method returns the dummy URL contains the status code.

func (*ErrorResource) View Uses

func (resc *ErrorResource) View() interface{}

View implements resource.Resource.

type ErrorView Uses

type ErrorView struct {
    Message string `json:"message"`
}

ErrorView is a view for the error.

type ExposedErrorHandler Uses

type ExposedErrorHandler struct {
    Renderer Renderer
}

ExposedErrorHandler is an ErrorHandler. [NOTICE] This handler exposes the raw error message to the response view.

func (*ExposedErrorHandler) HandleError Uses

func (eh *ExposedErrorHandler) HandleError(resc Resource, w http.ResponseWriter, r *http.Request) Resource

HandleError implements resource.ErrorHandler.

func (*ExposedErrorHandler) IsError Uses

func (eh *ExposedErrorHandler) IsError(resc Resource) bool

IsError implements resource.ErrorHandler.

type HALRenderer Uses

type HALRenderer struct {
}

HALRenderer represents the resource in hal+json format.

func NewHALRenderer Uses

func NewHALRenderer() *HALRenderer

NewHALRenderer creates a new HALRenderer.

func (*HALRenderer) Render Uses

func (renderer *HALRenderer) Render(resc Resource, pretty bool) []byte

Render implements resource.Renderer.

type JSONRenderer Uses

type JSONRenderer struct {
}

JSONRenderer represents the resource in json format.

func NewJSONRenderer Uses

func NewJSONRenderer() *JSONRenderer

NewJSONRenderer creates a new JSONRenderer.

func (*JSONRenderer) Render Uses

func (renderer *JSONRenderer) Render(resc Resource, pretty bool) []byte

Render implements resource.Renderer.

type Link struct {
    Href string
}

Link represents a link.

type Logger Uses

type Logger interface {
    Debug(args ...interface{})
    Info(args ...interface{})
    Warn(args ...interface{})
    Error(args ...interface{})
    Fatal(args ...interface{})
    Debugf(template string, args ...interface{})
    Infof(template string, args ...interface{})
    Warnf(template string, args ...interface{})
    Errorf(template string, args ...interface{})
    Fatalf(template string, args ...interface{})
}

Logger is a logger interface. This interface is compatible with zap.SugaredLogger.

type Mux Uses

type Mux interface {
    http.Handler

    // HandleFunc registers the handler function for the given pattern.
    HandleFunc(pattern string, handler http.HandlerFunc)
}

Mux is an HTTP request multiplexer. This interface is designed to be compatible with chi.Mux.

type NilLogger Uses

type NilLogger struct {
}

NilLogger is a nil logger implementation.

func (*NilLogger) Debug Uses

func (logger *NilLogger) Debug(args ...interface{})

Debug implements resource.Logger.

func (*NilLogger) Debugf Uses

func (logger *NilLogger) Debugf(template string, args ...interface{})

Debugf implements resource.Logger.

func (*NilLogger) Error Uses

func (logger *NilLogger) Error(args ...interface{})

Error implements resource.Logger.

func (*NilLogger) Errorf Uses

func (logger *NilLogger) Errorf(template string, args ...interface{})

Errorf implements resource.Logger.

func (*NilLogger) Fatal Uses

func (logger *NilLogger) Fatal(args ...interface{})

Fatal implements resource.Logger.

func (*NilLogger) Fatalf Uses

func (logger *NilLogger) Fatalf(template string, args ...interface{})

Fatalf implements resource.Logger.

func (*NilLogger) Info Uses

func (logger *NilLogger) Info(args ...interface{})

Info implements resource.Logger.

func (*NilLogger) Infof Uses

func (logger *NilLogger) Infof(template string, args ...interface{})

Infof implements resource.Logger.

func (*NilLogger) Warn Uses

func (logger *NilLogger) Warn(args ...interface{})

Warn implements resource.Logger.

func (*NilLogger) Warnf Uses

func (logger *NilLogger) Warnf(template string, args ...interface{})

Warnf implements resource.Logger.

type Renderer Uses

type Renderer interface {
    // Render renders a resource view.
    Render(resc Resource, pretty bool) []byte
}

Renderer provides a resource renderer interface.

type Resource Uses

type Resource interface {
    // Href returns the pathinfo.
    Href() string
    // Context returns the context.
    Context() context.Context

    // Code returns the status code.
    Code() int
    // Header returns the HTTP header object.
    Header() http.Header
    // View returns the view part.
    View() interface{}
    // Error returns an error if occurred.
    Error() error

    // SetCode sets the code.
    SetCode(code int)
    // SetError sets an error.
    SetError(error error)

    // AddLink adds a link.
    AddLink(rel string, l Link)
    // AddLinkCollection adds the link collection.
    AddLinkCollection(rel string, l []Link)
    // LinkResource adds a link to the resource.
    LinkResource(rel string, aResc Resource)
    // LinkResourceCollection adds link to the resource collection.
    LinkResourceCollection(rel string, aResc []Resource)
    // Links returns the links.
    Links() map[string]Link
    // LinksCollection returns link collection.
    LinksCollection() map[string][]Link

    // Embed embeds a resource.
    Embed(rel string, er Resource)
    // Embed embeds the resource collection.
    EmbedCollection(rel string, er []Resource)
    // Embedded returns embedded resources.
    Embedded() map[string]Resource
    // EmbeddedCollection returns embedded the resource collection.
    EmbeddedCollection() map[string][]Resource

    // Renderer returns the renderer.
    Renderer() Renderer
    // SetRenderer sets a renderer to the resource.
    SetRenderer(renderer Renderer)

    // Bind binds passed value to the resource.
    Bind(i interface{})
    // OnGet handles the GET request.
    OnGet(w http.ResponseWriter, r *http.Request)
    // OnGet handles the POST request.
    OnPost(w http.ResponseWriter, r *http.Request)
    // OnGet handles the PUT request.
    OnPut(w http.ResponseWriter, r *http.Request)
    // OnGet handles the PATCH request.
    OnPatch(w http.ResponseWriter, r *http.Request)
    // OnGet handles the DELETE request.
    OnDelete(w http.ResponseWriter, r *http.Request)
}

Resource represents a resource.

func BindTo Uses

func BindTo(resc Resource, args ...interface{}) Resource

BindTo binds objects to the resource.

func GetActiveResource Uses

func GetActiveResource(ctx context.Context) Resource

GetActiveResource gets the activated resource from ActiveResourceContainer.

type ResourceContainer Uses

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

ResourceContainer is a container.

func GetActiveResourceContainer Uses

func GetActiveResourceContainer(ctx context.Context) *ResourceContainer

GetActiveResourceContainer gets a resource container.

func NewResourceContainer Uses

func NewResourceContainer() *ResourceContainer

NewResourceContainer creates a new ResourceContainer.

func (*ResourceContainer) Exists Uses

func (rcont *ResourceContainer) Exists() bool

Exists checks whether the container has a resource.

func (*ResourceContainer) Get Uses

func (rcont *ResourceContainer) Get() Resource

Get gets a resource from the container.

func (*ResourceContainer) Set Uses

func (rcont *ResourceContainer) Set(resc Resource)

Set sets a resource to the container.

type ResourceFactory Uses

type ResourceFactory = func(context.Context) Resource

ResourceFactory is a resource factory.

Directories

PathSynopsis
_examples
_examples/model
_examples/repos
_examples/resources

Package resozyme imports 8 packages (graph) and is imported by 2 packages. Updated 2019-09-11. Refresh now. Tools for package owners.