resozyme

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2019 License: MIT Imports: 8 Imported by: 0

README

Resozyme

Go Report Card GoDoc Build Status codecov

resozyme is a resource-oriented framework for building the RESTful web application in Go.

Installation

$ go get -u github.com/fivestar/resozyme

Examples

See _examples/.

License

Copyright (c) 2019 Katsuhiro Ogawa

Licensed under the MIT License.

Documentation

Overview

Package resozyme provides a resource-oriented http handler interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActivateResource

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

ActivateResource sets a resource to ActiveResourceContainer.

func Route

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

Route binds a ResourceFactory to the router.

func WithActiveResourceContainer

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

WithActiveResourceContainer sets a resource container.

func WithController

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

WithController sets a controller to the context.

Types

type ActiveResourceContainerContextKey

type ActiveResourceContainerContextKey struct{}

ActiveResourceContainerContextKey is a context key.

type Base

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

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

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

AddLinkCollection implements resource.Resource.

func (*Base) Bind

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

Bind implements resource.Resource.

func (*Base) Code

func (resc *Base) Code() int

Code implements resource.Resource.

func (*Base) Context

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

Context implements resource.Resource.

func (*Base) Embed

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

Embed implements resource.Resource.

func (*Base) EmbedCollection

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

EmbedCollection implements resource.Resource.

func (*Base) Embedded

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

Embedded implements resource.Resource.

func (*Base) EmbeddedCollection

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

EmbeddedCollection implements resource.Resource.

func (*Base) Error

func (resc *Base) Error() error

Error implements resource.Resource.

func (*Base) Header

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

Header implements resource.Resource.

func (*Base) LinkResource

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

LinkResource implements resource.Resource.

func (*Base) LinkResourceCollection

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

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

LinksCollection implements resource.Resource.

func (*Base) OnDelete

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

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

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

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

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

func (resc *Base) Renderer() Renderer

Renderer implements resource.Resource.

func (*Base) SetCode

func (resc *Base) SetCode(code int)

SetCode implements resource.Resource.

func (*Base) SetError

func (resc *Base) SetError(err error)

SetError implements resource.Resource.

func (*Base) SetRenderer

func (resc *Base) SetRenderer(renderer Renderer)

SetRenderer implements resource.Resource.

func (*Base) View

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

View implements resource.Resource.

type Controller

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

func GetController(ctx context.Context) Controller

GetController gets a controller from the context.

type ControllerContextKey

type ControllerContextKey struct{}

ControllerContextKey is a context key.

type Dispatcher

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

Dispatcher handles HTTP request and response.

func NewController

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

NewController creates a controller.

func (*Dispatcher) Dispatch

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

Dispatch implements Controller.

func (*Dispatcher) HandleError

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

HandleError implements Controller.

func (*Dispatcher) IsError

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

IsError implements Controller.

func (*Dispatcher) Logger

func (dispatcher *Dispatcher) Logger() Logger

Logger implements Controller.

func (*Dispatcher) ServeHTTP

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

ServeHTTP implements http.Handler.

func (*Dispatcher) SetDefaultRenderer

func (dispatcher *Dispatcher) SetDefaultRenderer(renderer Renderer)

SetDefaultRenderer implements Controller.

type ErrorHandler

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

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

ErrorResource is an resource to represent the error.

func (*ErrorResource) Href

func (resc *ErrorResource) Href() string

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

func (*ErrorResource) View

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

View implements resource.Resource.

type ErrorView

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

ErrorView is a view for the error.

type ExposedErrorHandler

type ExposedErrorHandler struct {
	Renderer Renderer
}

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

func (*ExposedErrorHandler) HandleError

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

HandleError implements resource.ErrorHandler.

func (*ExposedErrorHandler) IsError

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

IsError implements resource.ErrorHandler.

type HALRenderer

type HALRenderer struct {
}

HALRenderer represents the resource in hal+json format.

func NewHALRenderer

func NewHALRenderer() *HALRenderer

NewHALRenderer creates a new HALRenderer.

func (*HALRenderer) Render

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

Render implements resource.Renderer.

type JSONRenderer

type JSONRenderer struct {
}

JSONRenderer represents the resource in json format.

func NewJSONRenderer

func NewJSONRenderer() *JSONRenderer

NewJSONRenderer creates a new JSONRenderer.

func (*JSONRenderer) Render

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

Render implements resource.Renderer.

type Link struct {
	Href string
}

Link represents a link.

type Logger

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

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

type NilLogger struct {
}

NilLogger is a nil logger implementation.

func (*NilLogger) Debug

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

Debug implements resource.Logger.

func (*NilLogger) Debugf

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

Debugf implements resource.Logger.

func (*NilLogger) Error

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

Error implements resource.Logger.

func (*NilLogger) Errorf

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

Errorf implements resource.Logger.

func (*NilLogger) Fatal

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

Fatal implements resource.Logger.

func (*NilLogger) Fatalf

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

Fatalf implements resource.Logger.

func (*NilLogger) Info

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

Info implements resource.Logger.

func (*NilLogger) Infof

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

Infof implements resource.Logger.

func (*NilLogger) Warn

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

Warn implements resource.Logger.

func (*NilLogger) Warnf

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

Warnf implements resource.Logger.

type Renderer

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

Renderer provides a resource renderer interface.

type Resource

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

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

BindTo binds objects to the resource.

func GetActiveResource

func GetActiveResource(ctx context.Context) Resource

GetActiveResource gets the activated resource from ActiveResourceContainer.

type ResourceContainer

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

ResourceContainer is a container.

func GetActiveResourceContainer

func GetActiveResourceContainer(ctx context.Context) *ResourceContainer

GetActiveResourceContainer gets a resource container.

func NewResourceContainer

func NewResourceContainer() *ResourceContainer

NewResourceContainer creates a new ResourceContainer.

func (*ResourceContainer) Exists

func (rcont *ResourceContainer) Exists() bool

Exists checks whether the container has a resource.

func (*ResourceContainer) Get

func (rcont *ResourceContainer) Get() Resource

Get gets a resource from the container.

func (*ResourceContainer) Set

func (rcont *ResourceContainer) Set(resc Resource)

Set sets a resource to the container.

type ResourceFactory

type ResourceFactory = func(context.Context) Resource

ResourceFactory is a resource factory.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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