yarf: github.com/yarf-framework/yarf Index | Files

package yarf

import "github.com/yarf-framework/yarf"

Index

Package Files

cache.go context.go errors.go middleware.go resource.go router.go yarf.go

Constants

const Version = "0.8.5"

Version string

type Cache Uses

type Cache struct {

    // Sync Mutex
    sync.RWMutex
    // contains filtered or unexported fields
}

Cache is the service handler for route caching

func NewCache Uses

func NewCache() *Cache

NewCache creates and initializes a new Cache service object.

func (*Cache) Get Uses

func (c *Cache) Get(k string) (rc RouteCache, ok bool)

Get retrieves a routeCache object by key name.

func (*Cache) Set Uses

func (c *Cache) Set(k string, r RouteCache)

Set stores a routeCache object under a key name.

type Context Uses

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 Uses

func NewContext(r *http.Request, rw http.ResponseWriter) *Context

NewContext creates a new *Context object with default values and returns it.

func (*Context) FormValue Uses

func (c *Context) FormValue(name string) string

FormValue is a wrapper for c.Request.Form.Get() and it calls c.Request.ParseForm().

func (*Context) GetClientIP Uses

func (c *Context) GetClientIP() (ip string)

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) Param Uses

func (c *Context) Param(name string) string

Param is a wrapper for c.Params.Get()

func (*Context) QueryValue Uses

func (c *Context) QueryValue(name string) string

QueryValue is a wrapper for c.Request.URL.Query().Get().

func (*Context) Redirect Uses

func (c *Context) Redirect(url string, code int)

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 Uses

func (c *Context) Render(content string)

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 Uses

func (c *Context) RenderGzip(content []byte) error

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 Uses

func (c *Context) RenderGzipJSON(data interface{})

RenderGzipJSON takes a interface{} object and writes the JSON verion through RenderGzip.

func (*Context) RenderGzipXML Uses

func (c *Context) RenderGzipXML(data interface{})

RenderGzipXML takes a interface{} object and writes the XML verion through RenderGzip.

func (*Context) RenderJSON Uses

func (c *Context) RenderJSON(data interface{})

RenderJSON takes a interface{} object and writes the JSON encoded string of it.

func (*Context) RenderJSONIndent Uses

func (c *Context) RenderJSONIndent(data interface{})

RenderJSONIndent is the indented (beauty) of RenderJSON

func (*Context) RenderXML Uses

func (c *Context) RenderXML(data interface{})

RenderXML takes a interface{} object and writes the XML encoded string of it.

func (*Context) RenderXMLIndent Uses

func (c *Context) RenderXMLIndent(data interface{})

RenderXMLIndent is the indented (beauty) of RenderXML

func (*Context) Status Uses

func (c *Context) Status(code int)

Status sets the HTTP status code to be returned on the response.

type ContextData Uses

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 Uses

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 Uses

func (e *CustomError) Body() string

Body returns the error's content body, if needed, to be returned in the HTTP response.

func (*CustomError) Code Uses

func (e *CustomError) Code() int

Code returns the error's HTTP code to be used in the response.

func (*CustomError) Error Uses

func (e *CustomError) Error() string

Implements the error interface returning the ErrorMsg value of each error.

func (*CustomError) ID Uses

func (e *CustomError) ID() int

ID returns the error's ID for further reference.

func (*CustomError) Msg Uses

func (e *CustomError) Msg() string

Msg returns the error's message, used to implement the Error interface.

type GroupRoute Uses

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

GroupRoute stores routes grouped under a single url prefix.

func RouteGroup Uses

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 Uses

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 Uses

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 Uses

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 Uses

func (g *GroupRoute) Insert(m MiddlewareHandler)

Insert adds a MiddlewareHandler into the middleware list of the group object.

func (*GroupRoute) Match Uses

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 Uses

type GroupRouter interface {
    Router
    Add(string, ResourceHandler)
    AddGroup(*GroupRoute)
    Insert(MiddlewareHandler)
}

GroupRouter interface adds methods to work with children routers

type MethodNotImplementedError Uses

type MethodNotImplementedError struct {
    CustomError
}

MethodNotImplementedError is used to communicate that a specific HTTP method isn't implemented by a resource.

func ErrorMethodNotImplemented Uses

func ErrorMethodNotImplemented() *MethodNotImplementedError

ErrorMethodNotImplemented creates MethodNotImplementedError

type Middleware Uses

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 Uses

func (m *Middleware) End(c *Context) error

End will be executed ALWAYS after every request, even if there were errors present.

func (*Middleware) PostDispatch Uses

func (m *Middleware) PostDispatch(c *Context) error

PostDispatch includes code to be executed after every Resource request.

func (*Middleware) PreDispatch Uses

func (m *Middleware) PreDispatch(c *Context) error

PreDispatch includes code to be executed before every Resource request.

type MiddlewareHandler Uses

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 Uses

type NotFoundError struct {
    CustomError
}

NotFoundError is the HTTP 404 error equivalent.

func ErrorNotFound Uses

func ErrorNotFound() *NotFoundError

ErrorNotFound creates NotFoundError

type Params Uses

type Params map[string]string

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

func (Params) Del Uses

func (p Params) Del(key string)

Del deletes the values associated with key.

func (Params) Get Uses

func (p Params) Get(key string) string

Get gets the first value associated with the given key. If there are no values associated with the key, Get returns the empty string.

func (Params) Set Uses

func (p Params) Set(key, value string)

Set sets the key to value. It replaces any existing values.

type Resource Uses

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 Uses

func (r *Resource) Connect(c *Context) error

Connect is the default HTTP CONNECT implementation. It returns a NotImplementedError

func (*Resource) Delete Uses

func (r *Resource) Delete(c *Context) error

Delete is the default HTTP DELETE implementation. It returns a NotImplementedError

func (*Resource) Get Uses

func (r *Resource) Get(c *Context) error

Get is the default HTTP GET implementation. It returns a NotImplementedError

func (*Resource) Head Uses

func (r *Resource) Head(c *Context) error

Head is the default HTTP HEAD implementation. It returns a NotImplementedError

func (*Resource) Options Uses

func (r *Resource) Options(c *Context) error

Options is the default HTTP OPTIONS implementation. It returns a NotImplementedError

func (*Resource) Patch Uses

func (r *Resource) Patch(c *Context) error

Patch is the default HTTP PATCH implementation. It returns a NotImplementedError

func (*Resource) Post Uses

func (r *Resource) Post(c *Context) error

Post is the default HTTP POST implementation. It returns a NotImplementedError

func (*Resource) Put Uses

func (r *Resource) Put(c *Context) error

Put is the default HTTP PUT implementation. It returns a NotImplementedError

func (*Resource) Trace Uses

func (r *Resource) Trace(c *Context) error

Trace is the default HTTP TRACE implementation. It returns a NotImplementedError

type ResourceHandler Uses

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 Uses

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

RouteCache stores previously matched and parsed routes

type Router Uses

type Router interface {
    Match(string, *Context) bool
    Dispatch(*Context) error
}

Router interface provides the methods used to handle route and GroupRoute objects.

func Route Uses

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 Uses

type UnexpectedError struct {
    CustomError
}

UnexpectedError is used when the origin of the error can't be discovered

func ErrorUnexpected Uses

func ErrorUnexpected() *UnexpectedError

ErrorUnexpected creates UnexpectedError

type YError Uses

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 Uses

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 Uses

func New() *Yarf

New creates a new yarf and returns a pointer to it. Performs needed initializations

func (*Yarf) ServeHTTP Uses

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.

func (*Yarf) Start Uses

func (y *Yarf) Start(address string)

Start initiates a new http yarf server and start listening. It's a shortcut for http.ListenAndServe(address, y)

func (*Yarf) StartTLS Uses

func (y *Yarf) StartTLS(address, cert, key string)

StartTLS initiates a new http yarf server and starts listening to HTTPS requests. It is a shortcut for http.ListenAndServeTLS(address, cert, key, yarf)

Package yarf imports 9 packages (graph) and is imported by 9 packages. Updated 2019-03-18. Refresh now. Tools for package owners.