Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transport): expose server implementation on transport #3279

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

NathanBaulch
Copy link

@NathanBaulch NathanBaulch commented Apr 15, 2024

Description (what this PR does / why we need it):

Expose the server implementation on Transporter, similar to what's available in GRPC interceptors. This gives middlewares the ability to make decisions or delegate functionality to the server/handlers that do the work.

Example middleware that delegates authentication checks to the server handler:

func Server() middleware.Middleware {
	return func(handler middleware.Handler) middleware.Handler {
		return func(ctx context.Context, req any) (any, error) {
			if tr, ok := transport.FromServerContext(ctx); !ok {
				return nil, errors.Unauthorized()
			} else if auth, ok := tr.Server().(Authenticator); !ok {
				return nil, errors.Unauthorized()
			} else if ctx, err := auth.Authenticate(ctx, tr); err != nil {
				return nil, err
			} else {
				return handler(ctx, req)
			}
		}
	}
}

This is an additive change that requires HTTP servers to be regenerated since they need to call http.SetServer alongside the existing http.SetOperation.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Apr 15, 2024
@shenqidebaozi
Copy link
Sponsor Member

This change looks strange, I don't know why it's done this way

@NathanBaulch
Copy link
Author

I'm more than happy to answer any questions. My approach exactly mirrors the way Transport.Operation (aka grpc FullMethod) is intercepted and exposed to middlewares.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants