gateway

package
v0.0.0-...-3a625a3 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2018 License: MIT Imports: 22 Imported by: 0

README

gateway

GoDoc

Gateway is library used for creating a "gateway api", a simple rpc server which forwards requests to other rpc servers that it discovers, based on the service portion of the rpc call's method name.

It also allows for injection of code just before the forwarding, to allow for things like rate-limiting, authentication, and any other behavior you might want to apply to some or all of the calls coming through it.

You can also define methods and services internal to gateway to handle things like sessions or helper methods.

Documentation

Overview

Package gateway implements the actual gateway which will listen for requests and forward them to servers wrapped by gatewayrpc

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gateway

type Gateway struct {
	SRVClient *srvclient.SRVClient

	// BackupHandler, if not nil, will be used to handle the requests which
	// don't have a corresponding backend service to forward to (based on their
	// method)
	BackupHandler http.Handler

	// RequestCallback, if not nil, will be called just before actually
	// forwarding a request onto its backend service. See the Request docstring
	// for more on what is actually possible with this. If you respond to the
	// Request using a Write* method then no forwarding will be done
	RequestCallback func(*Request)

	// CORSMatch, if not nil, will be used against the Origin header. and if it
	// matches Access-Control-Allow-* headers will be sent back, including an
	// Allow-Access-Control-Origin matching the sent in Origin
	CORSMatch *regexp.Regexp
	// contains filtered or unexported fields
}

Gateway is an http.Handler which implements the JSON RPC2 spec, but forwards all of its requests onto backend services

func NewGateway

func NewGateway() *Gateway

NewGateway returns an instantiated Gateway object

func (*Gateway) AddURL

func (g *Gateway) AddURL(u string) error

AddURL performs the RPC.GetServices request against the given url, and will add all returned services to its mapping.

All DNS will be attempted to be resolved using SRV records first, and will use a normal DNS request as a backup

func (*Gateway) GetMethodURL

func (g *Gateway) GetMethodURL(mStr string) (*url.URL, error)

GetMethodURL returns the url which should be used to call the given method ("Service.MethodName"). If the service was originally resolved using a srv request it will be re-resolved everytime this is called, in order to load-balance across instances. Will return an error if the service is unknown, or the resolving fails for some reason.

func (*Gateway) RegisterCodec

func (g *Gateway) RegisterCodec(codec rpc.Codec, contentType string)

RegisterCodec is used to register an encoder/decoder which will operate on requests with the given contentType

func (*Gateway) ServeHTTP

func (g *Gateway) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP satisfies Gateway being a http.Handler

type Request

type Request struct {
	*http.Request
	RemoteMethod gatewaytypes.Method
	ServiceName  string
	// contains filtered or unexported fields
}

Request contains all the data about an incoming request which is currently known. It can be used in conjunction to RequestCallback in order to provide extra functionality or checks to request coming through.

It provides methods to write an error, response, get the method and get the args in a map[string]interface{} that can be modified in order to change the request before it gets sent to the handler

Although the CodecRequest is public, it's use is deprecated. Also, the ResponseWriter should not be used and instead use Write* methods on Request.

func (*Request) Method

func (r *Request) Method() (string, error)

Method returns the RPC method that this request is going to call

func (*Request) ReadRequest

func (r *Request) ReadRequest(v interface{}) error

ReadRequest fills in the args into the passed interface If you change the struct you passed, you must call UpdateRequest and pass the updated struct in order to actually affect the forwarded request

func (*Request) UpdateRequest

func (r *Request) UpdateRequest(method string, params interface{}) error

UpdateRequest takes a new method string and an interface that it json encodes to new params for the request. If method is empty then the method will not be changed. If params is nil, then params will not be changed.

func (*Request) WriteError

func (r *Request) WriteError(status int, err error)

WriteError responds to the client with an error code and error it deals with the CodecRequest so you don't have to After calling, you should return false from the callback

func (*Request) WriteResponse

func (r *Request) WriteResponse(i interface{})

WriteResponse responds to the client with the sent result After calling, you should return false from the callback

Jump to

Keyboard shortcuts

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