Documentation
¶
Overview ¶
The httpcrud package defines a number of interfaces and types that provide an alternative, idiosyncratic approach to writing HTTP handlers in Go.
Index ¶
- Variables
- func ExecuteAction(a Action) error
- func InitRouter(r *route.Router, routes RouteList, opts RouteOptions)
- func InitServeMux(m *http.ServeMux, routes RouteList, opts RouteOptions)
- type Action
- type ErrorHandler
- type Handler
- type HandlerInitializer
- type HandlerInitializerAdapter
- type NopAction
- type NopHandler
- type RouteList
- type RouteOptions
Constants ¶
This section is empty.
Variables ¶
var IsDone done
The IsDone value acts as a "signal" for the ExecuteAction function. By implementing the error interface IsDone can be returned by an Action's method to indicate that the execution should skip to, and invoke, the Action's Done method without calling any of the Action's remaining methods.
Functions ¶
func ExecuteAction ¶
ExecuteAction executes the given Action returning the error that the Action's Done method returned, if any.
func InitRouter ¶
func InitRouter(r *route.Router, routes RouteList, opts RouteOptions)
InitRouter takes the HandlerInitializers in the provided RouteList and registers them as route.Handlers with the given *route.Router.
func InitServeMux ¶
func InitServeMux(m *http.ServeMux, routes RouteList, opts RouteOptions)
InitServeMux takes the HandlerInitializers in the provided RouteList and registers them as http.Handlers with the given *http.ServeMux.
NOTE(mkopriva): InitServeMux can be called only once per *http.ServeMux.
Types ¶
type Action ¶
type Action interface {
BeforeValidate() error // Prepare for input validation.
Validate() error // Validate the input.
AfterValidate() error // Post processing after the input has been validated.
BeforeExecute() error // Prepare for main task execution.
Execute() error // Execute the main task.
AfterExecute() error // Post processing after the main task has been executed.
// Done is the last of the Action's methods to be invoked, it is special
// in that regard that it will be invoked irrespective of whether or not
// one of the preceding methods returned an error.
//
// The in error parameter will either be nil or it will hold the error
// value returned by one of the preceding methods. The out error paremeter
// is used as the final return value of the ExecuteAction function and this
// gives Done the ability to override the error returned from one of those
// preceding methods if need be.
//
// The IsDone value, when returned by any of the preceding methods,
// will NOT be passed into Done.
Done(in error) (out error)
}
Action wraps a set of methods that are executed in sequence to accomplish the Action's objective. Each of the Action's methods is intended to implement a subset of the work that the Action needs to do to accomplish the objective. The kind of work that a method should perform is indicated by the method's name, however this is nonbinding and it is left to the developer's judgement to decide which method should do what subset of the Action's work.
If, during execution, any of the Action's methods returns an error the execution will skip over to, and invoke, the Done method passing it the error value and leaving the methods in-between uninvoked.
type ErrorHandler ¶
type ErrorHandler interface {
HandleError(w http.ResponseWriter, r *http.Request, err error)
}
ErrorHandler interface is used to handle the errors returned from Handlers. When one of the Handler's methods returns an error, that error will be passed in to the ErrorHandler which then has a chance to format the error response as it sees fit.
If no ErrorHandler is provided, then by default the http.Error function is used to write the response using the err.Error() as the response text and the http.StatusBadRequest as the status code.
type Handler ¶
type Handler interface {
// Authenticate and authorize the incoming request.
AuthCheck(r *http.Request, c context.Context) error
// Read the input from the incoming request (headers, url, query, body).
ReadRequest(r *http.Request, c context.Context) error
// Prepare the response. In the general case this is unnecessary and
// most handlers do not need to do anything in this method.
//
// However this method becomes useful when one needs to stream data,
// more specifically it allows the Handler to setup the writer as the
// destination for the data and then, piece by piece, the Handler can
// send the data to the writer as it is being retrieved.
InitResponse(w http.ResponseWriter) error
// The meat of the Handler.
Action
// Write the output to the response (headers, body).
WriteResponse(w http.ResponseWriter, r *http.Request) error
}
Handler wraps a set of methods that are executed in sequence to handle an incoming HTTP request. Each of the Handler's methods is intended to implement a subset of the work that the Handler needs to do to handle the request. The kind of the work that a method should perform is indicated by the method's name, however this is nonbinding and it is left to the developer's judgement to decide which method should do what subset of the Handler's work.
If, during execution, any of the Handler's methods returns an error the execution will stop and return that error, leaving all of the subsequent methods uninvoked.
type HandlerInitializer ¶
type HandlerInitializer interface {
// Init returns a Handler instance (in general a new one).
//
// Init will be invoked once for every incoming request and the returned
// Handler will be used to handle that request. As long as Init is implemented
// to return a new Handler instance everytime it's invoked it is guaranteed
// that each request will be handled by their own Handler instance.
//
// The given request can be used by the initializer to decide what type
// of Handler to initialize. For example one could implement a common
// version-specific handler initializer that would, based on the value
// of the Accept header, choose which version of the handler to initialize.
//
// type VersionHandlerInitializer struct {
// V0 HandlerInitializer
// V1 HandlerInitializer
// V2 HandlerInitializer
// }
//
// func (v VersionHandlerInitializer) Init(r *http.Request) Handler {
// switch r.Header.Get("Accept") {
// case "application/vnd.example.v1":
// return v.V1.Init(r)
// case "application/vnd.example.v2":
// return v.V2.Init(r)
// }
// return v.V0.Init(r)
// }
Init(r *http.Request) Handler
}
HandlerInitializer is an interface used for initializing new, request specific, instances of the Handler interface.
type HandlerInitializerAdapter ¶
type HandlerInitializerAdapter interface {
AdaptHandlerInitializer(hi interface{}, path, method string) HandlerInitializer
}
HandlerInitializerAdapter converts a custom, user-defined, handler initializer into a value that implements the httpcrud.HandlerInitializer interface.
type NopAction ¶
type NopAction struct {
// contains filtered or unexported fields
}
NopAction is a noop helper type that can be embedded by user defined types that are intended to implement the Action interface but do not need to, nor want to, declare every single one of its methods.
type NopHandler ¶
type NopHandler struct {
// contains filtered or unexported fields
}
NopHandler is a noop helper type that can be embedded by user defined types that are intended to implement the Handler interface but do not need to, nor want to, declare every single one of its methods.
func (NopHandler) InitResponse ¶
func (NopHandler) InitResponse(_ http.ResponseWriter) error
This method is a no-op.
func (NopHandler) ReadRequest ¶
This method is a no-op.
func (NopHandler) WriteResponse ¶
func (NopHandler) WriteResponse(_ http.ResponseWriter, _ *http.Request) error
This method is a no-op.
type RouteList ¶
type RouteList []struct {
// The path pattern for which to register the HandlerInitializer.
Path string
// The HTTP method for which to register the HandlerInitializer.
Method string
// The HandlerInitializer to be registered.
HandlerInitializer interface{}
}
RouteList is a list of settings used to register HandlerInitializers for the specified paths.
type RouteOptions ¶
type RouteOptions struct {
// The HandlerInitializerAdapter to be used to convert the custom
// route Handler value into an httpcrud.Handler.
HandlerInitializerAdapter HandlerInitializerAdapter
// The ErrorHandler to be used to handle errors returned
// from the route Handlers.
ErrorHandler ErrorHandler
// The prefix to be applied to the routes' paths.
PathPrefix string
}
RouteOptions is a set of options that, if set, will be applied to each route being registered.