diff --git a/README.md b/README.md index 6b72c98..a9d7c70 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,15 @@ ![image](https://raw.githubusercontent.com/ysugimoto/grpc-graphql-gateway/master/misc/grpc-graphql-gateway.png) +## Change Log + +### v0.9.1 + +#### Changed middleware fucntion type + +On MiddlewareFunc, you need to return `context.Context` as first return value. this is because we need to make custom metadata to gRPC on middleware process. +If you are already using your onw middleware, plase change its interface. see https://github.com/ysugimoto/grpc-graphql-gateway/pull/10 + ## Motivation On API development, frequently we choose some IDL, in order to manage API definitions from a file. diff --git a/runtime/middlewares.go b/runtime/middlewares.go index cfd1822..3096083 100644 --- a/runtime/middlewares.go +++ b/runtime/middlewares.go @@ -7,11 +7,11 @@ import ( // Cors is middelware function to provide CORS headers to response headers func Cors() MiddlewareFunc { - return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { + return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, error) { w.Header().Set("Access-Control-Allow-Origin", r.URL.Host) w.Header().Set("Access-Control-Allow-Credentials", "true") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS") w.Header().Set("Access-Control-Max-Age", "1728000") - return nil + return ctx, nil } } diff --git a/runtime/mux.go b/runtime/mux.go index 68466d1..ead9c15 100644 --- a/runtime/mux.go +++ b/runtime/mux.go @@ -14,7 +14,7 @@ import ( type ( // MiddlewareFunc type definition - MiddlewareFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request) error + MiddlewareFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, error) // Custom error handler which is called on graphql result has an error GraphqlErrorHandler func(errs gqlerrors.FormattedErrors) @@ -92,9 +92,10 @@ func (s *ServeMux) Use(ms ...MiddlewareFunc) *ServeMux { // ServeHTTP implements http.Handler func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - for _, m := range s.middlewares { - if err := m(ctx, w, r); err != nil { + var err error + ctx, err = m(ctx, w, r) + if err != nil { http.Error(w, "middleware error occurred: "+err.Error(), http.StatusInternalServerError) return }