Documentation ¶
Index ¶
- type Context
- func (c *Context) ClientAddr() string
- func (c *Context) Context() context.Context
- func (c *Context) Data(data interface{}, err error)
- func (c *Context) Flush()
- func (c *Context) Get(key string) interface{}
- func (c *Context) GetError() error
- func (c *Context) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (c *Context) Json(data interface{})
- func (c *Context) Json2(data interface{}, err error)
- func (c *Context) Next()
- func (c *Context) Ok(message string)
- func (c *Context) Param(i int) string
- func (c *Context) Redirect(url string)
- func (c *Context) RequestBody() ([]byte, error)
- func (c *Context) RequestId() string
- func (c *Context) ResponseBody() []byte
- func (c *Context) ResponseBodySize() int64
- func (c *Context) Scheme() string
- func (c *Context) Set(key string, value interface{})
- func (c *Context) SetError(err error)
- func (c *Context) Status() int64
- func (c *Context) StatusJson(status int, data interface{})
- func (c *Context) Url() string
- func (c *Context) 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) 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) Listen(listeners ...func(method, fullPath string, args []interface{}) func(*Context)) *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
Examples ¶
- Context.ClientAddr
- Context.Context
- Context.Data
- Context.Data (Code_message)
- Context.Data (Code_message_data_error)
- Context.Data (Code_message_error)
- Context.Data (Error)
- Context.Flush
- Context.Get
- Context.Hijack
- Context.Json
- Context.Json (Error)
- Context.Json2
- Context.Next
- Context.Ok
- Context.Param
- Context.Redirect
- Context.RequestBody
- Context.ResponseBody
- Context.ResponseBodySize
- Context.Scheme
- Context.Set
- Context.Status
- Context.Url
- Router
- Router.NotFound
- Router.String
- Router.Use
- RouterGroup
- RouterGroup (ConcatPath_basic)
- RouterGroup (ConcatPath_error1)
- RouterGroup (ConcatPath_error2)
- RouterGroup.Add (Error1)
- RouterGroup.Add (Error2)
- RouterGroup.Listen
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct { *http.Request http.ResponseWriter // contains filtered or unexported fields }
func (*Context) ClientAddr ¶
Example ¶
req := httptest.NewRequest("GET", "/path", nil) c := &Context{Request: req} fmt.Println(c.ClientAddr()) req.Header.Set("X-Real-IP", "192.0.2.2") fmt.Println(c.ClientAddr()) req.Header.Set("X-Forwarded-For", "192.0.2.3") fmt.Println(c.ClientAddr())
Output: 192.0.2.1 192.0.2.2 192.0.2.3
func (*Context) Context ¶
Example ¶
c := &Context{Request: httptest.NewRequest("GET", "/", nil)} fmt.Println(c.Context()) ctx := context.WithValue(context.Background(), "custom", 1) c.Set("context", ctx) fmt.Println(c.Context() == ctx)
Output: context.Background true
func (*Context) Data ¶
Example ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} c.Data([]string{"data1", "data2"}, nil) res := recorder.Result() fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err)
Output: 200 map[Content-Type:[application/json; charset=utf-8]] {"code":"ok","message":"success","data":["data1","data2"]} <nil>
Example (Code_message) ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} c.Data(nil, errs.New("the-code", "the message")) res := recorder.Result() fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err) fmt.Println(c.GetError())
Output: 200 map[Content-Type:[application/json; charset=utf-8]] {"code":"the-code","message":"the message"} <nil> <nil>
Example (Code_message_data_error) ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} theErr := errs.New("the-code", "the message") theErr.SetError(errors.New("some error")) theErr.SetData([]string{"data1", "data2"}) c.Data(nil, theErr) res := recorder.Result() fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err) fmt.Println(c.GetError())
Output: 200 map[Content-Type:[application/json; charset=utf-8]] {"code":"the-code","message":"the message","data":["data1","data2"]} <nil> some error
Example (Code_message_error) ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} theErr := errs.New("the-code", "the message") theErr.SetError(errors.New("some error")) c.Data(nil, theErr) res := recorder.Result() fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err) fmt.Println(c.GetError())
Output: 200 map[Content-Type:[application/json; charset=utf-8]] {"code":"the-code","message":"the message"} <nil> some error
Example (Error) ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} c.Data(nil, errors.New("some error")) res := recorder.Result() fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err) fmt.Println(c.GetError())
Output: 500 map[Content-Type:[application/json; charset=utf-8]] {"code":"server-err","message":"Server Error."} <nil> some error
func (*Context) Flush ¶
func (c *Context) Flush()
Example ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} c.Flush() ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c := &Context{ResponseWriter: w} c.Flush() })) defer ts.Close() if _, err := http.Get(ts.URL); err != nil { log.Fatal(err) }
Output:
func (*Context) Get ¶
Example ¶
c := &Context{} fmt.Println(c.Get("a")) c.Set("a", "韩梅梅") fmt.Println(c.Get("a"))
Output: <nil> 韩梅梅
func (*Context) Hijack ¶
Example ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} fmt.Println(c.Hijack()) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c := &Context{ResponseWriter: w} conn, buf, err := c.Hijack() if err != nil { log.Fatal(err) } defer conn.Close() buf.Write([]byte("HTTP/1.1 200 OK\r\nHeader-Key: Header-Value\r\n\r\nBody")) buf.Flush() })) defer ts.Close() if res, err := http.Get(ts.URL); err != nil { log.Fatal(err) } else { fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err) }
Output: <nil> <nil> the ResponseWriter doesn't support hijacking. 200 map[Header-Key:[Header-Value]] Body <nil>
func (*Context) Json ¶
func (c *Context) Json(data interface{})
Example ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} c.Json(map[string]interface{}{"k": "<value>"}) res := recorder.Result() fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err)
Output: 200 map[Content-Type:[application/json; charset=utf-8]] {"k":"<value>"} <nil>
Example (Error) ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} c.Json(map[string]interface{}{"a": "a", "b": func() {}}) res := recorder.Result() fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err)
Output: 200 map[Content-Type:[application/json; charset=utf-8]] {"code":"json-marshal-error","message":"json marshal error"} <nil>
func (*Context) Json2 ¶
Example ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} c.Json2(map[string]interface{}{"k": "<value>"}, errors.New("the error")) res := recorder.Result() fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err) fmt.Println(c.GetError())
Output: 200 map[Content-Type:[application/json; charset=utf-8]] {"k":"<value>"} <nil> the error
func (*Context) Ok ¶
Example ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} c.Ok("success") res := recorder.Result() fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err)
Output: 200 map[Content-Type:[application/json; charset=utf-8]] {"code":"ok","message":"success"} <nil>
func (*Context) Param ¶
Param returns captured subpatterns. index begin at 0, so pass 0 to get the first captured subpatterns.
Example ¶
c := &Context{params: []string{"123", "sdf"}} fmt.Println(c.Param(0), "-") fmt.Println(c.Param(1), "-") fmt.Println(c.Param(2), "-")
Output: 123 - sdf - -
func (*Context) Redirect ¶
Example ¶
recorder := httptest.NewRecorder() c := &Context{ResponseWriter: recorder} c.Redirect("http://example.com/path") res := recorder.Result() fmt.Println(res.StatusCode) fmt.Println(res.Header) body, err := ioutil.ReadAll(res.Body) fmt.Println(string(body), err) fmt.Println(c.GetError())
Output: 302 map[Location:[http://example.com/path]] <nil> <nil>
func (*Context) RequestBody ¶
Example ¶
buf := []byte("hello world!") c := &Context{Request: &http.Request{Body: ioutil.NopCloser(bytes.NewBuffer(buf))}} body, err := c.RequestBody() if err != nil { fmt.Println(err) } else { fmt.Println(string(body)) } body, err = c.RequestBody() if err != nil { fmt.Println(err) } else { fmt.Println(string(body)) }
Output: hello world! hello world!
func (*Context) ResponseBody ¶
Example ¶
c := &Context{ResponseWriter: httptest.NewRecorder()} fmt.Println("empty" + string(c.ResponseBody())) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c := &Context{ResponseWriter: w} fmt.Println("empty" + string(c.ResponseBody())) c.Write([]byte("1")) fmt.Println(string(c.ResponseBody())) c.ResponseWriter.Write([]byte("23")) fmt.Println(string(c.ResponseBody())) c.Write([]byte("456")) fmt.Println(string(c.ResponseBody())) })) res, err := http.Get(ts.URL) if err != nil { log.Fatal(err) } body, err := ioutil.ReadAll(res.Body) if err != nil { log.Fatal(err) } fmt.Println(string(body))
Output: empty empty 1 1 1456 123456
func (*Context) ResponseBodySize ¶
Example ¶
c := &Context{ResponseWriter: httptest.NewRecorder()} fmt.Println(c.ResponseBodySize()) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c := &Context{ResponseWriter: w} fmt.Println(c.ResponseBodySize()) c.Write([]byte("1")) fmt.Println(c.ResponseBodySize()) c.ResponseWriter.Write([]byte("23")) fmt.Println(c.ResponseBodySize()) c.Write([]byte("456")) fmt.Println(c.ResponseBodySize()) })) _, err := http.Get(ts.URL) if err != nil { log.Fatal(err) }
Output: 0 0 1 3 6
func (*Context) Scheme ¶
Example ¶
req := httptest.NewRequest("GET", "http://example.com/path", nil) c := &Context{Request: req} fmt.Println(c.Scheme()) req.Header.Set("X-Forwarded-Proto", "https") fmt.Println(c.Scheme())
Output: http https
func (*Context) Set ¶
Example ¶
c := &Context{} c.Set("a", "韩梅梅") fmt.Println(c.Get("a"))
Output: 韩梅梅
func (*Context) Status ¶
Example ¶
c := &Context{ResponseWriter: httptest.NewRecorder()} fmt.Println(c.Status()) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c := &Context{ResponseWriter: w} fmt.Println(c.Status()) c.WriteHeader(http.StatusOK) fmt.Println(c.Status()) })) defer ts.Close() res, err := http.Get(ts.URL) if err != nil { log.Fatal(err) } fmt.Println(res.StatusCode)
Output: 0 0 200 200
func (*Context) StatusJson ¶
type HandlerFuncs ¶
type HandlerFuncs []func(*Context)
func (HandlerFuncs) String ¶
func (handlers HandlerFuncs) String() string
to print regex_tree.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 { 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) 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 { 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: [ github.com/lovego/goa.ExampleRouter_String.func1 ] routes: { GET: { static: /, data: [ github.com/lovego/goa.ExampleRouter_String.func1 github.com/lovego/goa.ExampleRouter_String.func2 ], children: [ { static: users, data: [ github.com/lovego/goa.ExampleRouter_String.func1 github.com/lovego/goa.ExampleRouter_String.func3 ], children: [ { dynamic: ^/([0-9]+), data: [ github.com/lovego/goa.ExampleRouter_String.func1 github.com/lovego/goa.ExampleRouter_String.func4 ] } ] } ] } POST: { static: /users, data: [ github.com/lovego/goa.ExampleRouter_String.func1 github.com/lovego/goa.ExampleRouter_String.func5 ], children: [ { static: /postx, data: [ github.com/lovego/goa.ExampleRouter_String.func1 github.com/lovego/goa.ExampleRouter_String.func6 ] } ] } } notFound: github.com/lovego/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 { 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]*regex_tree.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: [ github.com/lovego/goa.ExampleRouterGroup.func1 ] routes: { GET: { static: /users, data: [ github.com/lovego/goa.ExampleRouterGroup.func1 github.com/lovego/goa.ExampleRouterGroup.func2 ] } } } [ github.com/lovego/goa.ExampleRouterGroup.func1 github.com/lovego/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]*regex_tree.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]*regex_tree.Node)} g.Add("GET", "/", func(*Context) {}) g.Add("GET", "/", func(*Context) {})
Output: path already exists
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
func (*RouterGroup) Listen ¶
func (g *RouterGroup) Listen( listeners ...func(method, fullPath string, args []interface{}) func(*Context), ) *RouterGroup
Listen listens every route in the group, and optionally return a middleware to be executed only for this route.
Example ¶
g := &RouterGroup{basePath: "/users", routes: make(map[string]*regex_tree.Node)} g.Listen(func(method, path string, args []interface{}) func(*Context) { fmt.Println(method, path, args) return nil }) g.Get("/", func(*Context) { }, "list") g.Post("/", func(*Context) { }, "create")
Output: GET /users [list] POST /users [create]
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.
Source Files ¶
Click to show internal directories.
Click to hide internal directories.