Documentation ¶
Index ¶
- Variables
- func JSONRenderer(c *Context, data interface{}) error
- func PlainRenderer(c *Context, data interface{}) error
- type Context
- func (c *Context) Deadline() (deadline time.Time, ok bool)
- func (c *Context) DecodeJSONBody(v interface{}) error
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) Get(key string) interface{}
- func (c *Context) Header() http.Header
- func (c *Context) Next()
- func (c *Context) Param(key string) string
- func (c *Context) Query(key string) string
- func (c *Context) Redirect(url string, code int)
- func (s *Context) Register(service interface{})
- func (c *Context) Render(status int, data interface{})
- func (c *Context) RenderEmpty(status int)
- func (c *Context) Set(key string, value interface{})
- func (c *Context) Status() int
- func (c *Context) Value(key interface{}) interface{}
- func (c *Context) Write(b []byte) (int, error)
- func (c *Context) WriteHeader(status int)
- func (c *Context) Written() int
- type Endpoint
- func (e *Endpoint) Delete(p string, h Handler) *Endpoint
- func (e *Endpoint) Get(p string, h Handler) *Endpoint
- func (e *Endpoint) Handle(requestMethod, p string, h Handler) *Endpoint
- func (e *Endpoint) Options(p string, h Handler) *Endpoint
- func (e *Endpoint) Patch(p string, h Handler) *Endpoint
- func (e *Endpoint) Post(p string, h Handler) *Endpoint
- func (e *Endpoint) Put(p string, h Handler) *Endpoint
- func (s *Endpoint) Register(service interface{})
- func (e *Endpoint) Renderer(r Renderer) *Endpoint
- func (c *Endpoint) Use(middlewares ...Middleware)
- func (c *Endpoint) UseHandler(middlewares ...http.Handler)
- type ErrHandler
- type Handler
- type M
- type Middleware
- type Renderer
- type River
- func (v River) Dump()
- func (rv *River) Handle(p string, e *Endpoint) *River
- func (rv *River) NotAllowed(h Handler) *River
- func (rv *River) NotFound(h Handler) *River
- func (s *River) Register(service interface{})
- func (rv *River) RenderError(h ErrHandler) *River
- func (rv *River) Renderer(r Renderer) *River
- func (rv *River) Run(addr string) error
- func (rv *River) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (c *River) Use(middlewares ...Middleware)
- func (c *River) UseHandler(middlewares ...http.Handler)
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func JSONRenderer ¶
JSONRenderer is json renderer.
func PlainRenderer ¶
PlainRenderer is plain text renderer.
Types ¶
type Context ¶
Context is a request scope context. Context implements http.ResponseWriter and embeds *http.Request.
It can be adapted for use in an http.Handler e.g.
handler.ServeHTTP(c, c.Request)
func (*Context) Deadline ¶
Deadline returns the time when work done on behalf of this context should be canceled. Deadline returns ok==false when no deadline is set. Successive calls to Deadline return the same results.
func (*Context) DecodeJSONBody ¶
DecodeJSONBody decodes the request body as JSON into v.
The request body must be JSON and v must be a pointer to a compatible type for the JSON body.
Type conversion is done if required; based on v's underlying type. If v points to a struct and request body is a json array, an attempt is made to decode to a slice of the struct and the first element of the slice will be stored in v.
Likewise if v points to a slice and request body is a json object, an attempt is made to decode to the item type of the slice and a slice containing the item will be stored in v.
var v []Type // c.DecodeJSONBody(&v) works even if body is a json object. var v Type // c.DecodeJSONBody(&v) works even if body is a json array.
func (*Context) Done ¶
func (c *Context) Done() <-chan struct{}
Done returns a channel that's closed when work done on behalf of this context should be canceled. Done may return nil if this context can never be canceled. Successive calls to Done return the same value.
func (*Context) Err ¶
Err returns a non-nil error value after Done is closed. Err returns Canceled if the context was canceled or DeadlineExceeded if the context's deadline passed. No other values for Err are defined. After Done is closed, successive calls to Err return the same value.
func (*Context) Get ¶
Get gets the value for key in the context. Key must have been previously set using c.Set.
func (*Context) Header ¶
Header returns the header map that will be sent by WriteHeader. Changing the header after a call to WriteHeader (or Write) has no effect unless the modified headers were declared as trailers by setting the "Trailer" header before the call to WriteHeader (see example). To suppress implicit response headers, set their value to nil.
func (*Context) Next ¶
func (c *Context) Next()
Next calls the next handler in the middleware chain. A middleware must call Next, otherwise the request stops at the middleware.
Next has no effect if called in an endpoint handler.
func (*Context) Param ¶
Param returns URL parameters. If key is not found, empty string is returned.
Params are set with :key in the handle path. e.g. /:category/:id
func (*Context) Query ¶
Query returns URL query parameters. If key not found, empty string is returned.
func (*Context) Redirect ¶
Redirect performs HTTP redirect to url with code as redirect code. code must be 3xx, otherwise http.StatusFound (302) will be used.
func (*Context) Register ¶
func (s *Context) Register(service interface{})
Register registers a new service. Services are identifiable by their types. Multiple services of same type should be grouped into a struct, and the struct should be registered instead.
func (*Context) Render ¶
Render renders data using the current endpoint's renderer (if any) or global renderer (if any) or PlainRenderer; in that preference order. status is HTTP status code to respond with.
func (*Context) RenderEmpty ¶
RenderEmpty renders status text for status as body. status is HTTP status code to respond with.
func (*Context) Status ¶
Status returns the response status code. This returns 0 unless response has been written.
func (*Context) Value ¶
func (c *Context) Value(key interface{}) interface{}
Value returns the value associated with this context for key, or nil if no value is associated with key. Successive calls to Value with the same key returns the same result.
func (*Context) Write ¶
Write writes the data to the connection as part of an HTTP reply. If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data. If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType.
func (*Context) WriteHeader ¶
WriteHeader sends an HTTP response header with status code. If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes.
type Endpoint ¶
type Endpoint struct {
// contains filtered or unexported fields
}
Endpoint is a REST endpoint.
func (*Endpoint) Register ¶
func (s *Endpoint) Register(service interface{})
Register registers a new service. Services are identifiable by their types. Multiple services of same type should be grouped into a struct, and the struct should be registered instead.
func (*Endpoint) Use ¶
func (c *Endpoint) Use(middlewares ...Middleware)
Use adds middlewares to the middleware chain.
func (*Endpoint) UseHandler ¶
UseHandler adds any http.Handler as middleware to the middleware chain.
type ErrHandler ¶
ErrHandler handles error returned by Renderer.
type Handler ¶
type Handler interface{}
Handler is an endpoint handler with support for dependency injection. Any function type (including http.Handler) is a valid Handler. Function parameters will be injected accordingly.
If a service is not previously registered and it is not one of *river.Context, http.ResponseWriter and *http.Request, zero value of the type (or nil if the type is a pointer) will be passed as the parameter.
If there is an attempt to register a non function type as a request handler, a panic occurs immediately. This prevents possible runtime panic.
The return values of the function (if any) are discarded.
type M ¶
type M map[string]interface{}
M is a convenience wrapper for map[string]interface{}.
M{"status": "success, "data": M{"id": 1, "type": "complex"}}
type Middleware ¶
type Middleware func(*Context)
Middleware is River middleware. A middleware needs to call c.Next() for request to continue to other middlewares.
func (c *river.Context){ // do something before c.Next() // do something after }
func Recovery ¶
func Recovery(handlers ...func(c *Context, err interface{})) Middleware
Recovery creates a panic recovery middleware. handlers are called after recovery.
type Renderer ¶
Renderer renders data in a specified format. Renderer should set Content-Type accordingly.
type River ¶
type River struct {
// contains filtered or unexported fields
}
River is a REST server handler and toolkit.
func New ¶
func New(middlewares ...Middleware) *River
New creates a new River and initiates with middlewares. Middlewares can also be added with river.Use* methods.
Renderer defaults to JSONRenderer.
func (River) Dump ¶
func (v River) Dump()
Dump dumps all endpoints that are being handled to the log.
func (*River) Handle ¶
Handle handles endpoint at path p. This should only be called after Endpoint requests have been handled.
func (*River) NotAllowed ¶
NotAllowed replaces the default handler for methods not handled by any endpoint with h.
func (*River) NotFound ¶
NotFound replaces the default handler for request paths without any endpoint.
func (*River) Register ¶
func (s *River) Register(service interface{})
Register registers a new service. Services are identifiable by their types. Multiple services of same type should be grouped into a struct, and the struct should be registered instead.
func (*River) RenderError ¶
func (rv *River) RenderError(h ErrHandler) *River
RenderError sets the handler that handles error returned by a Renderer.
func (*River) Use ¶
func (c *River) Use(middlewares ...Middleware)
Use adds middlewares to the middleware chain.
func (*River) UseHandler ¶
UseHandler adds any http.Handler as middleware to the middleware chain.