Documentation ¶
Index ¶
- type Context
- type ContextBeforeLookup
- func (c *ContextBeforeLookup) ClientAddr() string
- func (c *ContextBeforeLookup) Context() context.Context
- func (c *ContextBeforeLookup) Data(data interface{}, err error)
- func (c *ContextBeforeLookup) Flush()
- func (c *ContextBeforeLookup) Get(key string) interface{}
- func (c *ContextBeforeLookup) GetError() error
- func (c *ContextBeforeLookup) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (c *ContextBeforeLookup) Json(data interface{})
- func (c *ContextBeforeLookup) Json2(data interface{}, err error)
- func (c *ContextBeforeLookup) Ok(message string)
- func (c *ContextBeforeLookup) Origin() string
- func (c *ContextBeforeLookup) ParseForm() error
- func (c *ContextBeforeLookup) Redirect(url string)
- func (c *ContextBeforeLookup) RequestBody() ([]byte, error)
- func (c *ContextBeforeLookup) RequestId() string
- func (c *ContextBeforeLookup) ResponseBody() []byte
- func (c *ContextBeforeLookup) ResponseBodySize() int64
- func (c *ContextBeforeLookup) Scheme() string
- func (c *ContextBeforeLookup) Set(key string, value interface{})
- func (c *ContextBeforeLookup) SetError(err error)
- func (c *ContextBeforeLookup) Status() int64
- func (c *ContextBeforeLookup) StatusJson(status int, data interface{})
- func (c *ContextBeforeLookup) Url() string
- func (c *ContextBeforeLookup) Write(content []byte) (int, error)
- type HandlerFuncs
- type Router
- type RouterGroup
- func (g *RouterGroup) Add(method, path string, handler interface{}, args ...interface{}) *RouterGroup
- func (g *RouterGroup) Child(path string, descs ...string) *RouterGroup
- func (g *RouterGroup) Delete(path string, handler interface{}, args ...interface{}) *RouterGroup
- func (g *RouterGroup) DocDir(dir string) *RouterGroup
- func (g *RouterGroup) Get(path string, handler interface{}, args ...interface{}) *RouterGroup
- func (g *RouterGroup) GetPost(path string, handler interface{}, args ...interface{}) *RouterGroup
- func (g *RouterGroup) Group(path string, descs ...string) *RouterGroup
- func (g *RouterGroup) Lookup(method, path string) (HandlerFuncs, []string)
- func (g *RouterGroup) Patch(path string, handler interface{}, args ...interface{}) *RouterGroup
- func (g *RouterGroup) Post(path string, handler interface{}, args ...interface{}) *RouterGroup
- func (g *RouterGroup) Put(path string, handler interface{}, args ...interface{}) *RouterGroup
- func (g *RouterGroup) RoutesString() string
- func (g *RouterGroup) String() string
- func (g *RouterGroup) Use(handlers ...func(*Context)) *RouterGroup
- func (g *RouterGroup) Watch(watchers ...func(method, fullPath string, args []interface{}) func(*Context)) *RouterGroup
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { ContextBeforeLookup // contains filtered or unexported fields }
func (*Context) Next ¶
func (c *Context) Next()
run the next midllware or route handler.
Example ¶
c := &Context{} c.Next() c.Next()
Output:
type ContextBeforeLookup ¶
type ContextBeforeLookup struct { *http.Request http.ResponseWriter // contains filtered or unexported fields }
func (*ContextBeforeLookup) ClientAddr ¶
func (c *ContextBeforeLookup) ClientAddr() string
func (*ContextBeforeLookup) Context ¶
func (c *ContextBeforeLookup) Context() context.Context
func (*ContextBeforeLookup) Data ¶
func (c *ContextBeforeLookup) Data(data interface{}, err error)
func (*ContextBeforeLookup) Flush ¶
func (c *ContextBeforeLookup) Flush()
func (*ContextBeforeLookup) Get ¶
func (c *ContextBeforeLookup) Get(key string) interface{}
func (*ContextBeforeLookup) GetError ¶
func (c *ContextBeforeLookup) GetError() error
func (*ContextBeforeLookup) Hijack ¶
func (c *ContextBeforeLookup) Hijack() (net.Conn, *bufio.ReadWriter, error)
func (*ContextBeforeLookup) Json ¶
func (c *ContextBeforeLookup) Json(data interface{})
func (*ContextBeforeLookup) Json2 ¶
func (c *ContextBeforeLookup) Json2(data interface{}, err error)
func (*ContextBeforeLookup) Ok ¶
func (c *ContextBeforeLookup) Ok(message string)
func (*ContextBeforeLookup) Origin ¶
func (c *ContextBeforeLookup) Origin() string
func (*ContextBeforeLookup) ParseForm ¶
func (c *ContextBeforeLookup) ParseForm() error
func (*ContextBeforeLookup) Redirect ¶
func (c *ContextBeforeLookup) Redirect(url string)
func (*ContextBeforeLookup) RequestBody ¶
func (c *ContextBeforeLookup) RequestBody() ([]byte, error)
func (*ContextBeforeLookup) RequestId ¶
func (c *ContextBeforeLookup) RequestId() string
func (*ContextBeforeLookup) ResponseBody ¶
func (c *ContextBeforeLookup) ResponseBody() []byte
func (*ContextBeforeLookup) ResponseBodySize ¶
func (c *ContextBeforeLookup) ResponseBodySize() int64
func (*ContextBeforeLookup) Scheme ¶
func (c *ContextBeforeLookup) Scheme() string
func (*ContextBeforeLookup) Set ¶
func (c *ContextBeforeLookup) Set(key string, value interface{})
func (*ContextBeforeLookup) SetError ¶
func (c *ContextBeforeLookup) SetError(err error)
func (*ContextBeforeLookup) Status ¶
func (c *ContextBeforeLookup) Status() int64
func (*ContextBeforeLookup) StatusJson ¶
func (c *ContextBeforeLookup) StatusJson(status int, data interface{})
func (*ContextBeforeLookup) Url ¶
func (c *ContextBeforeLookup) Url() string
type HandlerFuncs ¶
type HandlerFuncs []func(*Context)
func (HandlerFuncs) String ¶
func (handlers HandlerFuncs) String() string
to print regexptree.Node in unit tests.
func (HandlerFuncs) StringIndent ¶
func (handlers HandlerFuncs) StringIndent(indent string) string
type Router ¶
type Router struct { RouterGroup // contains filtered or unexported fields }
Example ¶
router := New() router.Get("/", func(c *Context) { fmt.Println("root") }) users := router.Group("/users") users.Get("/", func(c *Context) { fmt.Println("list users") }) users.Get(`/(\d+)`, func(c *Context) { fmt.Printf("show user: %s\n", c.Param(0)) }) users.Post(`/`, func(c *Context) { fmt.Println("create a user") }) users.Post(`/postx`, func(c *Context) { }) users = users.Group(`/(\d+)`) users.Put(`/`, func(c *Context) { fmt.Printf("fully update user: %s\n", c.Param(0)) }) users.Put(`/putx`, func(c *Context) { }) users.Patch(`/`, func(c *Context) { fmt.Printf("partially update user: %s\n", c.Param(0)) }) users.Patch(`/patchx`, func(c *Context) { }) users.Delete(`/`, func(c *Context) { fmt.Printf("delete user: %s\n", c.Param(0)) }) users.Delete(`/deletex`, func(c *Context) { }) request, err := http.NewRequest("GET", "http://localhost/", nil) if err != nil { log.Panic(err) } for _, route := range [][2]string{ {"GET", "/"}, {"GET", "/users"}, {"POST", "/users"}, {"GET", "/users/101/"}, // with a trailing slash {"PUT", "/users/101"}, {"PATCH", "/users/101"}, {"DELETE", "/users/101"}, } { request.Method = route[0] request.URL.Path = route[1] router.ServeHTTP(nil, request) }
Output: root list users create a user show user: 101 fully update user: 101 partially update user: 101 delete user: 101
func (*Router) BeforeLookup ¶
func (r *Router) BeforeLookup(fun func(ctx *ContextBeforeLookup))
BeforeLookup regiter a function to be run before every route Lookup.
func (*Router) NotFound ¶
Example ¶
router := New() router.Use(func(c *Context) { fmt.Println("middleware") c.Next() }) router.Get("/", func(c *Context) { fmt.Println("root") }) request, err := http.NewRequest("GET", "http://localhost/404", nil) if err != nil { log.Panic(err) } rw := httptest.NewRecorder() router.ServeHTTP(rw, request) response := rw.Result() if body, err := ioutil.ReadAll(response.Body); err != nil { fmt.Println(err) } else { fmt.Println(response.StatusCode, string(body)) } router.NotFound(func(c *Context) { fmt.Println("404 not found") }) router.ServeHTTP(nil, request) request.URL.Path = "/" router.ServeHTTP(nil, request)
Output: middleware 404 {"code":"404","message":"Not Found."} middleware 404 not found middleware root
func (*Router) String ¶
Example ¶
router := New() router.Use(func(c *Context) { c.Next() }) router.Get("/", func(c *Context) { fmt.Println("root") }) users := router.Group("/users") users.Get("/", func(c *Context) { fmt.Println("list users") }) users.Get(`/(\d+)`, func(c *Context) { fmt.Printf("show user: %s\n", c.Param(0)) }) users.Post(`/`, func(c *Context) { fmt.Println("create a user") }) users.Post(`/postx`, func(c *Context) { }) fmt.Println(router)
Output: { handlers: [ gitee.com/go-better/dev/net/goa.ExampleRouter_String.func1 ] routes: { GET: { static: /, data: [ gitee.com/go-better/dev/net/goa.ExampleRouter_String.func1 gitee.com/go-better/dev/net/goa.ExampleRouter_String.func2 ], children: [ { static: users, data: [ gitee.com/go-better/dev/net/goa.ExampleRouter_String.func1 gitee.com/go-better/dev/net/goa.ExampleRouter_String.func3 ], children: [ { dynamic: ^/([0-9]+), data: [ gitee.com/go-better/dev/net/goa.ExampleRouter_String.func1 gitee.com/go-better/dev/net/goa.ExampleRouter_String.func4 ] } ] } ] } POST: { static: /users, data: [ gitee.com/go-better/dev/net/goa.ExampleRouter_String.func1 gitee.com/go-better/dev/net/goa.ExampleRouter_String.func5 ], children: [ { static: /postx, data: [ gitee.com/go-better/dev/net/goa.ExampleRouter_String.func1 gitee.com/go-better/dev/net/goa.ExampleRouter_String.func6 ] } ] } } notFound: gitee.com/go-better/dev/net/goa.defaultNotFound }
func (*Router) Use ¶
Example ¶
router := New() router.Use(func(c *Context) { fmt.Println("middleware 1 pre") c.Next() fmt.Println("middleware 1 post") }) router.Use(func(c *Context) { fmt.Println("middleware 2 pre") c.Next() fmt.Println("middleware 2 post") }) router.Get("/", func(c *Context) { fmt.Println("root") }) request, err := http.NewRequest("GET", "http://localhost/", nil) if err != nil { log.Panic(err) } router.ServeHTTP(nil, request)
Output: middleware 1 pre middleware 2 pre root middleware 2 post middleware 1 post
type RouterGroup ¶
type RouterGroup struct {
// contains filtered or unexported fields
}
Example ¶
g := &RouterGroup{basePath: "/users", routes: make(map[string]*regexptree.Node)} g.Use(func(*Context) {}) g.Get("/nil", nil) g.Get("/", func(*Context) {}) fmt.Println(g) fmt.Println(g.Lookup("HEAD", "/users")) fmt.Println(g.Lookup("POST", "/users"))
Output: { basePath: /users handlers: [ gitee.com/go-better/dev/net/goa.ExampleRouterGroup.func1 ] routes: { GET: { static: /users, data: [ gitee.com/go-better/dev/net/goa.ExampleRouterGroup.func1 gitee.com/go-better/dev/net/goa.ExampleRouterGroup.func2 ] } } } [ gitee.com/go-better/dev/net/goa.ExampleRouterGroup.func1 gitee.com/go-better/dev/net/goa.ExampleRouterGroup.func2 ] [] [ ] []
Example (ConcatPath_basic) ¶
fmt.Println(RouterGroup{}.concatPath("/")) fmt.Println(RouterGroup{}.concatPath("/users/")) fmt.Println(RouterGroup{basePath: "/admin"}.concatPath(`/users/(\d+)`))
Output: / /users /admin/users/(\d+)
Example (ConcatPath_error1) ¶
defer func() { fmt.Println(recover()) }() fmt.Println(RouterGroup{}.concatPath(""))
Output: router path must not be empty.
Example (ConcatPath_error2) ¶
defer func() { fmt.Println(recover()) }() fmt.Println(RouterGroup{}.concatPath("abc"))
Output: router path must begin with "/".
func (*RouterGroup) Add ¶
func (g *RouterGroup) Add(method, path string, handler interface{}, args ...interface{}) *RouterGroup
Example (Error1) ¶
defer func() { fmt.Println(recover()) }() g := &RouterGroup{routes: make(map[string]*regexptree.Node)} g.Add("GET", "/(", func(*Context) {})
Output: error parsing regexp: missing closing ): `/(`
Example (Error2) ¶
defer func() { fmt.Println(recover()) }() g := &RouterGroup{routes: make(map[string]*regexptree.Node)} g.Add("GET", "/", func(*Context) {}) g.Add("GET", "/", func(*Context) {})
Output: path already exists
func (*RouterGroup) Child ¶
func (g *RouterGroup) Child(path string, descs ...string) *RouterGroup
Child returns a new RouterGroup with inline docs. That means, its docs is generated in its parent's REAMED file.
func (*RouterGroup) Delete ¶
func (g *RouterGroup) Delete(path string, handler interface{}, args ...interface{}) *RouterGroup
func (*RouterGroup) DocDir ¶
func (g *RouterGroup) DocDir(dir string) *RouterGroup
func (*RouterGroup) Get ¶
func (g *RouterGroup) Get(path string, handler interface{}, args ...interface{}) *RouterGroup
func (*RouterGroup) GetPost ¶
func (g *RouterGroup) GetPost(path string, handler interface{}, args ...interface{}) *RouterGroup
func (*RouterGroup) Group ¶
func (g *RouterGroup) Group(path string, descs ...string) *RouterGroup
Group returns a new RouterGroup with separate docs. That means, its docs is generated in its own README file unless path is "", ".", or "/".
func (*RouterGroup) Lookup ¶
func (g *RouterGroup) Lookup(method, path string) (HandlerFuncs, []string)
func (*RouterGroup) Patch ¶
func (g *RouterGroup) Patch(path string, handler interface{}, args ...interface{}) *RouterGroup
func (*RouterGroup) Post ¶
func (g *RouterGroup) Post(path string, handler interface{}, args ...interface{}) *RouterGroup
func (*RouterGroup) Put ¶
func (g *RouterGroup) Put(path string, handler interface{}, args ...interface{}) *RouterGroup
func (*RouterGroup) RoutesString ¶
func (g *RouterGroup) RoutesString() string
func (*RouterGroup) String ¶
func (g *RouterGroup) String() string
func (*RouterGroup) Use ¶
func (g *RouterGroup) Use(handlers ...func(*Context)) *RouterGroup
Use adds middlewares to the group, which will be executed for all routes in this group.
func (*RouterGroup) Watch ¶
func (g *RouterGroup) Watch( watchers ...func(method, fullPath string, args []interface{}) func(*Context), ) *RouterGroup
Watch watchs every route in the group, and optionally return a middleware to be executed only for this route.
Source Files ¶
Click to show internal directories.
Click to hide internal directories.