Documentation ¶
Index ¶
- Constants
- type Cache
- type Context
- func (c *Context) FormValue(name string) string
- func (c *Context) GetClientIP() (ip string)
- func (c *Context) Param(name string) string
- func (c *Context) QueryValue(name string) string
- func (c *Context) Redirect(url string, code int)
- func (c *Context) Render(content string)
- func (c *Context) RenderGzip(content []byte) error
- func (c *Context) RenderGzipJSON(data interface{})
- func (c *Context) RenderGzipXML(data interface{})
- func (c *Context) RenderJSON(data interface{})
- func (c *Context) RenderJSONIndent(data interface{})
- func (c *Context) RenderXML(data interface{})
- func (c *Context) RenderXMLIndent(data interface{})
- func (c *Context) Status(code int)
- type ContextData
- type CustomError
- type GroupRoute
- type GroupRouter
- type MethodNotImplementedError
- type Middleware
- type MiddlewareHandler
- type NotFoundError
- type Params
- type Resource
- func (r *Resource) Connect(c *Context) error
- func (r *Resource) Delete(c *Context) error
- func (r *Resource) Get(c *Context) error
- func (r *Resource) Head(c *Context) error
- func (r *Resource) Options(c *Context) error
- func (r *Resource) Patch(c *Context) error
- func (r *Resource) Post(c *Context) error
- func (r *Resource) Put(c *Context) error
- func (r *Resource) Trace(c *Context) error
- type ResourceHandler
- type RouteCache
- type Router
- type UnexpectedError
- type YError
- type Yarf
Constants ¶
const Version = "0.8.5"
Version string
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
Cache is the service handler for route caching
func (*Cache) Get ¶
func (c *Cache) Get(k string) (rc RouteCache, ok bool)
Get retrieves a routeCache object by key name.
func (*Cache) Set ¶
func (c *Cache) Set(k string, r RouteCache)
Set stores a routeCache object under a key name.
type Context ¶
type Context struct { // The *http.Request object as received by the HandleFunc. Request *http.Request // The http.ResponseWriter object as received by the HandleFunc. Response http.ResponseWriter // Parameters received through URL route Params Params // Free storage to be used freely by apps to their convenience. Data ContextData // contains filtered or unexported fields }
Context is the data/status storage of every YARF request. Every request will instantiate a new Context object and fill in with all the request data. Each request Context will be shared along the entire request life to ensure accesibility of its data at all levels.
func NewContext ¶
func NewContext(r *http.Request, rw http.ResponseWriter) *Context
NewContext creates a new *Context object with default values and returns it.
func (*Context) FormValue ¶
FormValue is a wrapper for c.Request.Form.Get() and it calls c.Request.ParseForm().
func (*Context) GetClientIP ¶
GetClientIP retrieves the client IP address from the request information. It detects common proxy headers to return the actual client's IP and not the proxy's.
func (*Context) QueryValue ¶ added in v0.8.3
QueryValue is a wrapper for c.Request.URL.Query().Get().
func (*Context) Redirect ¶ added in v0.8.3
Redirect sends the corresponding HTTP redirect response with the provided URL and status code. It's just a wrapper for net/http.Redirect()
func (*Context) Render ¶
Render writes a string to the http.ResponseWriter. This is the default renderer that just sends the string to the client. Check other Render[Type] functions for different types.
func (*Context) RenderGzip ¶ added in v0.8.5
RenderGzip takes a []byte content and if the client accepts compressed responses, writes the compressed version of the content to the response. Otherwise it just writes the plain []byte to it.
func (*Context) RenderGzipJSON ¶ added in v0.8.5
func (c *Context) RenderGzipJSON(data interface{})
RenderGzipJSON takes a interface{} object and writes the JSON verion through RenderGzip.
func (*Context) RenderGzipXML ¶ added in v0.8.5
func (c *Context) RenderGzipXML(data interface{})
RenderGzipXML takes a interface{} object and writes the XML verion through RenderGzip.
func (*Context) RenderJSON ¶
func (c *Context) RenderJSON(data interface{})
RenderJSON takes a interface{} object and writes the JSON encoded string of it.
func (*Context) RenderJSONIndent ¶
func (c *Context) RenderJSONIndent(data interface{})
RenderJSONIndent is the indented (beauty) of RenderJSON
func (*Context) RenderXML ¶
func (c *Context) RenderXML(data interface{})
RenderXML takes a interface{} object and writes the XML encoded string of it.
func (*Context) RenderXMLIndent ¶
func (c *Context) RenderXMLIndent(data interface{})
RenderXMLIndent is the indented (beauty) of RenderXML
type ContextData ¶
type ContextData interface { // Get retrieves a data item by it's key name. Get(key string) (interface{}, error) // Set saves a data item under a key name. Set(key string, data interface{}) error // Del removes the data item and key name for a given key. Del(key string) error }
ContextData interface represents a common get/set/del set of methods to handle data storage. Is designed to be used as the Data property of the Context obejct. The Data property is a free storage unit that apps using the framework can implement to their convenience to share context data during a request life. All methods returns an error status that different implementations can design to fulfill their needs.
type CustomError ¶
type CustomError struct { HTTPCode int // HTTP status code to be used as this error response. ErrorCode int // Internal YARF error code for further reference. ErrorMsg string // YARF error message. ErrorBody string // Error content to be rendered to the client response. }
CustomError is the standard error response format used through the framework. Implements Error and YError interfaces
func (*CustomError) Body ¶
func (e *CustomError) Body() string
Body returns the error's content body, if needed, to be returned in the HTTP response.
func (*CustomError) Code ¶
func (e *CustomError) Code() int
Code returns the error's HTTP code to be used in the response.
func (*CustomError) Error ¶
func (e *CustomError) Error() string
Implements the error interface returning the ErrorMsg value of each error.
func (*CustomError) ID ¶
func (e *CustomError) ID() int
ID returns the error's ID for further reference.
func (*CustomError) Msg ¶
func (e *CustomError) Msg() string
Msg returns the error's message, used to implement the Error interface.
type GroupRoute ¶ added in v0.8.0
type GroupRoute struct {
// contains filtered or unexported fields
}
GroupRoute stores routes grouped under a single url prefix.
func RouteGroup ¶
func RouteGroup(url string) *GroupRoute
RouteGroup creates a new GroupRoute object and initializes it with the provided url prefix. The object implements Router interface to being able to handle groups as routes. Groups can be nested into each other, so it's possible to add a GroupRoute as a route inside another GroupRoute. Includes methods to work with middleware.
func (*GroupRoute) Add ¶ added in v0.8.0
func (g *GroupRoute) Add(url string, h ResourceHandler)
Add inserts a new resource with it's associated route into the group object.
func (*GroupRoute) AddGroup ¶ added in v0.8.0
func (g *GroupRoute) AddGroup(r *GroupRoute)
AddGroup inserts a GroupRoute into the routes list of the group object. This makes possible to nest groups.
func (*GroupRoute) Dispatch ¶ added in v0.8.0
func (g *GroupRoute) Dispatch(c *Context) (err error)
Dispatch loops through all routes inside the group and dispatch the one that matches the request. Outside the box, works exactly the same as route.Dispatch().
func (*GroupRoute) Insert ¶ added in v0.8.0
func (g *GroupRoute) Insert(m MiddlewareHandler)
Insert adds a MiddlewareHandler into the middleware list of the group object.
func (*GroupRoute) Match ¶ added in v0.8.0
func (g *GroupRoute) Match(url string, c *Context) bool
Match loops through all routes inside the group and find for one that matches the request. After a match is found, the route matching is stored into Context.groupDispatch to being able to dispatch it directly after a match without looping again. Outside the box, works exactly the same as route.Match()
type GroupRouter ¶
type GroupRouter interface { Router Add(string, ResourceHandler) AddGroup(*GroupRoute) Insert(MiddlewareHandler) }
GroupRouter interface adds methods to work with children routers
type MethodNotImplementedError ¶
type MethodNotImplementedError struct {
CustomError
}
MethodNotImplementedError is used to communicate that a specific HTTP method isn't implemented by a resource.
func ErrorMethodNotImplemented ¶
func ErrorMethodNotImplemented() *MethodNotImplementedError
ErrorMethodNotImplemented creates MethodNotImplementedError
type Middleware ¶
type Middleware struct{}
Middleware struct is the default implementation of a Middleware and does nothing. Users can either implement both methods or composite this struct into their own. Both methods needs to be present to satisfy the MiddlewareHandler interface.
func (*Middleware) End ¶
func (m *Middleware) End(c *Context) error
End will be executed ALWAYS after every request, even if there were errors present.
func (*Middleware) PostDispatch ¶
func (m *Middleware) PostDispatch(c *Context) error
PostDispatch includes code to be executed after every Resource request.
func (*Middleware) PreDispatch ¶
func (m *Middleware) PreDispatch(c *Context) error
PreDispatch includes code to be executed before every Resource request.
type MiddlewareHandler ¶
type MiddlewareHandler interface { PreDispatch(*Context) error PostDispatch(*Context) error End(*Context) error }
MiddlewareHandler interface provides the methods for request filters that needs to run before, or after, every request Resource is executed.
type NotFoundError ¶
type NotFoundError struct {
CustomError
}
NotFoundError is the HTTP 404 error equivalent.
type Params ¶ added in v0.8.0
Params wraps a map[string]string and adds Get/Set/Del methods to work with it. Inspired on url.Values but simpler as it doesn't handles a map[string][]string
type Resource ¶
type Resource struct{}
The Resource type is the representation of each REST resource of the application. It implements the ResourceHandler interface and allows the developer to extend the methods needed. All resources being used by a YARF application have to composite this Resource struct.
func (*Resource) Connect ¶
Connect is the default HTTP CONNECT implementation. It returns a NotImplementedError
func (*Resource) Delete ¶
Delete is the default HTTP DELETE implementation. It returns a NotImplementedError
func (*Resource) Head ¶
Head is the default HTTP HEAD implementation. It returns a NotImplementedError
func (*Resource) Options ¶
Options is the default HTTP OPTIONS implementation. It returns a NotImplementedError
func (*Resource) Patch ¶
Patch is the default HTTP PATCH implementation. It returns a NotImplementedError
func (*Resource) Post ¶
Post is the default HTTP POST implementation. It returns a NotImplementedError
type ResourceHandler ¶
type ResourceHandler interface { // HTTP methods Get(*Context) error Post(*Context) error Put(*Context) error Patch(*Context) error Delete(*Context) error Options(*Context) error Head(*Context) error Trace(*Context) error Connect(*Context) error }
The ResourceHandler interface defines how Resources through the application have to be defined. Ideally, the developer will composite the Resource struct into their own resources, but it's possible to implement each one by their own.
type RouteCache ¶ added in v0.8.0
type RouteCache struct {
// contains filtered or unexported fields
}
RouteCache stores previously matched and parsed routes
type Router ¶
Router interface provides the methods used to handle route and GroupRoute objects.
func Route ¶
func Route(url string, h ResourceHandler) Router
Route returns a new route object initialized with the provided data. Params:
- url string // The route path to handle
- h ResourceHandler // The ResourceHandler object that will process the requests to the url.
type UnexpectedError ¶
type UnexpectedError struct {
CustomError
}
UnexpectedError is used when the origin of the error can't be discovered
func ErrorUnexpected ¶
func ErrorUnexpected() *UnexpectedError
ErrorUnexpected creates UnexpectedError
type YError ¶
type YError interface { Code() int // HTTP response code for this error ID() int // Error code ID. Msg() string // Error description Body() string // Error body content to be returned to the client if needed. }
YError is the interface used to handle error responses inside the framework.
type Yarf ¶
type Yarf struct { // UseCache indicates if the route cache should be used. UseCache bool // Debug enables/disables the debug mode. // On debug mode, extra error information is sent to the client. Debug bool // PanicHandler can store a func() that will be defered by each request to be able to recover(). // If you need to log, send information or do anything about a panic, this is your place. PanicHandler func() GroupRouter // Logger object will be used if present Logger *log.Logger // Follow defines a standard http.Handler implementation to follow if no route matches. Follow http.Handler // NotFound defines a function interface to execute when a NotFound (404) error is thrown. NotFound func(c *Context) // contains filtered or unexported fields }
Yarf is the main entry point for the framework and it centralizes most of the functionality. All configuration actions are handled by this object.
func New ¶
func New() *Yarf
New creates a new yarf and returns a pointer to it. Performs needed initializations
func (*Yarf) ServeHTTP ¶
func (y *Yarf) ServeHTTP(res http.ResponseWriter, req *http.Request)
ServeHTTP Implements http.Handler interface into yarf. Initializes a Context object and handles middleware and route actions. If an error is returned by any of the actions, the flow is stopped and a response is sent. If no route matches, tries to forward the request to the Yarf.Follow (http.Handler type) property if set. Otherwise it returns a 404 response.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
complete
The complete example implements all possible features from YARF.
|
The complete example implements all possible features from YARF. |
static
static example package demonstrates how to easily implement a yarf handler that serves static files using the net/http package.
|
static example package demonstrates how to easily implement a yarf handler that serves static files using the net/http package. |