mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-22 03:38:41 +02:00
transport/http: rename route to router (#1082)
* rename route to router
This commit is contained in:
parent
1dab58616b
commit
30334dc47c
@ -16,9 +16,6 @@ import (
|
|||||||
|
|
||||||
var _ Context = (*wrapper)(nil)
|
var _ Context = (*wrapper)(nil)
|
||||||
|
|
||||||
// HandlerFunc defines a function to serve HTTP requests.
|
|
||||||
type HandlerFunc func(Context) error
|
|
||||||
|
|
||||||
// Context is an HTTP Context.
|
// Context is an HTTP Context.
|
||||||
type Context interface {
|
type Context interface {
|
||||||
context.Context
|
context.Context
|
||||||
@ -60,10 +57,10 @@ func (w *responseWriter) Write(data []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type wrapper struct {
|
type wrapper struct {
|
||||||
route *Route
|
router *Router
|
||||||
req *http.Request
|
req *http.Request
|
||||||
res http.ResponseWriter
|
res http.ResponseWriter
|
||||||
w responseWriter
|
w responseWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *wrapper) Header() http.Header {
|
func (c *wrapper) Header() http.Header {
|
||||||
@ -90,9 +87,9 @@ func (c *wrapper) Query() url.Values {
|
|||||||
func (c *wrapper) Request() *http.Request { return c.req }
|
func (c *wrapper) Request() *http.Request { return c.req }
|
||||||
func (c *wrapper) Response() http.ResponseWriter { return c.res }
|
func (c *wrapper) Response() http.ResponseWriter { return c.res }
|
||||||
func (c *wrapper) Middleware(h middleware.Handler) middleware.Handler {
|
func (c *wrapper) Middleware(h middleware.Handler) middleware.Handler {
|
||||||
return middleware.Chain(c.route.srv.ms...)(h)
|
return middleware.Chain(c.router.srv.ms...)(h)
|
||||||
}
|
}
|
||||||
func (c *wrapper) Bind(v interface{}) error { return c.route.srv.dec(c.req, v) }
|
func (c *wrapper) Bind(v interface{}) error { return c.router.srv.dec(c.req, v) }
|
||||||
func (c *wrapper) BindVars(v interface{}) error { return binding.BindQuery(c.Vars(), v) }
|
func (c *wrapper) BindVars(v interface{}) error { return binding.BindQuery(c.Vars(), v) }
|
||||||
func (c *wrapper) BindQuery(v interface{}) error { return binding.BindQuery(c.Query(), v) }
|
func (c *wrapper) BindQuery(v interface{}) error { return binding.BindQuery(c.Query(), v) }
|
||||||
func (c *wrapper) BindForm(v interface{}) error { return binding.BindForm(c.req, v) }
|
func (c *wrapper) BindForm(v interface{}) error { return binding.BindForm(c.req, v) }
|
||||||
@ -100,7 +97,7 @@ func (c *wrapper) Returns(v interface{}, err error) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := c.route.srv.enc(&c.w, c.req, v); err != nil {
|
if err := c.router.srv.enc(&c.w, c.req, v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -108,7 +105,7 @@ func (c *wrapper) Returns(v interface{}, err error) error {
|
|||||||
|
|
||||||
func (c *wrapper) Result(code int, v interface{}) error {
|
func (c *wrapper) Result(code int, v interface{}) error {
|
||||||
c.w.WriteHeader(code)
|
c.w.WriteHeader(code)
|
||||||
if err := c.route.srv.enc(&c.w, c.req, v); err != nil {
|
if err := c.router.srv.enc(&c.w, c.req, v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
16
transport/http/filter.go
Normal file
16
transport/http/filter.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
// FilterFunc is a function which receives an http.Handler and returns another http.Handler.
|
||||||
|
type FilterFunc func(http.Handler) http.Handler
|
||||||
|
|
||||||
|
// FilterChain returns a FilterFunc that specifies the chained handler for HTTP Router.
|
||||||
|
func FilterChain(filters ...FilterFunc) FilterFunc {
|
||||||
|
return func(next http.Handler) http.Handler {
|
||||||
|
for i := len(filters) - 1; i >= 0; i-- {
|
||||||
|
next = filters[i](next)
|
||||||
|
}
|
||||||
|
return next
|
||||||
|
}
|
||||||
|
}
|
@ -6,41 +6,31 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FilterFunc is a function which receives an http.Handler and returns another http.Handler.
|
// HandlerFunc defines a function to serve HTTP requests.
|
||||||
type FilterFunc func(http.Handler) http.Handler
|
type HandlerFunc func(Context) error
|
||||||
|
|
||||||
// FilterChain returns a FilterFunc that specifies the chained handler for HTTP Router.
|
// Router is an HTTP router.
|
||||||
func FilterChain(filters ...FilterFunc) FilterFunc {
|
type Router struct {
|
||||||
return func(next http.Handler) http.Handler {
|
|
||||||
for i := len(filters) - 1; i >= 0; i-- {
|
|
||||||
next = filters[i](next)
|
|
||||||
}
|
|
||||||
return next
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route is an HTTP route.
|
|
||||||
type Route struct {
|
|
||||||
prefix string
|
prefix string
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
srv *Server
|
srv *Server
|
||||||
filters []FilterFunc
|
filters []FilterFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRoute(prefix string, srv *Server, filters ...FilterFunc) *Route {
|
func newRouter(prefix string, srv *Server, filters ...FilterFunc) *Router {
|
||||||
r := &Route{
|
r := &Router{
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
srv: srv,
|
srv: srv,
|
||||||
filters: filters,
|
filters: filters,
|
||||||
}
|
}
|
||||||
r.pool.New = func() interface{} {
|
r.pool.New = func() interface{} {
|
||||||
return &wrapper{route: r}
|
return &wrapper{router: r}
|
||||||
}
|
}
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle registers a new route with a matcher for the URL path and method.
|
// Handle registers a new route with a matcher for the URL path and method.
|
||||||
func (r *Route) Handle(method, relativePath string, h HandlerFunc, filters ...FilterFunc) {
|
func (r *Router) Handle(method, relativePath string, h HandlerFunc, filters ...FilterFunc) {
|
||||||
next := http.Handler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
next := http.Handler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||||
ctx := r.pool.Get().(Context)
|
ctx := r.pool.Get().(Context)
|
||||||
ctx.Reset(res, req)
|
ctx.Reset(res, req)
|
||||||
@ -56,46 +46,46 @@ func (r *Route) Handle(method, relativePath string, h HandlerFunc, filters ...Fi
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GET registers a new GET route for a path with matching handler in the router.
|
// GET registers a new GET route for a path with matching handler in the router.
|
||||||
func (r *Route) GET(path string, h HandlerFunc, m ...FilterFunc) {
|
func (r *Router) GET(path string, h HandlerFunc, m ...FilterFunc) {
|
||||||
r.Handle(http.MethodGet, path, h, m...)
|
r.Handle(http.MethodGet, path, h, m...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HEAD registers a new HEAD route for a path with matching handler in the router.
|
// HEAD registers a new HEAD route for a path with matching handler in the router.
|
||||||
func (r *Route) HEAD(path string, h HandlerFunc, m ...FilterFunc) {
|
func (r *Router) HEAD(path string, h HandlerFunc, m ...FilterFunc) {
|
||||||
r.Handle(http.MethodHead, path, h, m...)
|
r.Handle(http.MethodHead, path, h, m...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST registers a new POST route for a path with matching handler in the router.
|
// POST registers a new POST route for a path with matching handler in the router.
|
||||||
func (r *Route) POST(path string, h HandlerFunc, m ...FilterFunc) {
|
func (r *Router) POST(path string, h HandlerFunc, m ...FilterFunc) {
|
||||||
r.Handle(http.MethodPost, path, h, m...)
|
r.Handle(http.MethodPost, path, h, m...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUT registers a new PUT route for a path with matching handler in the router.
|
// PUT registers a new PUT route for a path with matching handler in the router.
|
||||||
func (r *Route) PUT(path string, h HandlerFunc, m ...FilterFunc) {
|
func (r *Router) PUT(path string, h HandlerFunc, m ...FilterFunc) {
|
||||||
r.Handle(http.MethodPut, path, h, m...)
|
r.Handle(http.MethodPut, path, h, m...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PATCH registers a new PATCH route for a path with matching handler in the router.
|
// PATCH registers a new PATCH route for a path with matching handler in the router.
|
||||||
func (r *Route) PATCH(path string, h HandlerFunc, m ...FilterFunc) {
|
func (r *Router) PATCH(path string, h HandlerFunc, m ...FilterFunc) {
|
||||||
r.Handle(http.MethodPatch, path, h, m...)
|
r.Handle(http.MethodPatch, path, h, m...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DELETE registers a new DELETE route for a path with matching handler in the router.
|
// DELETE registers a new DELETE route for a path with matching handler in the router.
|
||||||
func (r *Route) DELETE(path string, h HandlerFunc, m ...FilterFunc) {
|
func (r *Router) DELETE(path string, h HandlerFunc, m ...FilterFunc) {
|
||||||
r.Handle(http.MethodDelete, path, h, m...)
|
r.Handle(http.MethodDelete, path, h, m...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CONNECT registers a new CONNECT route for a path with matching handler in the router.
|
// CONNECT registers a new CONNECT route for a path with matching handler in the router.
|
||||||
func (r *Route) CONNECT(path string, h HandlerFunc, m ...FilterFunc) {
|
func (r *Router) CONNECT(path string, h HandlerFunc, m ...FilterFunc) {
|
||||||
r.Handle(http.MethodConnect, path, h, m...)
|
r.Handle(http.MethodConnect, path, h, m...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OPTIONS registers a new OPTIONS route for a path with matching handler in the router.
|
// OPTIONS registers a new OPTIONS route for a path with matching handler in the router.
|
||||||
func (r *Route) OPTIONS(path string, h HandlerFunc, m ...FilterFunc) {
|
func (r *Router) OPTIONS(path string, h HandlerFunc, m ...FilterFunc) {
|
||||||
r.Handle(http.MethodOptions, path, h, m...)
|
r.Handle(http.MethodOptions, path, h, m...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TRACE registers a new TRACE route for a path with matching handler in the router.
|
// TRACE registers a new TRACE route for a path with matching handler in the router.
|
||||||
func (r *Route) TRACE(path string, h HandlerFunc, m ...FilterFunc) {
|
func (r *Router) TRACE(path string, h HandlerFunc, m ...FilterFunc) {
|
||||||
r.Handle(http.MethodTrace, path, h, m...)
|
r.Handle(http.MethodTrace, path, h, m...)
|
||||||
}
|
}
|
@ -127,9 +127,9 @@ func NewServer(opts ...ServerOption) *Server {
|
|||||||
return srv
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
// Route registers an HTTP route.
|
// Route registers an HTTP router.
|
||||||
func (s *Server) Route(prefix string, filters ...FilterFunc) *Route {
|
func (s *Server) Route(prefix string, filters ...FilterFunc) *Router {
|
||||||
return newRoute(prefix, s, filters...)
|
return newRouter(prefix, s, filters...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle registers a new route with a matcher for the URL path.
|
// Handle registers a new route with a matcher for the URL path.
|
||||||
|
@ -22,15 +22,9 @@ type testData struct {
|
|||||||
|
|
||||||
func TestServer(t *testing.T) {
|
func TestServer(t *testing.T) {
|
||||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
data := &testData{Path: r.RequestURI}
|
json.NewEncoder(w).Encode(testData{Path: r.RequestURI})
|
||||||
json.NewEncoder(w).Encode(data)
|
|
||||||
|
|
||||||
if r.Context().Value(testKey{}) != "test" {
|
|
||||||
w.WriteHeader(500)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, testKey{}, "test")
|
|
||||||
srv := NewServer()
|
srv := NewServer()
|
||||||
srv.HandleFunc("/index", fn)
|
srv.HandleFunc("/index", fn)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user