httprpc

package module
v0.0.0-...-18d5cef Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2018 License: MIT Imports: 8 Imported by: 0

README

httprpc

GoDoc Go Report Card

go get github.com/lukechampine/httprpc

httprpc provides HTTP wrappers for the net/rpc package.

While the net/rpc package does provide DialHTTP and ServeHTTP functions, these only support Go's gob encoding format, and do so by hijacking the underlying HTTP connection. httprpc supports any text-based format satisfying the rpc.ServerCodec/rpc.ClientCodec interfaces (most notably JSON-RPC) by storing request/response payloads in the request/response body.

httprpc has only been tested with JSON-RPC.

Usage

type Args struct {
	A, B int
}

type Arith int

func (t *Arith) Multiply(args *Args, reply *int) error {
	*reply = args.A * args.B
	return nil
}

func jsonBasicAuthHandler(next http.Handler, password string) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		_, pass, ok := req.BasicAuth()
		if !ok || pass != password {
			w.Header().Set("WWW-Authenticate", "Basic realm=\"MyRealm\"")
			http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
			return
		}
		if req.Header.Get("Content-Type") != "application/json" {
			http.Error(w, http.StatusText(http.StatusUnsupportedMediaType), http.StatusUnsupportedMediaType)
			return
		}

		w.Header().Set("Content-Type", "application/json")
		next.ServeHTTP(w, req)
	}
}

func main() {
	// create JSON-RPC HTTP server, wrapped with Basic Auth middleware
	rpc.Register(new(Arith))
	srv := httprpc.NewServer(rpc.DefaultServer, jsonrpc.NewServerCodec)
	srv = jsonBasicAuthHandler(srv, "foo")
	go http.ListenAndServe(":5555", srv)

	// create JSON-RPC HTTP client, wrapped with Basic Auth "tweak"
	c := httprpc.NewClient("http://127.0.0.1:5555", jsonrpc.NewClientCodec, func(req *http.Request) {
		req.SetBasicAuth("", "foo")
		req.Header.Set("Content-Type", "application/json")
	})

	// invoke RPC
	args := &Args{7, 8}
	var reply int
	err := c.Call("Arith.Multiply", args, &reply)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServer

func NewServer(srv *rpc.Server, newCodec NewServerCodecFunc) http.Handler

NewServer returns an http.Handler that handles RPC requests.

Types

type Client

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

A Client can make RPC requests over HTTP.

func NewClient

func NewClient(url string, codecFn NewClientCodecFunc, tweak func(*http.Request)) *Client

NewClient returns a Client that directs its requests at the specified url. If a tweak function is supplied, it is called on each http.Request immediately before the request is performed.

func (*Client) Call

func (c *Client) Call(method string, args interface{}, reply interface{}) error

Call invokes the named function, waits for it to complete, and returns its error status.

func (*Client) Go

func (c *Client) Go(method string, args interface{}, reply interface{}, done chan *rpc.Call) *rpc.Call

Go invokes the function asynchronously. It returns the rpc.Call structure representing the invocation. The done channel will signal when the call is complete by returning the same rpc.Call object. If done is nil, Go will allocate a new channel.

type NewClientCodecFunc

type NewClientCodecFunc func(io.ReadWriteCloser) rpc.ClientCodec

NewClientCodecFunc creates a new rpc.ClientCodec.

type NewServerCodecFunc

type NewServerCodecFunc func(io.ReadWriteCloser) rpc.ServerCodec

NewServerCodecFunc creates a new rpc.ServerCodec.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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