2022-10-27 15:34:06 +00:00
|
|
|
package grpcweb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2023-08-08 12:48:05 +00:00
|
|
|
"github.com/traefik/grpc-web/go/grpcweb"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares"
|
2022-10-27 15:34:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2022-11-21 17:36:05 +00:00
|
|
|
middlewares.GetLogger(ctx, name, typeName).Debug().Msg("Creating middleware")
|
2022-10-27 15:34:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}))
|
|
|
|
}
|