http: fix request context (#745)

* fix request context
* fix recovery logger
pull/746/head
Tony Chen 4 years ago committed by GitHub
parent 946e9ca814
commit 330d878aa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      middleware/recovery/recovery.go
  2. 55
      transport/http/route.go
  3. 7
      transport/http/server.go
  4. 21
      transport/http/server_test.go

@ -45,7 +45,7 @@ func Recovery(opts ...Option) middleware.Middleware {
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)
} }
log := log.NewHelper("middleware/recovery", log.DefaultLogger) log := log.NewHelper("middleware/recovery", options.logger)
return func(handler middleware.Handler) middleware.Handler { return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) { return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
defer func() { defer func() {

@ -1,55 +0,0 @@
package http
import (
"net/http"
"github.com/gorilla/mux"
)
// RouteGroup adds a matcher for the URL path and method. This matches if the given
// template is a prefix of the full URL path. See route.Path() for details on
// the tpl argument.
type RouteGroup struct {
prefix string
router *mux.Router
}
// ANY maps an HTTP Any request to the path and the specified handler.
func (r *RouteGroup) ANY(path string, handler http.HandlerFunc) {
r.router.PathPrefix(r.prefix).Path(path).HandlerFunc(handler)
}
// GET maps an HTTP Get request to the path and the specified handler.
func (r *RouteGroup) GET(path string, handler http.HandlerFunc) {
r.router.PathPrefix(r.prefix).Path(path).HandlerFunc(handler).Methods("GET")
}
// HEAD maps an HTTP Head request to the path and the specified handler.
func (r *RouteGroup) HEAD(path string, handler http.HandlerFunc) {
r.router.PathPrefix(r.prefix).Path(path).HandlerFunc(handler).Methods("HEAD")
}
// POST maps an HTTP Post request to the path and the specified handler.
func (r *RouteGroup) POST(path string, handler http.HandlerFunc) {
r.router.PathPrefix(r.prefix).Path(path).HandlerFunc(handler).Methods("POST")
}
// PUT maps an HTTP Put request to the path and the specified handler.
func (r *RouteGroup) PUT(path string, handler http.HandlerFunc) {
r.router.PathPrefix(r.prefix).Path(path).HandlerFunc(handler).Methods("PUT")
}
// DELETE maps an HTTP Delete request to the path and the specified handler.
func (r *RouteGroup) DELETE(path string, handler http.HandlerFunc) {
r.router.PathPrefix(r.prefix).Path(path).HandlerFunc(handler).Methods("DELETE")
}
// PATCH maps an HTTP Patch request to the path and the specified handler.
func (r *RouteGroup) PATCH(path string, handler http.HandlerFunc) {
r.router.PathPrefix(r.prefix).Path(path).HandlerFunc(handler).Methods("PATCH")
}
// OPTIONS maps an HTTP Options request to the path and the specified handler.
func (r *RouteGroup) OPTIONS(path string, handler http.HandlerFunc) {
r.router.PathPrefix(r.prefix).Path(path).HandlerFunc(handler).Methods("OPTIONS")
}

@ -77,11 +77,6 @@ func NewServer(opts ...ServerOption) *Server {
return srv return srv
} }
// RouteGroup returns a new route group for the URL path prefix.
func (s *Server) RouteGroup(prefix string) *RouteGroup {
return &RouteGroup{prefix: prefix, router: s.router}
}
// Handle registers a new route with a matcher for the URL path. // Handle registers a new route with a matcher for the URL path.
func (s *Server) Handle(path string, h http.Handler) { func (s *Server) Handle(path string, h http.Handler) {
s.router.Handle(path, h) s.router.Handle(path, h)
@ -103,7 +98,7 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
defer cancel() defer cancel()
ctx = transport.NewContext(ctx, transport.Transport{Kind: "HTTP"}) ctx = transport.NewContext(ctx, transport.Transport{Kind: "HTTP"})
ctx = NewServerContext(ctx, ServerInfo{Request: req, Response: res}) ctx = NewServerContext(ctx, ServerInfo{Request: req, Response: res})
s.router.ServeHTTP(res, req) s.router.ServeHTTP(res, req.WithContext(ctx))
} }
// Endpoint return a real address to registry endpoint. // Endpoint return a real address to registry endpoint.

@ -21,16 +21,7 @@ func TestServer(t *testing.T) {
json.NewEncoder(w).Encode(data) json.NewEncoder(w).Encode(data)
} }
srv := NewServer() srv := NewServer()
group := srv.RouteGroup("/test") srv.HandleFunc("/index", fn)
{
group.GET("/", fn)
group.HEAD("/index", fn)
group.OPTIONS("/home", fn)
group.PUT("/products/{id}", fn)
group.POST("/products/{id}", fn)
group.PATCH("/products/{id}", fn)
group.DELETE("/products/{id}", fn)
}
time.AfterFunc(time.Second, func() { time.AfterFunc(time.Second, func() {
defer srv.Stop() defer srv.Stop()
@ -47,11 +38,11 @@ func testClient(t *testing.T, srv *Server) {
method string method string
path string path string
}{ }{
{"GET", "/test/"}, {"GET", "/index"},
{"PUT", "/test/products/1"}, {"PUT", "/index"},
{"POST", "/test/products/2"}, {"POST", "/index"},
{"PATCH", "/test/products/3"}, {"PATCH", "/index"},
{"DELETE", "/test/products/4"}, {"DELETE", "/index"},
} }
client, err := NewClient(context.Background()) client, err := NewClient(context.Background())
if err != nil { if err != nil {

Loading…
Cancel
Save