ginrpc

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 5 Imported by: 14

README

ginrpc

中文

A Gin middleware for RPC-Style coding

  • High Performance Use generics instead of reflection
  • Simple to use
  • Automatic parameter binding
  • Unified response/error handling

Installation

$ go get github.com/apicat/ginrpc

Usage

base
type In struct {
	ID int64      `uri:"id" binding:"required"`
}

type Out struct {
	Message string `json:"message"`
}

func rpcHandleDemo(c *gin.Context, in *In) (*Out, error) {
	return &Out{
		Message: fmt.Sprintf(" myid = %d", in.ID),
	}, nil
}

func main() {
	e := gin.Default()
	e.POST("/example/:id", ginrpc.Handle(rpcHandleDemo))
}

custom

Inject custom configuration using middleware

  • ReponseRender Customize the response
  • AutomaticBinding Automatic binding default:enable
  • RequestBeforeHook Add a front hook With Handle

See the full example

e := gin.Default()
e.Use(ginrpc.AutomaticBinding(false), ginrpc.RequestBeforeHook(customBind))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutomaticBinding

func AutomaticBinding(v bool) gin.HandlerFunc

AutomaticBinding enable/disable automatic binding and parameter validation

func Handle

func Handle[T any, M any](handler func(*gin.Context, *T) (M, error)) gin.HandlerFunc

Handle for turning handlers into gin.handlefunc

func IsEmpty

func IsEmpty(v any) bool

func ReponseRender

func ReponseRender(r func(*gin.Context, any, *Error)) gin.HandlerFunc

ReponseRender custom response output

func RequestBeforeHook

func RequestBeforeHook(hook ...func(*gin.Context, any, error) error) gin.HandlerFunc

// RequestBeforeHook The input parameter error represents a binding verification error. // If you want to prevent this error, you need to return the error as nil, otherwise, please return the error of the input parameter

Types

type Empty

type Empty struct{}

Empty like google.protobuf.Empty

type Error

type Error struct {
	Code  int   // http statusCode
	Err   error // src error
	Attrs map[string]any
}

Error is a response error with http status code and attributes

func NewError

func NewError(code int, err error) *Error

func (*Error) Error

func (e *Error) Error() string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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