Add GrpcWeb middleware

Co-authored-by: Kevin Pollet <pollet.kevin@gmail.com>
This commit is contained in:
Julien Salleyron 2022-10-27 17:34:06 +02:00 committed by GitHub
parent 7a6bfd3336
commit bd3eaf4f5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 251 additions and 2 deletions

View file

@ -0,0 +1,27 @@
package grpcweb
import (
"context"
"net/http"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/traefik/traefik/v2/pkg/config/dynamic"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/traefik/traefik/v2/pkg/middlewares"
)
const typeName = "grpc-web"
// New builds a new gRPC web request converter.
func New(ctx context.Context, next http.Handler, config dynamic.GrpcWeb, name string) http.Handler {
log.FromContext(middlewares.GetLoggerCtx(ctx, name, typeName)).Debug("Creating middleware")
return grpcweb.WrapHandler(next, grpcweb.WithCorsForRegisteredEndpointsOnly(false), grpcweb.WithOriginFunc(func(origin string) bool {
for _, originCfg := range config.AllowOrigins {
if originCfg == "*" || originCfg == origin {
return true
}
}
return false
}))
}