Documentation
¶
Overview ¶
Package ezgrpc provides a simplified setup for gRPC services with a grpc-gateway. It includes utilities for handling cookies, sessions, and standard interceptors.
Package ezgrpc provides a simplified setup for gRPC services with a grpc-gateway. It includes utilities for handling cookies, sessions, and standard interceptors.
Package ezgrpc provides a simplified setup for gRPC services with a grpc-gateway. It includes utilities for handling cookies, sessions, and standard interceptors.
Package ezgrpc provides a simplified setup for gRPC services with a grpc-gateway. It includes utilities for handling cookies, sessions, and standard interceptors.
Index ¶
- Constants
- Variables
- func Close() error
- func DeleteSession(ctx context.Context) error
- func GetSessionData[T any](ctx context.Context) (T, error)
- func GetUser(ctx context.Context) (*user, error)
- func InitSessionStore()
- func InjectGrpcService(f func(grpc.ServiceRegistrar))
- func Invoke[T any, R any](ctx context.Context, addr, service, method string, req T) (R, error)
- func RegisterHandlerFromEndpoint(f RegisterHandlerFromEndpointFunc)
- func Run(ctx context.Context, port int) error
- func RunGrpcGateway(ctx context.Context, port int) error
- func RunGrpcWithHttp(ctx context.Context, port int, httpHandler http.Handler) error
- func SetCookie(ctx context.Context, key, value string, path string, maxAge int) error
- func SetRedirectUrl(ctx context.Context, url string) error
- func SetServeMuxOpts(opts ...runtime.ServeMuxOption)
- func SetSessionData[T any](ctx context.Context, value T) error
- func ToStatus(err error) *status.Status
- type RegisterHandlerFromEndpointFunc
Constants ¶
const MaxBytesReader = 10 << 20
Variables ¶
var ( // Status_EZgrpc_Session_NotFound indicates that a session could not be found. Status_EZgrpc_Session_NotFound = status.New(codes.NotFound, "session not found") // Status_EZgrpc_Session_SaveFailed indicates that a session failed to save. Status_EZgrpc_Session_SaveFailed = status.New(codes.Internal, "session save failed") // Err_SessionNotFound is the underlying error for a missing session. ErrSessionNotFound = errors.New("session not found") // Err_SessionSaveFailed is the underlying error for a session save failure. ErrSessionSaveFailed = errors.New("session save failed") )
Pre-defined gRPC status errors for session-related issues. These provide consistent error responses for clients.
var ( // DefaultHeaderMatcher is a grpc-gateway option that maps incoming HTTP headers to gRPC metadata. // This allows user information from headers to be passed to the gRPC service. DefaultHeaderMatcher = runtime.WithIncomingHeaderMatcher(func(k string) (string, bool) { if v, ok := headerTransMap[strings.ToLower(k)]; ok { return v, true } return runtime.DefaultHeaderMatcher(k) }) // DefaultServeMuxOpts provides default options for the grpc-gateway's ServeMux. DefaultServeMuxOpts = []runtime.ServeMuxOption{ DefaultHeaderMatcher, } )
var ( RedirectResponseOption = runtime.WithForwardResponseOption(redirectResponseOptionHandler) RedirectResponseModifier = runtime.WithForwardResponseRewriter(redirectResponseModifierHandler) )
var ( // SessionCookieForwarder is a grpc-gateway option that modifies the response to handle session data. SessionCookieForwarder = runtime.WithForwardResponseOption(gatewayResponseModifier) // SessionCookieExtractor is a grpc-gateway option that extracts session data from cookies. SessionCookieExtractor = runtime.WithMetadata(extractSessionDataFromCookie) )
var CookieForwarder = runtime.WithForwardResponseOption(setCookieForwarder)
CookieForwarder is a grpc-gateway option that forwards Set-Cookie headers from gRPC metadata to the HTTP response. This allows gRPC handlers to set cookies on the client's browser.
var OutgoingHeaderMatcher = runtime.WithOutgoingHeaderMatcher(outgoingHeaderMatcherHandler)
Functions ¶
func DeleteSession ¶
DeleteSession signals that the session should be deleted by setting a metadata key.
func GetSessionData ¶
GetSessionData retrieves and decodes session data from the incoming gRPC context.
func GetUser ¶
GetUser extracts user information from the incoming gRPC context. It returns a user struct if the required metadata is present.
func InitSessionStore ¶
func InitSessionStore()
InitSessionStore initializes the session store with a secret key and configures session options.
func InjectGrpcService ¶
func InjectGrpcService(f func(grpc.ServiceRegistrar))
InjectGrpcService allows gRPC services to be registered with the central gRPC server.
func RegisterHandlerFromEndpoint ¶
func RegisterHandlerFromEndpoint(f RegisterHandlerFromEndpointFunc)
RegisterHandlerFromEndpoint adds a handler registration function to the list of handlers to be executed.
func RunGrpcGateway ¶
RunGrpcGateway starts the gRPC gateway and HTTP server. It listens on the specified port and serves both gRPC and HTTP traffic.
func RunGrpcWithHttp ¶
func SetCookie ¶
SetCookie sends a cookie to the client by embedding it in the gRPC header metadata. The grpc-gateway, configured with CookieForwarder, will translate this into a standard HTTP Set-Cookie header.
ctx: The context of the gRPC call. key: The name of the cookie. value: The value of the cookie. path: The path for which the cookie is valid. maxAge: The maximum age of the cookie in seconds.
func SetServeMuxOpts ¶
func SetServeMuxOpts(opts ...runtime.ServeMuxOption)
SetServeMuxOpts allows overriding the default ServeMux options.
func SetSessionData ¶
SetSessionData encodes a value and sets it as session data in the gRPC metadata.
func ToStatus ¶
ToStatus converts a custom wrapper error into a gRPC status.Status. This allows for detailed error information to be sent to the client, including a descriptive message and structured details.
err: The custom error with a message. Returns a gRPC status, or nil if the input error is nil.
Types ¶
type RegisterHandlerFromEndpointFunc ¶
type RegisterHandlerFromEndpointFunc func( ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption, ) (err error)
RegisterHandlerFromEndpointFunc is a function type for registering gRPC endpoint handlers.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package interceptor provides gRPC unary server interceptors for common concerns such as logging, metrics, rate limiting, and panic recovery.
|
Package interceptor provides gRPC unary server interceptors for common concerns such as logging, metrics, rate limiting, and panic recovery. |