mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 16:30:01 +08:00
chore: refactor (#5137)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
@@ -13,21 +12,15 @@ import (
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// MetadataHeaderPrefix is the http prefix that represents custom metadata
|
||||
// parameters to or from a gRPC call.
|
||||
const MetadataHeaderPrefix = "Grpc-Metadata-"
|
||||
const (
|
||||
// MetadataHeaderPrefix is the http prefix that represents custom metadata
|
||||
// parameters to or from a gRPC call.
|
||||
MetadataHeaderPrefix = "Grpc-Metadata-"
|
||||
|
||||
// MetadataTrailerPrefix is prepended to gRPC metadata as it is converted to
|
||||
// HTTP headers in a response handled by go-zero gateway
|
||||
const MetadataTrailerPrefix = "Grpc-Trailer-"
|
||||
|
||||
func defaultOutgoingHeaderMatcher(key string) (string, bool) {
|
||||
return fmt.Sprintf("%s%s", MetadataHeaderPrefix, key), true
|
||||
}
|
||||
|
||||
func defaultOutgoingTrailerMatcher(key string) (string, bool) {
|
||||
return fmt.Sprintf("%s%s", MetadataTrailerPrefix, key), true
|
||||
}
|
||||
// MetadataTrailerPrefix is prepended to gRPC metadata as it is converted to
|
||||
// HTTP headers in a response handled by go-zero gateway
|
||||
MetadataTrailerPrefix = "Grpc-Trailer-"
|
||||
)
|
||||
|
||||
type EventHandler struct {
|
||||
Status *status.Status
|
||||
@@ -49,10 +42,9 @@ func (h *EventHandler) OnReceiveHeaders(md metadata.MD) {
|
||||
w, ok := h.writer.(http.ResponseWriter)
|
||||
if ok {
|
||||
for k, vs := range md {
|
||||
if h, ok := defaultOutgoingHeaderMatcher(k); ok {
|
||||
for _, v := range vs {
|
||||
w.Header().Add(h, v)
|
||||
}
|
||||
header := defaultOutgoingHeaderMatcher(k)
|
||||
for _, v := range vs {
|
||||
w.Header().Add(header, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,10 +60,9 @@ func (h *EventHandler) OnReceiveTrailers(status *status.Status, md metadata.MD)
|
||||
w, ok := h.writer.(http.ResponseWriter)
|
||||
if ok {
|
||||
for k, vs := range md {
|
||||
if h, ok := defaultOutgoingTrailerMatcher(k); ok {
|
||||
for _, v := range vs {
|
||||
w.Header().Add(h, v)
|
||||
}
|
||||
header := defaultOutgoingTrailerMatcher(k)
|
||||
for _, v := range vs {
|
||||
w.Header().Add(header, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,3 +75,11 @@ func (h *EventHandler) OnResolveMethod(_ *desc.MethodDescriptor) {
|
||||
|
||||
func (h *EventHandler) OnSendHeaders(_ metadata.MD) {
|
||||
}
|
||||
|
||||
func defaultOutgoingHeaderMatcher(key string) string {
|
||||
return MetadataHeaderPrefix + key
|
||||
}
|
||||
|
||||
func defaultOutgoingTrailerMatcher(key string) string {
|
||||
return MetadataTrailerPrefix + key
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user