zeroapi

package module
v0.0.0-...-b2d3da9 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2022 License: Apache-2.0 Imports: 28 Imported by: 0

README

zeroapi

go-zero通用的api 让你不需要写api服务

示例

hello

http 响应拦截

详细代码 handler.go

package zeroapi

func (h *Handler) defaultResponseHandler(in proto.Message) (code int, msg string, data interface{}) {
	if in == nil {
		return 0, "", nil
	} else {
		message, ok := in.(*dynamic.Message)
		if ok {
			if cod, exist := protoMessageValue(message, "errCode", 0); exist {
				code = int(InterfaceToInt64(cod))
			}
			if failedReason, exist := protoMessageValue(message, "failedReason", ""); exist && failedReason != "" {
				msg = failedReason.(string)
				if code == 0 {
					code = -1
				}
			}
			data = in
			return
		}
		return 0, "", in
	}
}

http header 转 proto参数

详细代码 requestparser.go

_ = dm.TrySetFieldByName("ip", r.Header.Get("X-Real-IP"))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy(src interface{}) interface{}

Copy creates a deep copy of whatever is passed to it and returns the copy in an interface{}. The returned value will need to be asserted to the correct type.

func CopyByValue

func CopyByValue(original reflect.Value) interface{}

func GetUserIdFromCtx

func GetUserIdFromCtx(ctx context.Context) string

func Iface

func Iface(iface interface{}) interface{}

Iface is an alias to Copy; this exists for backwards compatibility reasons.

func InterfaceToInt64

func InterfaceToInt64(value interface{}) int64

func WithHeaderProcessor

func WithHeaderProcessor(processHeader func(http.Header) []string) func(*Server)

WithHeaderProcessor sets a processor to process request headers. The returned headers are used as metadata to invoke the RPC.

Types

type Config

type Config struct {
	PbGroup               string // 比如用户服务是 pb.userService
	RpcListenOn           string // 比如用户服务是 :8080
	CallRpcTimeoutSeconds int64  `json:",default=10"`
	Port                  int    `json:",default=9000"`
}

Config is the configuration for gateway.

type GatewayEngine

type GatewayEngine struct {
	RestConf rest.RestConf
	Config   Config

	ProtoSets [][]byte
	// contains filtered or unexported fields
}

func Engine

func Engine(serviceConf service.ServiceConf, conf Config, protoSets ...[]byte) *GatewayEngine

func (*GatewayEngine) DELETE

func (e *GatewayEngine) DELETE(url string, handler interface{}, optionFs ...OptionFunc)

func (*GatewayEngine) GET

func (e *GatewayEngine) GET(url string, handler interface{}, optionFs ...OptionFunc)

func (*GatewayEngine) Group

func (e *GatewayEngine) Group(prefix string) (n *GatewayEngine)

func (*GatewayEngine) PATCH

func (e *GatewayEngine) PATCH(url string, handler interface{}, optionFs ...OptionFunc)

func (*GatewayEngine) POST

func (e *GatewayEngine) POST(url string, handler interface{}, optionFs ...OptionFunc)

func (*GatewayEngine) PUT

func (e *GatewayEngine) PUT(url string, handler interface{}, optionFs ...OptionFunc)

func (*GatewayEngine) Server

func (e *GatewayEngine) Server(serverOptions ...rest.RunOption) *Server

func (*GatewayEngine) Upstreams

func (e *GatewayEngine) Upstreams() []upstream

func (*GatewayEngine) Use

func (e *GatewayEngine) Use(middlewares ...rest.Middleware)

type Handler

type Handler struct {
	*grpcurl.DefaultEventHandler
	// contains filtered or unexported fields
}

func NewHandler

func NewHandler(w http.ResponseWriter, source grpcurl.DescriptorSource, optionFs ...OptionFunc) *Handler

func (*Handler) OnReceiveResponse

func (h *Handler) OnReceiveResponse(resp proto.Message)

func (*Handler) OnReceiveTrailers

func (h *Handler) OnReceiveTrailers(stat *status.Status, md metadata.MD)

type Interface

type Interface interface {
	DeepCopy() interface{}
}

Interface for delegating copy process to type

type Option

type Option func(svr *Server)

Option defines the method to customize Server.

type OptionFunc

type OptionFunc func(*handlerOption)

func WithBuildHandler

func WithBuildHandler(resp interface{}) OptionFunc

func WithResponseHandler

func WithResponseHandler(responseHandler ResponseHandler) OptionFunc

type ResponseHandler

type ResponseHandler func(in proto.Message) (code int, msg string, data interface{})

func BuildHandler

func BuildHandler(resp interface{}) ResponseHandler

type RouteMapping

type RouteMapping struct {
	// Method is the HTTP method, like GET, POST, PUT, DELETE.
	Method string
	// Path is the HTTP path.
	Path string
	// RpcPath is the gRPC rpc method, with format of package.service/method
	RpcPath string
	// contains filtered or unexported fields
}

RouteMapping is a mapping between a gateway route and an upstream rpc method.

type Server

type Server struct {
	*rest.Server
	// contains filtered or unexported fields
}

Server is a gateway server.

func (*Server) Start

func (s *Server) Start()

Start starts the gateway server.

func (*Server) Stop

func (s *Server) Stop()

Stop stops the gateway server.

type XResponse

type XResponse struct {
	Code       int32       `json:"code"`
	Msg        string      `json:"msg"`
	Data       interface{} `json:"data,omitempty"`
	ServerTime int64       `json:"server_time"`
}

Jump to

Keyboard shortcuts

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