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 {
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(ctx context.Context, req interface{}) (reply interface{}, err error) {
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
}
// 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.
func (s *Server) Handle(path string, h http.Handler) {
s.router.Handle(path, h)
@ -103,7 +98,7 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
defer cancel()
ctx = transport.NewContext(ctx, transport.Transport{Kind: "HTTP"})
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.

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

Loading…
Cancel
Save