rpcutil

package
v0.0.0-...-0f89747 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2018 License: MIT Imports: 28 Imported by: 10

Documentation

Overview

Package rpcutil provides various methods for working with gorilla's JSON RPC 2 interface (http://www.gorillatoolkit.org/pkg/rpc/v2/json2)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddProxyXForwardedFor

func AddProxyXForwardedFor(dst, src *http.Request)

AddProxyXForwardedFor is deprecated, and has been moved to proxyutil

func InstallCustomValidators

func InstallCustomValidators()

InstallCustomValidators will set up our set of custom validators for the gopkg.in/validator.v2 package. You can see available validators which are installed by looking for the Validate* functions in this package

func IsInternalErr

func IsInternalErr(err error) bool

IsInternalErr returns whether or not this error is due to an internal server error (vs bad request data on the client's part). Returns false if nil, returns true if a non *json2.Error is passed in.

func JSONRPC2Call

func JSONRPC2Call(url string, res interface{}, method string, body interface{}) error

JSONRPC2Call will perform a JSON RPC2 call with the given method and marshalled body against the given url, and unmarshal the response into res. Any errors which occur will be returned. res and body must both be pointer types

func JSONRPC2CallHandler

func JSONRPC2CallHandler(h http.Handler, res interface{}, method string, body interface{}) error

JSONRPC2CallHandler is like JSONRPC2Call, except that the call will be made against the given http.Handler instead of an actual url

func JSONRPC2CallHandlerOpts

func JSONRPC2CallHandlerOpts(
	opts JSONRPC2Opts, h http.Handler, res interface{}, method string, body interface{},
) error

JSONRPC2CallHandlerOpts is like JSONRPC2CallHandler but it takes in a JSONRPC2Opts struct to modify behavior

func JSONRPC2CallOpts

func JSONRPC2CallOpts(opts JSONRPC2Opts, url string, res interface{}, method string, body interface{}) error

JSONRPC2CallOpts is like JSONRPC2Call but it takes in a JSONRPC2Opts struct to modify behavior

func JSONRPC2Handler

func JSONRPC2Handler(codec rpc.Codec, service ...interface{}) http.Handler

JSONRPC2Handler returns an http.Handler which will handle JSON RPC2 calls directed at the given services. A service is a type that has a set of RPC methods on it which will be discovered and used by the rpc package.

This method is super deprecated and very dumb, don't use it

func JSONRPC2RawCall

func JSONRPC2RawCall(url string, res interface{}, method, body string, args ...interface{}) error

JSONRPC2RawCall will perform a JSON RPC2 call with the given method, and a body formed using fmt.Sprintf(body, args...), against the given http.Handler. The result will be unmarshalled into res, which should be a pointer type. Any errors at any point in the process will be returned

res := struct{
	Foo int `json:"foo"`
}{}
err := rpcutil.JSONRPC2RawCall(
	"http://localhost/",
	&res,
	"Foo.Bar",
	`{
		"Arg1":"%s",
		"Arg2":"%d",
	}`,
	"some value for Arg1",
	5, // value for Arg2
)

func JSONRPC2RawCallHandler

func JSONRPC2RawCallHandler(h http.Handler, res interface{}, method, body string, args ...interface{}) error

JSONRPC2RawCallHandler is like JSONRPC2RawCall, except that the call will be made against the given http.Handler instead of an actual url

func JSONRPC2RawCallHandlerOpts

func JSONRPC2RawCallHandlerOpts(
	opts JSONRPC2Opts, h http.Handler, res interface{}, method, body string,
	args ...interface{},
) error

JSONRPC2RawCallHandlerOpts is like JSONRPC2RawCallHandler but it takes in a JSONRPC2Opts struct to modify behavior

func JSONRPC2RawCallOpts

func JSONRPC2RawCallOpts(
	opts JSONRPC2Opts, url string, res interface{}, method, body string,
	args ...interface{},
) error

JSONRPC2RawCallOpts is like JSONRPC2RawCall but it takes in a JSONRPC2Opts struct to modify behavior

func JSONRPC2Request

func JSONRPC2Request(url, method string, body interface{}) (*http.Request, error)

JSONRPC2Request returns an http.Request which can be used to make the given call against the given url. body must be a pointer type

func JSONRPC2RequestOpts

func JSONRPC2RequestOpts(opts JSONRPC2Opts, urlStr, method string, body interface{}) (*http.Request, error)

JSONRPC2RequestOpts is like JSONRPC2Request but it takes in a JSONRPC2Opts struct to modify behavior

func RegisterRegex

func RegisterRegex(name, regex string)

RegisterRegex registers a regex with a given name, so it can be used as an argument to various custom validators installed by this package

func RequestIP

func RequestIP(r *http.Request) string

RequestIP is deprecated, and has been moved to proxyutil

func RequestKV

func RequestKV(r *http.Request) llog.KV

RequestKV returns a basic KV for passing into llog, filled with entries related to the passed in http.Request

func ValidateArrMap

func ValidateArrMap(v interface{}, param string) error

ValidateArrMap maps over an array and validates each element in it using the passed in tag. For example

validator:"min=1,arrMap=min=1,arrMap=max=5,arrMap=regexp=[A-Z]+"

func ValidateEmail

func ValidateEmail(v interface{}, _ string) error

ValidateEmail validates a string as an email address as defined by RFC 5322 Behind the scenes it just uses ParseAddress in the src/net/mail package

func ValidateFutureTime

func ValidateFutureTime(v interface{}, s string) error

ValidateFutureTime validates that the time is > than now + (duration value) The default value is -15m (-15 minutes). It is not exactly 0 hours to prevent client time differences causing user frustration. Do not rely on the default value being -15m as it might change slightly in the future. If you want 0 hours just set "0". For example: futureTime=-4h validates that the time is > now - 4 hours

func ValidateLens

func ValidateLens(v interface{}, param string) error

ValidateLens validates the length of a string or slice and errors if the length is not equal to a passed in option. For numbers it verifies that the number equals one of the passed values. For example

validator:"lens=2|0"

func ValidatePreRegex

func ValidatePreRegex(v interface{}, regexName string) error

ValidatePreRegex will run a regex named by regexName on a string. This is registered as the "preRegex" validator, and can be used like `validate:"preRegex=regexName"` (see gopkg.in/validator.v2) in conjunction with RegisterRegex

When validating this will allow through an empty string

Types

type BufferedResponseWriter

type BufferedResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

BufferedResponseWriter is hella deprecated and has been moved into the proxyutil package

func NewBufferedResponseWriter

func NewBufferedResponseWriter(rw http.ResponseWriter) *BufferedResponseWriter

NewBufferedResponseWriter is deprecated, see the BufferedResponseWriter doc comment

NewBufferedResponseWriter returns an initialized BufferedResponseWriter, which will catch writes going to the given ResponseWriter

func (*BufferedResponseWriter) ActuallyWrite

func (brw *BufferedResponseWriter) ActuallyWrite() (int64, error)

ActuallyWrite takes all the buffered data and actually writes to the wrapped ResponseWriter. Returns the number of bytes written as the body

func (*BufferedResponseWriter) GetBody

func (brw *BufferedResponseWriter) GetBody() (io.ReadCloser, error)

GetBody returns a ReadCloser which gives the body of the response which was buffered. If the response was compressed the body will be transparently decompressed. Close must be called in order to clean up resources. GetBody may be called multiple times, each time it will return an independent ReadCloser for the original response body.

func (*BufferedResponseWriter) MarshalBinary

func (brw *BufferedResponseWriter) MarshalBinary() ([]byte, error)

MarshalBinary returns a binary form of the BufferedResponseWriter. Can only be called after a response has been buffered.

func (*BufferedResponseWriter) SetBody

func (brw *BufferedResponseWriter) SetBody(in io.Reader)

SetBody sets the io.Reader from which the new body of the buffered response should be read from. This io.Reader will be drained once ActuallyWrite is called. If this is never called the original body will be used.

Note: there is no need to send in compressed data here, the new response body will be automatically compressed in the same way the original was.

func (*BufferedResponseWriter) UnmarshalBinary

func (brw *BufferedResponseWriter) UnmarshalBinary(b []byte) error

UnmarshalBinary takes in a byte slice produced by calling MarshalBinary on another BufferedResponseWriter, and makes this one identical to it.

func (*BufferedResponseWriter) Write

func (brw *BufferedResponseWriter) Write(b []byte) (int, error)

func (*BufferedResponseWriter) WriteHeader

func (brw *BufferedResponseWriter) WriteHeader(code int)

WriteHeader catches the call to WriteHeader and doesn't actually do anything, except store the given code for later use when ActuallyWrite is called

type HTTPProxy

type HTTPProxy struct{}

HTTPProxy is also hella deprecated, see ReverseProxy in the proxyutil package

func (*HTTPProxy) ServeHTTP

func (h *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*HTTPProxy) ServeHTTPCtx

func (h *HTTPProxy) ServeHTTPCtx(ctx context.Context, w http.ResponseWriter, r *http.Request) <-chan error

ServeHTTPCtx will do the proxying of the given request and write the response to the ResponseWriter. ctx can be used to cancel the request mid-way.

type JSONRPC2Opts

type JSONRPC2Opts struct {

	// If set this will be used as the request which is sent to the JSONRPC2
	// server. It will be used as is, except:
	//
	// * Method will be set to POST
	// * URL will be set to whatever is passed in
	// * Body will be set to the proper request body
	// * Content-Type header will be set to application/json
	//
	// This is useful if there are other headers or setting you'd like to have
	// on your request going out, such as X-Forwarded-For
	BaseRequest *http.Request

	// If set, this will be used as the context for http request being made.
	// The call will respect the cancellation of the context
	Context context.Context

	// The http client to use. http.DefaultClient will be used if not set
	Client *http.Client
}

JSONRPC2Opts can be passed into any of the JSONRPC2*Opts calls to later their behavior in some way. All fields are optional, see each one for a description of what it does.

type LLCodec

type LLCodec struct {

	// If true any errors which are not user caused (error code < 1) will not
	// actually be returned to the client, only a generic error message in their
	// place
	HideServerErrors bool

	// If true the gopkg.in/validator.v2 package will be used to automatically
	// validate inputs to calls
	ValidateInput bool

	// If true the github.com/levenlabs/go-applicator package will be used to
	// apply functions to your input fields
	RunInputApplicators bool

	// If set, once a non-error response is returned by an rpc endpoint this
	// will be called and the result (if non-nil) will be inlined with the
	// original response. The original response must encode to a json object for
	// this to work.
	//
	// For example, if the original response encodes to `{"success":true}`, and
	// ResponseInliner returns `{"currentTime":123456}`, the final response sent
	// to the client will be `{"success":true,"currentTime":123456}`
	ResponseInliner func(*http.Request) map[string]interface{}

	// All endpoints (fullname, i.e. "Service.Method") set as keys in this map
	// will not have an INFO log printed out when they are hit
	ExcludeRequestLog map[string]bool
	// contains filtered or unexported fields
}

LLCodec wraps around gorilla's json2.Codec, adding logging to all requests

func NewLLCodec

func NewLLCodec() LLCodec

NewLLCodec returns an LLCodec, which is an implementation of rpc.Codec around json2.Codec. All public fields on LLCodec can be modified up intil passing this into rpc.RegisterCodec

func (LLCodec) NewRequest

func (c LLCodec) NewRequest(r *http.Request) rpc.CodecRequest

NewRequest implements the NewRequest method for the rpc.Codec interface

type QuietError

type QuietError json2.Error

A QuietError is a json2.Error except that it will not cause a warn log when returned from a handler

func (*QuietError) Error

func (e *QuietError) Error() string

Jump to

Keyboard shortcuts

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