Documentation ¶
Overview ¶
Package resozyme provides a resource-oriented http handler interface.
Index ¶
- func ActivateResource(ctx context.Context, resc Resource) error
- func Route(mux Mux, path string, fac ResourceFactory)
- func WithActiveResourceContainer(ctx context.Context, rcont *ResourceContainer) context.Context
- func WithController(ctx context.Context, rcont Controller) context.Context
- type ActiveResourceContainerContextKey
- type Base
- func (resc *Base) AddLink(rel string, l Link)
- func (resc *Base) AddLinkCollection(rel string, l []Link)
- func (resc *Base) Bind(i interface{})
- func (resc *Base) Code() int
- func (resc *Base) Context() context.Context
- func (resc *Base) Embed(rel string, er Resource)
- func (resc *Base) EmbedCollection(rel string, er []Resource)
- func (resc *Base) Embedded() map[string]Resource
- func (resc *Base) EmbeddedCollection() map[string][]Resource
- func (resc *Base) Error() error
- func (resc *Base) Header() http.Header
- func (resc *Base) LinkResource(rel string, aResc Resource)
- func (resc *Base) LinkResourceCollection(rel string, aResc []Resource)
- func (resc *Base) Links() map[string]Link
- func (resc *Base) LinksCollection() map[string][]Link
- func (resc *Base) OnDelete(w http.ResponseWriter, r *http.Request)
- func (resc *Base) OnGet(w http.ResponseWriter, r *http.Request)
- func (resc *Base) OnPatch(w http.ResponseWriter, r *http.Request)
- func (resc *Base) OnPost(w http.ResponseWriter, r *http.Request)
- func (resc *Base) OnPut(w http.ResponseWriter, r *http.Request)
- func (resc *Base) Renderer() Renderer
- func (resc *Base) SetCode(code int)
- func (resc *Base) SetError(err error)
- func (resc *Base) SetRenderer(renderer Renderer)
- func (resc *Base) View() interface{}
- type Controller
- type ControllerContextKey
- type Dispatcher
- func (dispatcher *Dispatcher) Dispatch(resc Resource, w http.ResponseWriter, r *http.Request)
- func (dispatcher *Dispatcher) HandleError(resc Resource, w http.ResponseWriter, r *http.Request) Resource
- func (dispatcher *Dispatcher) IsError(resc Resource) bool
- func (dispatcher *Dispatcher) Logger() Logger
- func (dispatcher *Dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (dispatcher *Dispatcher) SetDefaultRenderer(renderer Renderer)
- type ErrorHandler
- type ErrorResource
- type ErrorView
- type ExposedErrorHandler
- type HALRenderer
- type JSONRenderer
- type Link
- type Logger
- type Mux
- type NilLogger
- func (logger *NilLogger) Debug(args ...interface{})
- func (logger *NilLogger) Debugf(template string, args ...interface{})
- func (logger *NilLogger) Error(args ...interface{})
- func (logger *NilLogger) Errorf(template string, args ...interface{})
- func (logger *NilLogger) Fatal(args ...interface{})
- func (logger *NilLogger) Fatalf(template string, args ...interface{})
- func (logger *NilLogger) Info(args ...interface{})
- func (logger *NilLogger) Infof(template string, args ...interface{})
- func (logger *NilLogger) Warn(args ...interface{})
- func (logger *NilLogger) Warnf(template string, args ...interface{})
- type Renderer
- type Resource
- type ResourceContainer
- type ResourceFactory
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ActivateResource ¶
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 (*Base) AddLinkCollection ¶
AddLinkCollection implements resource.Resource.
func (*Base) EmbedCollection ¶
EmbedCollection implements resource.Resource.
func (*Base) EmbeddedCollection ¶
EmbeddedCollection implements resource.Resource.
func (*Base) LinkResource ¶
LinkResource implements resource.Resource.
func (*Base) LinkResourceCollection ¶
LinkResourceCollection implements resource.Resource.
func (*Base) LinksCollection ¶
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) SetRenderer ¶
SetRenderer 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.
type JSONRenderer ¶
type JSONRenderer struct { }
JSONRenderer represents the resource in json format.
func NewJSONRenderer ¶
func NewJSONRenderer() *JSONRenderer
NewJSONRenderer creates a new JSONRenderer.
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) Error ¶
func (logger *NilLogger) Error(args ...interface{})
Error implements resource.Logger.
func (*NilLogger) Fatal ¶
func (logger *NilLogger) Fatal(args ...interface{})
Fatal implements resource.Logger.
func (*NilLogger) Info ¶
func (logger *NilLogger) Info(args ...interface{})
Info 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 GetActiveResource ¶
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 ¶
ResourceFactory is a resource factory.