Replies: 1 comment
-
I'd put and func AuthMiddleware(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
authHeader := ctx.Value(.....). // get the authentication information from the context
authInfo, err := validate(authHeader) // here is the authentication logic
if err != nil {
return nil, err
}
ctx = context.With(ctx, <someKey>, authInfo)
return next(ctx, request)
}
} Service level middlewares are designed to enrich each method without changing the business logic (for instance to add logging, metrics, ...); to do that you need to know exactly which method is going to be executed |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello.
I'm wondering how you could go about reusing some of the middleware I've already written. In all of the examples I've seen, the middleware has a
next
field, with a next service hardcoded.Since Go 1.18 was released, I'm thinking how I could make this more generic so that "any" service can be accepted instead of just the one. Would I really need to implement an authentication middleware for each of my services separately? I must be missing something. Thanks.
Beta Was this translation helpful? Give feedback.
All reactions