zitadel/internal/api/grpc/server/middleware/cache_interceptor.go
Livio Amstutz bd8133aedd
fix: do not cache api (incl. grpc) and http errors (#2088)
* fix: add cache-control headers (no-store, no-cache) on grpc (for grpc-web)

* fix: do not cache api response (incl. grpc) and http errors
2021-07-28 13:19:44 +02:00

33 lines
953 B
Go

package middleware
import (
"context"
"net/http"
"time"
"github.com/caos/logging"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
_ "github.com/caos/zitadel/internal/statik"
)
func NoCacheInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
headers := map[string]string{
"cache-control": "no-store",
"expires": time.Now().UTC().Format(http.TimeFormat),
"pragma": "no-cache",
}
header := metadata.New(headers)
for key, value := range headers {
header.Append(runtime.MetadataHeaderPrefix+key, value)
}
err := grpc.SendHeader(ctx, header)
logging.Log("MIDDLE-efh41").OnError(err).WithField("req", info.FullMethod).Warn("cannot send cache-control on grpc response")
resp, err := handler(ctx, req)
return resp, err
}
}