brouter

package module
v0.0.0-...-012183d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2016 License: Apache-2.0 Imports: 7 Imported by: 0

README

byst-router

a light router for fast-http

preview

router.go

r := brouter.New()
r.Use(func(c *brouter.Context) {
    log.Print("in root")
})
r.Any("/", func(c *brouter.Context) {
    log.Print("black")
})
r.Post("/api",func(c *brouter.Context){
    log.Print("/api")
})
r.Group("/v1",func(r *brouter.Node){
    r.Get("/user",func(c *brouter.Context){
        log.Print("/v1/user")
    })
    r.Post("/user/(id)",func(c *brouter.Context){
        log.Print("/v1/user/",c.Param("id"))
    })
    r.Get(`/article/(id:\d+?)`,func(c *brouter.Context){
        log.Print("/v1/article/",c.Param("id"))
    })
    r.Controller("/news",NewsController{})    
}).Use(func (c *brouter.Context){
    log.Print("group /v1 handler")
}).UseToChild(func (c *brouter.Context){
    log.Print("end handler")
})


fasthttp.ListenAndServe(":8081", r.Init())

news_controller.go

type NewsController struct{}
// match url '/v1/news/id' , method is GET 
func (p *NewsController) GETid(c *brouter.Context) {}
// match url '/v1/news/getLast' , method is POST
func (p *NewsController) POST_GetLast(c *brouter.Context) {}
// match url '/v1/news/find' ,method is ANY if func name is not start with POST,GET,PUT or DELETE
func (p *NewsController) Find(c *brouter.Context) {}
// match url '/v1/news/set' ,method is ANY
func (p *NewsController) Any_Set(c *brouter.Context) {}

About Use

you can use too many handle to dispose a request, like middleware.

r.Use(m1).Get("/user",h).Use(m2)
// m1 -> h -> m2

handle be run order by depth

r.Use(m1)
a:=r.Group("/api")
a.Use(m2)
a.Get("/user",h).Use(m3)
a.Use(m4)
// h is a child of a, a's handler always run first
// m1 -> m2 -> h -> m4 -> m3

you can use UerToChild set handle to he's child's finally

r.Use(m1)
a:=r.Group("/api")
a.Use(m2)
a.Get("/user",h).Use(m3)
a.UseToChild(m4)
// m1 -> m2 -> h -> m3 -> m4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var METHOD_ANY = util.S2B("ANY")
View Source
var METHOD_DELETE = util.S2B("DELETE")
View Source
var METHOD_GET = util.S2B("GET")
View Source
var METHOD_HEAD = util.S2B("HEAD")
View Source
var METHOD_OPTION = util.S2B("OPTION")
View Source
var METHOD_POST = util.S2B("POST")
View Source
var METHOD_PUT = util.S2B("PUT")

Functions

This section is empty.

Types

type Context

type Context struct {
	*fasthttp.RequestCtx
	// contains filtered or unexported fields
}

func (*Context) Abort

func (c *Context) Abort()

func (*Context) Get

func (c *Context) Get(key string) interface{}

get data in the context

func (*Context) Param

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

get param from url

func (*Context) Set

func (c *Context) Set(key string, obj interface{})

set data to the context

type Handler

type Handler func(c *Context)

type Node

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

use a tree struct to save all url node

func (*Node) Any

func (p *Node) Any(path string, h ...Handler) *Node

func (*Node) Controller

func (p *Node) Controller(path string, c interface{}) *Node

func (*Node) Delete

func (p *Node) Delete(path string, h ...Handler) *Node

func (*Node) Get

func (p *Node) Get(path string, h ...Handler) *Node

func (*Node) Group

func (p *Node) Group(path string, funs ...func(node *Node)) *Node

if u want use index format to code router u can set funs param and code in it

func (*Node) Head

func (p *Node) Head(path string, h ...Handler) *Node

func (*Node) Option

func (p *Node) Option(path string, h ...Handler) *Node

func (*Node) Post

func (p *Node) Post(path string, h ...Handler) *Node

func (*Node) Put

func (p *Node) Put(path string, h ...Handler) *Node

func (*Node) Use

func (p *Node) Use(h ...Handler) *Node

add a handle to handle list

func (*Node) UseToChild

func (p *Node) UseToChild(h ...Handler) *Node

type Router

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

func New

func New() *Router

func (*Router) Any

func (p *Router) Any(path string, h ...Handler) *Node

return self

func (*Router) Controller

func (p *Router) Controller(path string, c interface{}) *Node

func (*Router) Delete

func (p *Router) Delete(path string, h ...Handler) *Node

func (*Router) Get

func (p *Router) Get(path string, h ...Handler) *Node

func (*Router) Group

func (p *Router) Group(path string, funs ...func(node *Node)) *Node

return child

func (*Router) Head

func (p *Router) Head(path string, h ...Handler) *Node

func (*Router) Init

func (p *Router) Init() func(ctx *fasthttp.RequestCtx)

func (*Router) Option

func (p *Router) Option(path string, h ...Handler) *Node

func (*Router) Post

func (p *Router) Post(path string, h ...Handler) *Node

func (*Router) Put

func (p *Router) Put(path string, h ...Handler) *Node

func (*Router) Use

func (p *Router) Use(h ...Handler) *Node

func (*Router) UseToChild

func (p *Router) UseToChild(h ...Handler) *Node

func (*Router) When404

func (p *Router) When404(h Handler)

set 404 handler

func (*Router) When405

func (p *Router) When405(h Handler)

set 404 handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL