rpc

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

README

rpc

rpc/v2 support for JSON-RPC 2.0 Specification.

gorilla/rpc is a foundation for RPC over HTTP services, providing access to the exported methods of an object through HTTP requests.

Read the full documentation here: http://www.gorillatoolkit.org/pkg/rpc

Documentation

Overview

Package rpc /*

Index

Constants

This section is empty.

Variables

View Source
var DefaultEncoder = &encoder{}
View Source
var DefaultEncoderSelector = &encoderSelector{}

Functions

func WriteError

func WriteError(w http.ResponseWriter, status int, msg string)

Types

type Codec

type Codec interface {
	NewRequest(*http.Request) Request
}

Codec creates a CodecRequest to process each request.

type CompressionSelector

type CompressionSelector struct {
}

CompressionSelector generates the compressed http encoder.

func (*CompressionSelector) Select

Select method selects the correct compression encoder based on http HEADER.

type Encoder

type Encoder interface {
	Encode(w http.ResponseWriter) io.Writer
}

Encoder interface contains the encoder for http response. Eg. gzip, flate compressions.

type EncoderSelector

type EncoderSelector interface {
	Select(r *http.Request) Encoder
}

EncoderSelector interface provides a way to select encoder using the http request. Typically people can use this to check HEADER of the request and figure out client capabilities. Eg. "Accept-Encoding" tells about supported compressions.

type Request

type Request interface {
	// Method Reads the request and returns the RPC method name.
	Method() (string, error)
	// ReadRequest Reads the request filling the RPC method args.
	ReadRequest(interface{}) error
	// WriteResponse Writes the response using the RPC method reply.
	WriteResponse(http.ResponseWriter, interface{})
	// WriteError Writes an error produced by the server.
	WriteError(w http.ResponseWriter, status int, err error)
}

Request CodecRequest decodes a request and encodes a response using a specific serialization scheme.

type RequestInfo

type RequestInfo struct {
	Method     string
	Error      error
	Request    *http.Request
	StatusCode int
}

RequestInfo contains all the information we pass to before/after functions

type Server

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

Server serves registered RPC services using registered codecs.

func NewServer

func NewServer() *Server

NewServer returns a new RPC server.

func (*Server) HasMethod

func (s *Server) HasMethod(method string) bool

HasMethod returns true if the given method is registered.

The method uses a dotted notation as in "Service.Method".

func (*Server) RegisterAfterFunc

func (s *Server) RegisterAfterFunc(f func(i *RequestInfo))

RegisterAfterFunc registers the specified function as the function that will be called after every request

Note: Only one function can be registered, subsequent calls to this method will overwrite all the previous functions.

func (*Server) RegisterAlias

func (s *Server) RegisterAlias(alias, target string) error

RegisterAlias allows to use different names to call a same rpc method.

func (*Server) RegisterBeforeFunc

func (s *Server) RegisterBeforeFunc(f func(i *RequestInfo))

RegisterBeforeFunc registers the specified function as the function that will be called before every request.

Note: Only one function can be registered, subsequent calls to this method will overwrite all the previous functions.

func (*Server) RegisterCodec

func (s *Server) RegisterCodec(codec Codec, contentType string)

RegisterCodec adds a new codec to the server.

Codecs are defined to process a given serialization scheme, e.g., JSON or XML. A codec is chosen based on the "Content-Type" header from the request, excluding the charset definition.

func (*Server) RegisterInterceptFunc

func (s *Server) RegisterInterceptFunc(f func(i *RequestInfo) *http.Request)

RegisterInterceptFunc registers the specified function as the function that will be called before every request. The function is allowed to intercept the request e.g. add values to the context.

Note: Only one function can be registered, subsequent calls to this method will overwrite all the previous functions.

func (*Server) RegisterMethod

func (s *Server) RegisterMethod(receiver any, name string, method string) error

RegisterMethod adds a new service method to the server.

The name parameter is required

Method from the receiver will be extracted if these rules are satisfied:

  • The receiver is exported (begins with an upper case letter) or local (defined in the package registering the service).
  • The method name is exported.
  • The method has three arguments: *http.Request, *args, *reply.
  • All three arguments are pointers.
  • The second and third arguments are exported or local.
  • The method has return type error.

The method parameter must start with an uppercase letter to indicate that it's exported.

func (*Server) RegisterService

func (s *Server) RegisterService(receiver interface{}, name string) error

RegisterService adds a new service to the server.

The name parameter is optional: if empty it will be inferred from the receiver type name.

Methods from the receiver will be extracted if these rules are satisfied:

  • The receiver is exported (begins with an upper case letter) or local (defined in the package registering the service).
  • The method name is exported.
  • The method has three arguments: *http.Request, *args, *reply.
  • All three arguments are pointers.
  • The second and third arguments are exported or local.
  • The method has return type error.

All other methods are ignored.

func (*Server) RegisterValidateRequestFunc

func (s *Server) RegisterValidateRequestFunc(f func(r *RequestInfo, i interface{}) error)

RegisterValidateRequestFunc registers the specified function as the function that will be called after the BeforeFunc (if registered) and before invoking the actual Service method. If this function returns a non-nil error, the method won't be invoked and this error will be considered as the method result. The first argument is information about the request, useful for accessing to http.Request.Context() The second argument of this function is the already-unmarshalled *args parameter of the method.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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