Documentation ¶
Overview ¶
Package r2router provide a simple router. Suitable for API where you could map parameter from the url.
package main import ( "github.com/vanng822/r2router" "net/http" ) func main() { router := r2router.NewRouter() router.Get("/users/:user", func(w http.ResponseWriter, r *http.Request, p r2router.Params) { w.Write([]byte(p.Get("user"))) }) http.ListenAndServe(":8080", router) }
Index ¶
- Constants
- type After
- type Before
- type Counter
- type GroupRouter
- func (gr *GroupRouter) Delete(path string, handler HandlerFunc)
- func (gr *GroupRouter) Get(path string, handler HandlerFunc)
- func (gr *GroupRouter) Head(path string, handler HandlerFunc)
- func (gr *GroupRouter) Patch(path string, handler HandlerFunc)
- func (gr *GroupRouter) Post(path string, handler HandlerFunc)
- func (gr *GroupRouter) Put(path string, handler HandlerFunc)
- type Handler
- type HandlerFunc
- type M
- type P
- type Params
- type RouteManager
- type Router
- func (r *Router) AddHandler(method, path string, handler HandlerFunc)
- func (r *Router) Delete(path string, handler HandlerFunc)
- func (r *Router) Dump() string
- func (r *Router) Get(path string, handler HandlerFunc)
- func (r *Router) Group(path string, fn func(r *GroupRouter))
- func (r *Router) Head(path string, handler HandlerFunc)
- func (r *Router) Patch(path string, handler HandlerFunc)
- func (r *Router) Post(path string, handler HandlerFunc)
- func (r *Router) Put(path string, handler HandlerFunc)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- type Seefor
- type Stat
- type Stats
- type Timer
Constants ¶
const ( HTTP_METHOD_GET = "GET" HTTP_METHOD_POST = "POST" HTTP_METHOD_DELETE = "DELETE" HTTP_METHOD_OPTIONS = "OPTIONS" HTTP_METHOD_HEAD = "HEAD" HTTP_METHOD_PUT = "PUT" HTTP_METHOD_PATCH = "PATCH" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type After ¶
After defines how a middleware should look like. After middlewares are for handling request after routing. After middlewares are executed in the order they were inserted. A middleware can choose to response to a request and not call next handler
func Wrap ¶
func Wrap(handler HandlerFunc) After
Wrap for wrapping a handler to After middleware Be aware that it will not be able to stop execution propagation That is it will continue to execute the next middleware/handler
func WrapHandler ¶
WrapHandler for wrapping a http.Handler to After middleware. Be aware that it will not be able to stop execution propagation That is it will continue to execute the next middleware/handler
type Before ¶
Before defines middleware interface. Before middlewares are for handling request before routing. Before middlewares are executed in the order they were inserted. A middleware can choose to response to a request and not call next handler
func WrapBeforeHandler ¶
WrapBeforeHandler for wrapping a http.Handler to Before middleware. Be aware that it will not be able to stop execution propagation That is it will continue to execute the next middleware/handler
type Counter ¶
type Counter struct { Count int64 // Number of requests at this endpoint Tot time.Duration // Accumulated total time Max time.Duration // Worst time of all requests Min time.Duration // Best time of all requests BeforeTot time.Duration // Total time of Before middlewares AfterTot time.Duration // Total time of After middlewares }
Statistic entry for one endpoint. Values are atomic updated
func (*Counter) Accumulate ¶
type GroupRouter ¶
type GroupRouter struct {
// contains filtered or unexported fields
}
GroupRouter is a helper for grouping endpoints All methods are proxy of Router Suitable for grouping different methods of an endpoint
func NewGroupRouter ¶
func NewGroupRouter(r *Router, path string) *GroupRouter
NewGroupRouter return GroupRouter which is a helper to construct a group of endpoints, such example could be API-version or different methods for an endpoint You should always use router.Group instead of using this directly
func (*GroupRouter) Delete ¶
func (gr *GroupRouter) Delete(path string, handler HandlerFunc)
func (*GroupRouter) Get ¶
func (gr *GroupRouter) Get(path string, handler HandlerFunc)
func (*GroupRouter) Head ¶
func (gr *GroupRouter) Head(path string, handler HandlerFunc)
func (*GroupRouter) Patch ¶
func (gr *GroupRouter) Patch(path string, handler HandlerFunc)
func (*GroupRouter) Post ¶
func (gr *GroupRouter) Post(path string, handler HandlerFunc)
func (*GroupRouter) Put ¶
func (gr *GroupRouter) Put(path string, handler HandlerFunc)
type Handler ¶
type Handler interface {
ServeHTTP(w http.ResponseWriter, req *http.Request, params Params)
}
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, req *http.Request, params Params)
HandlerFunc define interface handler
func (HandlerFunc) ServeHTTP ¶
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, req *http.Request, params Params)
type M ¶
type M map[string]interface{}
Shortcut for map[string]interface{} Helpful to build data for json response
type Params ¶
type Params interface { // Get returns param value for the given key Get(key string) string // Has is for checking if parameter value exists // for given key Has(key string) bool // AppSet is for application to set own data AppSet(key interface{}, val interface{}) // AppGet returns the value from AppSet AppGet(key interface{}) interface{} // AppHas is for checking if the application // has set data for given key AppHas(key interface{}) bool }
Params is for parameters that are matched from URL. It is also brigde to forward data from middleware. An Example could be that a middleware to identify the user API key, verify and get user data
type RouteManager ¶
type RouteManager interface { // For setting baseurl if one needs full url SetBaseUrl(baseUrl string) // Register a route and return the path // This can be good for adding and register handler at the same time // router.Get(rm.Add("user", "/user/:id"), handler) Add(routeName, path string) string // Return the path for a specific route name // Use for register handler // router.Delete(rm.PathFor("user"), handler) PathFor(routeName string) string // Returning url for given route name and provided data // Will panic if missmatched UrlFor(routeName string, params map[string][]string) string // Returning url for given path and provided data // Will panic if missmatched UrlForPath(path string, params map[string][]string) string }
For managing route and getting url
func NewRouteManager ¶
func NewRouteManager() RouteManager
type Router ¶
type Router struct { HandleMethodNotAllowed bool MethodNotAllowed http.HandlerFunc NotFound http.HandlerFunc // contains filtered or unexported fields }
func (*Router) AddHandler ¶
func (r *Router) AddHandler(method, path string, handler HandlerFunc)
func (*Router) Delete ¶
func (r *Router) Delete(path string, handler HandlerFunc)
func (*Router) Get ¶
func (r *Router) Get(path string, handler HandlerFunc)
func (*Router) Group ¶
func (r *Router) Group(path string, fn func(r *GroupRouter))
Group takes a path which typically a prefix for an endpoint It will call callback function with a group router which you can add handler for different request methods
func (*Router) Head ¶
func (r *Router) Head(path string, handler HandlerFunc)
func (*Router) Patch ¶
func (r *Router) Patch(path string, handler HandlerFunc)
func (*Router) Post ¶
func (r *Router) Post(path string, handler HandlerFunc)
func (*Router) Put ¶
func (r *Router) Put(path string, handler HandlerFunc)
type Seefor ¶
type Seefor struct { Router // contains filtered or unexported fields }
Seefor is a subtype of Router. It supports a simple middleware layers. Middlewares are always executed before handler, no matter where or when they are added. And middlewares are executed in the order they were inserted.
func NewSeeforRouter ¶
func NewSeeforRouter() *Seefor
NewSeeforRouter for creating a new instance of Seefor router
type Stat ¶
type Stat struct { Route string `json:"route"` Count int64 `json:"count"` Tot time.Duration `json:"tot"` Max time.Duration `json:"max"` Min time.Duration `json:"min"` Avg time.Duration `json:"avg"` AvgBefore time.Duration `json:"avg_before"` AvgAfter time.Duration `json:"avg_after"` }
Tot, Max, Min and Avg is time.Duration which mean in nanoseconds
type Stats ¶
type Stats struct { Generated time.Time `json:"generated"` UpTime string `json:"upTime"` Result []*Stat `json:"result"` SortBy string `json:"sortBy"` }
For generate statistics