serverutil

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

TODO: Rename or/and break this package.

Index

Constants

View Source
const (
	// GRPC Metadata prefix that is added to allowed headers specified with
	// WithIncomingHeaders.
	MetadataHeaderPrefix = "pf-header-"

	// GRPC Metadata prefix that is added to metadata keys that are extracted from
	// the HTTP request. These keys will only be present for Gateway requests.
	MetadataHTTPPrefix = "pf-http-"
)

Variables

This section is empty.

Functions

func AddressFromContext

func AddressFromContext(ctx context.Context) string

AddressFromContext returns the server's external address. This is the what links should reference, and likely points at a CDN or load balancer.

func CookiesFromIncomingContext

func CookiesFromIncomingContext(ctx context.Context) map[string]*http.Cookie

CookiesFromIncomingContext reads a standard HTTP cookie header from the GRPC metadata and parses the contents.

func HTTPHeader

func HTTPHeader(ctx context.Context, header string) string

HTTPHeader returns the value of a "permanent HTTP header" or a header that was added to the allow-list by a HeaderMatcher.

For permanent headers, see https://github.com/grpc-ecosystem/grpc-gateway/blob/main/runtime/context.go#L328

This will only ever return a value for requests coming via the GRPC Gateway.

func HTTPMethod

func HTTPMethod(ctx context.Context) string

HTTPMethod returns the HTTP method of the request that was made to the Gateway. This will only ever return a value for requests coming via the GRPC Gateway.

func HeaderMatcher

func HeaderMatcher(headers []string) func(string) (string, bool)

HeaderMatcher appends the given headers to the allow-list for incoming requests. This is used to allow certain headers to be passed through the Gateway and into the GRPC server.

See: runtime.WithIncomingHeaderMatcher.

func HttpMetadataAnnotator

func HttpMetadataAnnotator(_ context.Context, r *http.Request) metadata.MD

HttpMetadataAnnotator is a gateway option that maps certain HTTP request fields to incoming GRPC metadata.

func MethodOption

func MethodOption(info *grpc.UnaryServerInfo, ext protoreflect.ExtensionType) (any, bool)

MethodOption queries the value of a proto option for a GRPC method.

TODO: Consider creating an interceptor which injects the MethodDescriptor into the context. Then use methods which query options from the context, such that this can be used from both interceptors and handlers.

Example:

ok, value, err := MethodOption(info, SomeProto.E_Option)

func ParseCookies

func ParseCookies(headers ...string) map[string]*http.Cookie

ParseCookies takes a cookie header string and returns a map of cookies.

func SendCookie

func SendCookie(ctx context.Context, cookie *http.Cookie) error

SendCookie adds an http-set-cookie header for the provided cookie to the outgoing GRPC metadata.

func SendHeader

func SendHeader(ctx context.Context, key, value string) error

SendHeader adds an http header to the outgoing GRPC metadata for forwarding.

func SendStatusCode

func SendStatusCode(ctx context.Context, code int) error

SendStatusCode adds an http status code header to the outgoing GRPC metadata.

The GRPC Gateway will send this as the actual status code via the `statusCodeForwarder` function.

func WithAddress

func WithAddress(ctx context.Context, address string) context.Context

WithAddress adds the server's external address to the context.

Types

type FieldOptionValue

type FieldOptionValue struct {
	FieldName   string
	FieldValue  any
	OptionValue any
}

func FieldOption

func FieldOption(msg proto.Message, ext protoreflect.ExtensionType) ([]*FieldOptionValue, bool)

FieldOption queries a request proto and returns all fields which have the option set.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL