gors

package module
v1.5.8 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const GorsMetadataPrefix = "gors-"

GorsMetadataPrefix is prepended to permanent HTTP header keys when added to the gRPC context.

View Source
const MetadataHeaderPrefix = "Grpc-Metadata-"

MetadataHeaderPrefix is the http prefix that represents custom metadata parameters to or from a gRPC call.

View Source
const MetadataTrailerPrefix = "Grpc-Trailer-"

MetadataTrailerPrefix is prepended to gRPC metadata as it is converted to HTTP headers in a response handled by grpc-gateway

Variables

View Source
var (
	ErrBinding  = Error{StatusCode: 200, Code: 400, Message: "failed to bind request"}.Froze()
	ErrValidate = Error{StatusCode: 200, Code: 400, Message: "failed to validate request"}.Froze()
)
View Source
var (
	SuccessfulStatusCode = http.StatusOK
	SuccessfulCode       = 0
	SuccessfulMessage    = "ok"

	FailedStatusCode = http.StatusInternalServerError
	FailedCode       = 100001
	FailedMessage    = "an internal server error occurred"
)

全局默认错误码,可以自定义覆盖

View Source
var DefaultContextTimeout = 0 * time.Second

DefaultContextTimeout is used for gRPC call context.WithTimeout whenever a Grpc-Timeout inbound header isn't present. If the value is 0 the sent `context` will not have a timeout.

View Source
var DefaultMarshalOptions = protojson.MarshalOptions{}

Functions

func AddGRPCMetadata added in v1.3.0

func AddGRPCMetadata(
	ctx context.Context,
	headerMD, trailerMD metadata.MD,
	headerMatcher func(key string) (string, bool),
)

func AppendRichRoutes added in v1.1.1

func AppendRichRoutes[R gin.IRoutes](iRoutes R, routes ...RichRoute) R

AppendRichRoutes 追加富路由

func AppendRoutes added in v1.1.1

func AppendRoutes[R gin.IRoutes](iRoutes R, routes ...Route) R

AppendRoutes 追加基础路由

func AsciiJSONRender added in v1.3.0

func AsciiJSONRender(ctx context.Context, code int, resp any, _ string)

func BytesBinding added in v1.3.2

func BytesBinding(ctx context.Context, req any, tag string) error

func BytesRender added in v1.3.0

func BytesRender(ctx context.Context, code int, resp any, contentType string)

func CustomBinding added in v1.3.0

func CustomBinding(ctx context.Context, req any, _ string) error

func CustomRender added in v1.3.0

func CustomRender(ctx context.Context, code int, resp any, _ string)

func ErrorRender added in v1.3.0

func ErrorRender(
	ctx context.Context,
	err error,
	handler func(ctx context.Context, err error) error,
	wrapper func(resp any) any,
)

func FormBinding added in v1.3.0

func FormBinding(ctx context.Context, req any, tag string) error

func FormMultipartBinding added in v1.3.0

func FormMultipartBinding(ctx context.Context, req any, tag string) error

func FormPostBinding added in v1.3.0

func FormPostBinding(ctx context.Context, req any, tag string) error

func FromContext

func FromContext(ctx context.Context) *gin.Context

FromContext 从context获取gin.Contenxt

func HTMLRender added in v1.3.0

func HTMLRender(ctx context.Context, code int, resp any, _ string)
func Header(ctx context.Context) http.Header

Header 从context获取header

func HeaderBinding added in v1.3.0

func HeaderBinding(ctx context.Context, req any, tag string) error

func HttpRuleBinding added in v1.5.0

func HttpRuleBinding(binding *bindingPkg.HttpRuleBinding) func(ctx context.Context, req any, _ string) error

func IndentedJSONRender added in v1.3.0

func IndentedJSONRender(ctx context.Context, code int, resp any, _ string)

func JSONBinding added in v1.3.0

func JSONBinding(ctx context.Context, req any, _ string) error

func JSONPJSONRender added in v1.3.0

func JSONPJSONRender(ctx context.Context, code int, resp any, _ string)

func JSONRender added in v1.3.0

func JSONRender(ctx context.Context, code int, resp any, _ string)

func MsgPackBinding added in v1.3.0

func MsgPackBinding(ctx context.Context, req any, _ string) error

func MsgPackRender added in v1.3.0

func MsgPackRender(ctx context.Context, code int, resp any, _ string)

func NewContext

func NewContext(c *gin.Context, rpcMethodName string) context.Context

NewContext 向context注入gin.Context

func NewGRPCContext added in v1.3.0

func NewGRPCContext(
	ctx context.Context,
	headerMatcher func(key string) (string, bool),
	metadataAnnotators []func(ctx context.Context) metadata.MD,
) (context.Context, error)

NewGRPCContext adds context information such as metadata from the request. At a minimum, the RemoteAddr is included in the fashion of "X-Forwarded-For", except that the forwarded destination is not another HTTP service but rather a gRPC service.

func ProtoBufBinding added in v1.3.0

func ProtoBufBinding(ctx context.Context, req any, _ string) error

func ProtoBufRender added in v1.3.0

func ProtoBufRender(ctx context.Context, code int, resp any, _ string)

func ProtoJSONBinding added in v1.3.0

func ProtoJSONBinding(ctx context.Context, req any, _ string) error

func ProtoJSONRender added in v1.3.0

func ProtoJSONRender(mo protojson.MarshalOptions) func(ctx context.Context, code int, resp any, _ string)

func PureJSONRender added in v1.3.0

func PureJSONRender(ctx context.Context, code int, resp any, _ string)

func QueryBinding added in v1.3.0

func QueryBinding(ctx context.Context, req any, tag string) error

func RPCMethod added in v1.3.0

func RPCMethod(ctx context.Context) (string, bool)

RPCMethod returns the method string for the server context. The returned string is in the format of "/package.Service/method".

func ReaderBinding added in v1.3.2

func ReaderBinding(ctx context.Context, req any, _ string) error

func ReaderRender added in v1.3.0

func ReaderRender(ctx context.Context, code int, resp any, contentType string)

func RedirectRender added in v1.3.0

func RedirectRender(ctx context.Context, code int, resp any, _ string)

func RequestBind added in v1.3.0

func RequestBind(ctx context.Context, req any, tag string, bindings ...func(ctx context.Context, req any, tag string) error) error

func ResponseRender added in v1.3.0

func ResponseRender(
	ctx context.Context,
	code int,
	resp any,
	contentType string,
	render func(ctx context.Context, code int, resp any, contentType string),
	wrapper func(resp any) any,
)

func SecureJSONRender added in v1.3.0

func SecureJSONRender(ctx context.Context, code int, resp any, _ string)

func SetHeader added in v1.3.0

func SetHeader(ctx context.Context, header http.Header)

SetHeader 向context设置header

func SetStatusCode added in v1.3.0

func SetStatusCode(ctx context.Context, code int)

SetStatusCode 向context设置status code

func SetTrailer added in v1.3.0

func SetTrailer(ctx context.Context, trailer http.Header)

SetTrailer 向context设置trailer

func StatusCode added in v1.3.0

func StatusCode(ctx context.Context) int

StatusCode 从context获取status code

func StringBinding added in v1.3.2

func StringBinding(ctx context.Context, req any, tag string) error

func StringRender added in v1.3.0

func StringRender(ctx context.Context, code int, resp any, contentType string)

func TOMLBinding added in v1.3.0

func TOMLBinding(ctx context.Context, req any, _ string) error

func TOMLRender added in v1.3.0

func TOMLRender(ctx context.Context, code int, resp any, _ string)

func TextRender added in v1.3.0

func TextRender(ctx context.Context, code int, resp any, _ string)

func Trailer added in v1.3.0

func Trailer(ctx context.Context) http.Header

Trailer 从context获取trailer

func UriBinding added in v1.3.0

func UriBinding(ctx context.Context, req any, tag string) error

func Validate

func Validate(req interface{}) error

func XMLBinding added in v1.3.0

func XMLBinding(ctx context.Context, req any, _ string) error

func XMLRender added in v1.3.0

func XMLRender(ctx context.Context, code int, resp any, _ string)

func YAMLBinding added in v1.3.0

func YAMLBinding(ctx context.Context, req any, _ string) error

func YAMLRender added in v1.3.0

func YAMLRender(ctx context.Context, code int, resp any, _ string)

Types

type Binding added in v1.2.0

type Binding interface {
	Bind(ctx context.Context) error
}

Binding 如果请求参数struct实现Binding接口,框架会用自定义绑定逻辑来绑定请求参数

type Empty added in v1.4.4

type Empty struct{}

type Error added in v1.3.0

type Error struct {
	// StatusCode http status code
	StatusCode int
	// Code business service code
	Code int
	// Message error message
	Message string
	// Cause error
	Cause error
}

Error 包含业务状态的错误.

func ErrorFromMessage added in v1.3.0

func ErrorFromMessage(msg string) (Error, bool)

func FromError added in v1.4.6

func FromError(err error) Error

FromError 解析error为 Error. 解析 Error 或者 status.Status 为 Error, 其他类型的error返回 UnknownError.

func (Error) Error added in v1.3.0

func (e Error) Error() string

func (Error) Format added in v1.4.6

func (e Error) Format(s fmt.State, verb rune)

Format nolint: errcheck // WriteString could no check in pkg.

func (Error) Froze added in v1.4.6

func (e Error) Froze() ErrorAPI

Froze convert Error to ErrorAPI

func (Error) GRPCStatus added in v1.4.6

func (e Error) GRPCStatus() *gstatus.Status

GRPCStatus returns the Status represented by gors.Error.

func (Error) Is added in v1.4.6

func (e Error) Is(err error) bool

Is matches each error in the chain with the target value.

func (Error) Status added in v1.3.0

func (e Error) Status() *Status

func (Error) Unwrap added in v1.4.6

func (e Error) Unwrap() error

Unwrap provides compatibility for Go 1.13 error chains.

func (Error) Wrap added in v1.4.10

func (e Error) Wrap(err error) Error

type ErrorAPI added in v1.4.9

type ErrorAPI interface {
	Error() string
	Wrap(err error) Error
	Unwrap() error
	Status() *Status
	Is(err error) bool
	GRPCStatus() *gstatus.Status
}

type Option added in v1.3.0

type Option func(o *Options)

func DisableDefaultTag added in v1.4.11

func DisableDefaultTag() Option

func ErrorHandler added in v1.3.0

func ErrorHandler(h func(ctx context.Context, err error) error) Option

func IncomingHeaderMatcher added in v1.3.0

func IncomingHeaderMatcher(m func(key string) (string, bool)) Option

func MetadataAnnotator added in v1.3.0

func MetadataAnnotator(a ...func(ctx context.Context) metadata.MD) Option

func OutgoingHeaderMatcher added in v1.3.0

func OutgoingHeaderMatcher(m func(key string) (string, bool)) Option

func ProtoJSONMarshalOptions added in v1.4.13

func ProtoJSONMarshalOptions(mo protojson.MarshalOptions) Option

func ResponseWrapper added in v1.3.0

func ResponseWrapper(w func(resp any) any) Option

func Tag added in v1.3.0

func Tag(tag string) Option

type Options added in v1.3.0

type Options struct {
	Tag                     string
	DisableDefaultTag       bool
	ResponseWrapper         func(resp any) any
	ErrorHandler            func(ctx context.Context, err error) error
	IncomingHeaderMatcher   func(key string) (string, bool)
	OutgoingHeaderMatcher   func(key string) (string, bool)
	MetadataAnnotators      []func(ctx context.Context) metadata.MD
	ProtoJSONMarshalOptions protojson.MarshalOptions
}

func NewOptions added in v1.4.3

func NewOptions(opts ...Option) *Options

type Render added in v1.2.0

type Render interface {
	Render(ctx context.Context)
}

Render 如果响应参数struct实现Render接口,框架会用自定义渲染逻辑来渲染响应参数

type RichRoute added in v1.1.1

type RichRoute interface {
	Method() string
	Path() string
	Handlers() []gin.HandlerFunc
}

RichRoute 富路由,在 Route 基础上,为路由增加了中间件功能。

func NewRichRoute added in v1.1.1

func NewRichRoute(route Route, middlewares ...gin.HandlerFunc) RichRoute

NewRichRoute 创建一个富路由

func NewRichRoutes added in v1.2.0

func NewRichRoutes(routes []Route, middlewares ...gin.HandlerFunc) []RichRoute

NewRichRoutes 创建一个多个富路由

type Route

type Route interface {
	Method() string
	Path() string
	Handler() gin.HandlerFunc
}

Route 基础路由,定义一个接口路由,包括method、path和处理器

func NewRoute

func NewRoute(method string, path string, handler gin.HandlerFunc) Route

NewRoute 创建一个路由

type ServerTransportStream added in v1.3.0

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

ServerTransportStream implements grpc.ServerTransportStream. It should only be used by the generated files to support grpc.SendHeader outside of gRPC server use.

func NewServerTransportStream added in v1.3.0

func NewServerTransportStream(method string) *ServerTransportStream

func (*ServerTransportStream) Header added in v1.3.0

func (s *ServerTransportStream) Header() metadata.MD

Header returns the header metadata of the stream.

func (*ServerTransportStream) Method added in v1.3.0

func (s *ServerTransportStream) Method() string

func (*ServerTransportStream) SendHeader added in v1.3.0

func (s *ServerTransportStream) SendHeader(md metadata.MD) error

func (*ServerTransportStream) SetHeader added in v1.3.0

func (s *ServerTransportStream) SetHeader(md metadata.MD) error

SetHeader sets the header metadata.

func (*ServerTransportStream) SetTrailer added in v1.3.0

func (s *ServerTransportStream) SetTrailer(md metadata.MD) error

SetTrailer sets the trailer metadata.

func (*ServerTransportStream) Trailer added in v1.3.0

func (s *ServerTransportStream) Trailer() metadata.MD

Trailer returns the cached trailer metadata.

type Status added in v1.3.0

type Status struct {
	Code    int    `` /* 139-byte string literal not displayed */
	Message string `` /* 157-byte string literal not displayed */
}

Status 代表业务状态信息.

type Validator

type Validator interface {
	Validate() error
}

Validator 如果请求参数struct实现Validator接口,框架会验证数据

Jump to

Keyboard shortcuts

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