Documentation
¶
Index ¶
- Constants
- Variables
- func NewClient(target string, opts ...ClientOption) (*grpc.ClientConn, error)
- func NewContext(ctx context.Context, kvs ...any) context.Context
- func SpanID(ctx context.Context) string
- func TraceID(ctx context.Context) string
- type ClientOption
- func WithClientDialOptions(opts ...grpc.DialOption) ClientOption
- func WithClientLogger(logger grpclogging.Logger) ClientOption
- func WithClientRootCertificate(certFile string) ClientOption
- func WithClientRootCertificatePEM(certPEM []byte) ClientOption
- func WithClientServerName(serverName string) ClientOption
- func WithClientStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) ClientOption
- func WithClientTLSConfig(tlsConfig *tls.Config) ClientOption
- func WithClientUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) ClientOption
- type Context
- func (c *Context) AcceptLanguage() string
- func (c *Context) Blob(statusCode int, contentType string, data []byte) error
- func (c *Context) ClientIP() string
- func (c *Context) ContentType() string
- func (c *Context) Depth() int
- func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
- func (c *Context) FormValue(name string) string
- func (c *Context) Get(key any) any
- func (c *Context) Header(name string) string
- func (c *Context) JSON(statusCode int, value any) error
- func (c *Context) JWT() jwt.MapClaims
- func (c *Context) NoContent(statusCode int) error
- func (c *Context) Param(name string) string
- func (c *Context) Queries() url.Values
- func (c *Context) Query(name string) string
- func (c *Context) ReadFormFile(name string, maxFileBytes int64) (string, []byte, error)
- func (c *Context) Request() *http.Request
- func (c *Context) Response() http.ResponseWriter
- func (c *Context) SetHeader(name, value string)
- func (c *Context) SpanID() string
- func (c *Context) String(key any) string
- func (c *Context) Text(statusCode int, value string) error
- func (c *Context) TraceID() string
- func (c *Context) UserAgent() string
- func (c *Context) Value(key any) any
- type ErrorConvertFunc
- type ErrorConverter
- type GRPCRegisterFunc
- type GatewayRegisterFunc
- type HTTPServerOption
- type HandlerFunc
- type Option
- func WithAddress(address string) Option
- func WithCertificate(certFile, keyFile string) Option
- func WithCertificatePEM(certPEM, keyPEM []byte) Option
- func WithErrorConverters(converters ...ErrorConverter) Option
- func WithGRPCRegister(registers ...GRPCRegisterFunc) Option
- func WithGRPCServerOptions(opts ...grpc.ServerOption) Option
- func WithGatewayDialOptions(opts ...grpc.DialOption) Option
- func WithGatewayEndpoint(endpoint string) Option
- func WithGatewayOptions(opts ...runtime.ServeMuxOption) Option
- func WithGatewayRegister(registers ...GatewayRegisterFunc) Option
- func WithGatewayServerName(serverName string) Option
- func WithGracefulTimeout(timeout time.Duration) Option
- func WithHTTPServerOptions(opts ...HTTPServerOption) Option
- func WithI18nBundle(bundle *i18n.Bundle) Option
- func WithI18nFS(messageFS fs.FS) Option
- func WithJWTSecret(secret []byte) Option
- func WithListener(listener net.Listener) Option
- func WithLogger(logger grpclogging.Logger) Option
- func WithName(name string) Option
- func WithReflection() Option
- func WithStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) Option
- func WithTLSConfig(tlsConfig *tls.Config) Option
- func WithUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) Option
- type RespError
- func (e RespError) Error() string
- func (e RespError) GRPCStatus() *status.Status
- func (e RespError) WithData(data map[string]any) RespError
- func (e RespError) WithDetails(details ...proto.Message) RespError
- func (e RespError) WithDomainReason(domain, reason string) RespError
- func (e RespError) WithErrorCode(errorCode protoreflect.Enum) RespError
- func (e RespError) WithHTTPStatus(statusCode int) RespError
- func (e RespError) WithMessage(message string) RespError
- type Server
Constants ¶
const ( TraceIDKey = "trace-id" SpanIDKey = "span-id" JWTKey = "jwt" DepthKey = "x-depth" ClientIPKey = "client-ip" ContentTypeKey = "content-type" UserAgentKey = "user-agent" AcceptLanguageKey = "accept-language" )
Variables ¶
var ( // ErrInternal 表示未向调用方暴露内部实现细节的服务端错误。 ErrInternal = NewError(codes.Internal, "internal server error") // ErrInvalidParam 表示请求参数不合法。 ErrInvalidParam = NewError(codes.InvalidArgument, "invalid parameter") // ErrUnauthenticated 表示调用方尚未通过身份认证。 ErrUnauthenticated = NewError(codes.Unauthenticated, "unauthenticated") // ErrNotFound 表示请求的资源不存在。 ErrNotFound = NewError(codes.NotFound, "resource not found") // ErrPermissionDenied 表示调用方无权执行当前操作。 ErrPermissionDenied = NewError(codes.PermissionDenied, "permission denied") // ErrAlreadyExists 表示待创建的资源已经存在。 ErrAlreadyExists = NewError(codes.AlreadyExists, "resource already exists") // ErrResourceExhausted 表示请求超过服务允许的资源限制。 ErrResourceExhausted = NewError(codes.ResourceExhausted, "resource exhausted") // ErrFailedPrecondition 表示当前系统状态不满足操作前置条件。 ErrFailedPrecondition = NewError(codes.FailedPrecondition, "failed precondition") // ErrAborted 表示操作因并发冲突等原因被中止。 ErrAborted = NewError(codes.Aborted, "operation aborted") ErrUnavailable = NewError(codes.Unavailable, "service unavailable") )
Functions ¶
func NewClient ¶
func NewClient(target string, opts ...ClientOption) (*grpc.ClientConn, error)
NewClient 创建由 lifex 管理的标准 gRPC ClientConn,默认使用明文连接。
func NewContext ¶
NewContext 在保留已有参数的基础上创建新的请求上下文。
Types ¶
type ClientOption ¶
type ClientOption func(*clientConfig) error
ClientOption 配置标准 gRPC ClientConn。
func WithClientDialOptions ¶
func WithClientDialOptions(opts ...grpc.DialOption) ClientOption
func WithClientLogger ¶
func WithClientLogger(logger grpclogging.Logger) ClientOption
func WithClientRootCertificate ¶
func WithClientRootCertificate(certFile string) ClientOption
func WithClientRootCertificatePEM ¶
func WithClientRootCertificatePEM(certPEM []byte) ClientOption
func WithClientServerName ¶
func WithClientServerName(serverName string) ClientOption
func WithClientStreamInterceptors ¶
func WithClientStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) ClientOption
func WithClientTLSConfig ¶
func WithClientTLSConfig(tlsConfig *tls.Config) ClientOption
WithClientTLSConfig 使用自定义 TLS 配置连接 gRPC Server。
func WithClientUnaryInterceptors ¶
func WithClientUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) ClientOption
type Context ¶
Context 提供标准中间件写入的请求级参数,以及额外 HTTP Handler 的请求和响应能力。
func NewHTTPContext ¶
func NewHTTPContext(request *http.Request, response http.ResponseWriter, pathParams map[string]string) *Context
NewHTTPContext 创建包含原始 HTTP 请求、响应和路径参数的 Context,主要用于测试 Handler。
func (*Context) AcceptLanguage ¶
AcceptLanguage 返回请求携带的原始语言偏好。
func (*Context) ContentType ¶
func (*Context) FormFile ¶
func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
FormFile 返回 multipart 表单中的第一个文件。
func (*Context) ReadFormFile ¶
ReadFormFile 在限制文件和请求体大小后读取 multipart 表单中的第一个文件。
func (*Context) Response ¶
func (c *Context) Response() http.ResponseWriter
Response 返回额外 HTTP Handler 的原始响应写入器;非 HTTP Context 返回 nil。
type ErrorConvertFunc ¶
ErrorConvertFunc 将函数适配为 ErrorConverter。
type ErrorConverter ¶
ErrorConverter 将应用依赖返回的错误转换为统一响应错误。
type GRPCRegisterFunc ¶
type GRPCRegisterFunc func(grpc.ServiceRegistrar)
GRPCRegisterFunc 向统一的 gRPC Server 注册业务服务。
type GatewayRegisterFunc ¶
GatewayRegisterFunc 注册由 google.api.http 生成的 Gateway 路由。
type HTTPServerOption ¶
HTTPServerOption 调整底层 HTTP Server 的标准参数,不允许替换 Handler。
type HandlerFunc ¶
HandlerFunc 处理通过 Server.HandlePath 注册的额外 HTTP 请求;返回的错误由 Server 统一转换并写入响应。
type Option ¶
type Option func(*config) error
Option 配置 Server 的初始化参数。
func WithAddress ¶
func WithCertificate ¶
func WithCertificatePEM ¶
WithCertificatePEM 使用内存中的 PEM 证书和私钥启用 TLS。
func WithErrorConverters ¶
func WithErrorConverters(converters ...ErrorConverter) Option
WithErrorConverters 注入应用依赖错误到统一响应错误的转换器。
func WithGRPCRegister ¶
func WithGRPCRegister(registers ...GRPCRegisterFunc) Option
func WithGRPCServerOptions ¶
func WithGRPCServerOptions(opts ...grpc.ServerOption) Option
func WithGatewayDialOptions ¶
func WithGatewayDialOptions(opts ...grpc.DialOption) Option
func WithGatewayEndpoint ¶
func WithGatewayOptions ¶
func WithGatewayOptions(opts ...runtime.ServeMuxOption) Option
func WithGatewayRegister ¶
func WithGatewayRegister(registers ...GatewayRegisterFunc) Option
func WithGatewayServerName ¶
func WithGracefulTimeout ¶
func WithHTTPServerOptions ¶
func WithHTTPServerOptions(opts ...HTTPServerOption) Option
func WithI18nBundle ¶
WithI18nBundle 注入业务翻译目录;未匹配语言时回退到错误码定义的英文文案。
func WithI18nFS ¶
WithI18nFS 从文件系统递归加载 TOML 翻译文件,并使用英文作为默认语言。
func WithListener ¶
func WithLogger ¶
func WithLogger(logger grpclogging.Logger) Option
func WithStreamInterceptors ¶
func WithStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) Option
func WithTLSConfig ¶
func WithUnaryInterceptors ¶
func WithUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) Option
type RespError ¶
type RespError struct {
// contains filtered or unexported fields
}
RespError 描述可同时供 gRPC 和 Gateway 返回的业务错误。
func (RespError) GRPCStatus ¶
GRPCStatus 将响应错误转换为 gRPC Status。
func (RespError) WithDetails ¶
WithDetails 追加结构化错误详情。
func (RespError) WithDomainReason ¶
WithDomainReason 设置由业务定义的错误码和 i18n 信息。
func (RespError) WithErrorCode ¶
func (e RespError) WithErrorCode(errorCode protoreflect.Enum) RespError
WithErrorCode 从枚举选项读取业务错误码、英文默认文案和额外 HTTP 状态码。
func (RespError) WithHTTPStatus ¶
WithHTTPStatus 设置通过 HandlePath 返回错误时使用的 HTTP 状态码。
func (RespError) WithMessage ¶
WithMessage 设置返回给调用方的错误消息。
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 在同一个端口提供原生 gRPC 和 grpc-gateway HTTP 服务。
func (*Server) HandlePath ¶
func (s *Server) HandlePath(method, path string, handler HandlerFunc) error
HandlePath 注册无法通过 google.api.http 表达的额外 HTTP 接口。
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package testserver 提供标准 gRPC 与额外 HTTP 接口的测试环境。
|
Package testserver 提供标准 gRPC 与额外 HTTP 接口的测试环境。 |