gateway

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2025 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package gateway provides multi-protocol support for gRPC and Connect RPC.

Package gateway provides HTTP/2 transport with keepalive support.

Package gateway provides keepalive support for gRPC according to the specification.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigureServerWithKeepalive

func ConfigureServerWithKeepalive(server *http.Server, keepalive *KeepaliveParameters)

ConfigureServer configures an HTTP server with keepalive parameters.

func ListenAndServeHTTP2

func ListenAndServeHTTP2(addr string, handler http.Handler, opts Options) error

ListenAndServeHTTP2 starts an HTTP/2 server with keepalive support.

func MarshalOpenAPI

func MarshalOpenAPI(spec *OpenAPISpec) ([]byte, error)

MarshalOpenAPI marshals the OpenAPI spec to JSON.

func NewGRPCWebInterceptor added in v0.3.0

func NewGRPCWebInterceptor(handler http.Handler, timeout time.Duration) http.Handler

NewGRPCWebInterceptor creates a new gRPC-Web interceptor

func NewHTTP2Server

func NewHTTP2Server(addr string, handler http.Handler, opts Options) *http.Server

NewHTTP2Server creates an HTTP server configured for HTTP/2 with keepalive.

Types

type CORSConfig

type CORSConfig struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	AllowCredentials bool
	MaxAge           int
}

CORSConfig configures CORS settings.

func DefaultCORSConfig

func DefaultCORSConfig() *CORSConfig

DefaultCORSConfig returns a permissive CORS configuration for development.

type Gateway

type Gateway struct {
	// contains filtered or unexported fields
}

Gateway wraps HTTP handlers for multi-protocol support.

func New

func New(services []*Service, opts Options) (*Gateway, error)

New creates a new gateway.

func (*Gateway) CreateReflectionHandlers

func (g *Gateway) CreateReflectionHandlers() (map[string]http.Handler, error)

CreateReflectionHandlers creates the reflection handlers for the gateway.

func (*Gateway) ExportProtos

func (g *Gateway) ExportProtos() (map[string]string, error)

ExportProtos exports all proto files from the gateway. This is a programmatic API for exporting proto files.

func (*Gateway) ExportProtosToZip

func (g *Gateway) ExportProtosToZip() ([]byte, error)

ExportProtosToZip exports all proto files as a ZIP archive. This is a programmatic API for exporting proto files.

func (*Gateway) ServeHTTP

func (g *Gateway) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type HTTP2Transport

type HTTP2Transport struct {
	// contains filtered or unexported fields
}

HTTP2Transport wraps an HTTP/2 server with keepalive support.

func NewHTTP2Transport

func NewHTTP2Transport(opts Options) *HTTP2Transport

NewHTTP2Transport creates a new HTTP/2 transport with keepalive support.

func (*HTTP2Transport) WrapHandler

func (t *HTTP2Transport) WrapHandler(handler http.Handler) http.Handler

WrapHandler wraps an HTTP handler with HTTP/2 and keepalive support.

type KeepaliveEnforcementPolicy

type KeepaliveEnforcementPolicy struct {
	// Minimum time between receiving successive pings without data/header frames.
	// If pings are received more frequently, they are considered bad pings.
	// Default: 5 minutes
	MinTime time.Duration

	// If true, server allows keepalive pings even when there are no active streams.
	// Default: false
	PermitWithoutStream bool

	// Maximum number of bad pings before closing the connection.
	// 0 means the server will tolerate any number of bad pings.
	// Default: 2
	MaxPingStrikes int
}

KeepaliveEnforcementPolicy configures server-side keepalive enforcement.

func DefaultKeepaliveEnforcementPolicy

func DefaultKeepaliveEnforcementPolicy() KeepaliveEnforcementPolicy

DefaultKeepaliveEnforcementPolicy returns default server-side enforcement policy.

type KeepaliveParameters

type KeepaliveParameters struct {
	// Time after which a keepalive ping is sent on the transport.
	// Default: 2 hours (7200000ms)
	Time time.Duration

	// Timeout for keepalive ping acknowledgement.
	// If no acknowledgement is received, the connection is closed.
	// Default: 20 seconds (20000ms)
	Timeout time.Duration

	// If true, keepalive pings are sent even without active calls.
	// Default: false
	PermitWithoutStream bool

	// Maximum number of pings that can be sent when there is no data/header frame to be sent.
	// Default: 2
	MaxPingsWithoutData int
}

KeepaliveParameters configures gRPC keepalive according to the specification. These settings control HTTP/2 PING frames for connection health checking.

func AggressiveKeepaliveParams

func AggressiveKeepaliveParams() KeepaliveParameters

AggressiveKeepaliveParams returns more aggressive keepalive parameters for environments with proxies that kill idle connections.

func DefaultKeepaliveParams

func DefaultKeepaliveParams() KeepaliveParameters

DefaultKeepaliveParams returns default client-side keepalive parameters.

type OpenAPIComponents

type OpenAPIComponents struct {
	Schemas map[string]any `json:"schemas"`
}

OpenAPIComponents holds reusable components.

type OpenAPIInfo

type OpenAPIInfo struct {
	Title       string `json:"title"`
	Description string `json:"description,omitempty"`
	Version     string `json:"version"`
}

OpenAPIInfo represents API information.

type OpenAPIServer

type OpenAPIServer struct {
	URL         string `json:"url"`
	Description string `json:"description,omitempty"`
}

OpenAPIServer represents a server.

type OpenAPISpec

type OpenAPISpec struct {
	OpenAPI    string            `json:"openapi"`
	Info       OpenAPIInfo       `json:"info"`
	Servers    []OpenAPIServer   `json:"servers,omitempty"`
	Paths      map[string]any    `json:"paths"`
	Components OpenAPIComponents `json:"components"`
}

OpenAPISpec represents an OpenAPI 3.0 specification.

func GenerateOpenAPI

func GenerateOpenAPI(fdset *descriptorpb.FileDescriptorSet, info OpenAPIInfo) (*OpenAPISpec, error)

GenerateOpenAPI generates an OpenAPI spec from a FileDescriptorSet.

type Options

type Options struct {
	// EnableReflection enables gRPC reflection
	EnableReflection bool
	// EnableOpenAPI enables OpenAPI endpoint
	EnableOpenAPI bool
	// OpenAPIPath is the path to serve OpenAPI spec
	OpenAPIPath string
	// CORSConfig configures CORS
	CORSConfig *CORSConfig
	// KeepaliveParams configures client-side keepalive
	KeepaliveParams *KeepaliveParameters
	// KeepaliveEnforcementPolicy configures server-side keepalive enforcement
	KeepaliveEnforcementPolicy *KeepaliveEnforcementPolicy
	// EnabledProtocols specifies which protocols are enabled ("connect", "grpc", "grpcweb")
	EnabledProtocols map[string]bool
}

Options configures the gateway.

type Service

type Service struct {
	Name        string
	Package     string
	Handlers    map[string]http.Handler
	Descriptors *descriptorpb.FileDescriptorSet
}

Service represents a service with its handlers.

type ServiceBuilder

type ServiceBuilder struct {
	// contains filtered or unexported fields
}

ServiceBuilder helps build services.

func NewServiceBuilder

func NewServiceBuilder(name, packageName string) *ServiceBuilder

NewServiceBuilder creates a new service builder.

func (*ServiceBuilder) AddHandler

func (sb *ServiceBuilder) AddHandler(path string, handler http.Handler) *ServiceBuilder

AddHandler adds a handler to the service.

func (*ServiceBuilder) Build

func (sb *ServiceBuilder) Build() (*Service, error)

Build creates the service.

Jump to

Keyboard shortcuts

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